Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.53 KB | None | 0 0
  1. package pokus;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.image.BufferedImage;
  7.  
  8. import javax.swing.JPanel;
  9.  
  10. public class Renderer extends JPanel{
  11.     private static final long serialVersionUID = -4803438613828508577L;
  12.  
  13.     private BufferedImage img;
  14.     private Color colorRGB;
  15.     private Color colorHSL;
  16.     private float hue;
  17.     private float saturation;
  18.     private float lightness;
  19.    
  20.     public Renderer(int width, int height) {
  21.         setPreferredSize(new Dimension(width,height));
  22.     }
  23.     public void paint(Graphics g) {
  24.         g.drawImage(img, 0, 0, null);
  25.     }  
  26.     public BufferedImage changeColor(BufferedImage img) {
  27.         BufferedImage imgNew = new BufferedImage(img.getWidth(),img.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);    
  28.         for(int y = 0; y < img.getHeight(); y++){
  29.             for(int x = 0; x < img.getWidth(); x++){
  30.                 Color c = new Color(img.getRGB(x, y));
  31.                 int a = c.getAlpha();
  32.                 int r = 255 - c.getRed();
  33.                 int g = 255 - c.getGreen();
  34.                 int b = 255 - c.getBlue();
  35.                 Color newC = new Color(r,g,b,a);
  36.                 imgNew.setRGB(x, y, newC.getRGB());
  37. /*
  38.                 System.out.println("Zaklad:"+c);
  39.                
  40.                 HSLColor color;
  41.                 color = new HSLColor(c);
  42.                // System.out.println(color);
  43.                 System.out.println(RGBtoHSL(HSLtoRGB(c)));*/
  44.             }
  45.         }
  46.         return imgNew;
  47.     }
  48.     public BufferedImage getImg() {
  49.         return img;
  50.     }
  51.     public void setImg(BufferedImage img) {
  52.         this.img = img;
  53.     }
  54.    
  55.     private Color RGBtoHSL(Color colorRGB)
  56.     {
  57.         float r, g, b, h, s, l; //this function works with floats between 0 and 1
  58.         r = colorRGB.getRed() / 256f;
  59.         g = colorRGB.getGreen() / 256f;
  60.         b = colorRGB.getBlue() / 256f;
  61.        
  62.         float maxColor = Math.max(r, Math.max(g, b));
  63.         float minColor = Math.min(r, Math.min(g, b));
  64.        
  65.        
  66.         if (minColor == maxColor) {
  67.             //R == G == B, so it's a shade of gray
  68.              
  69.                 h = 0f; //it doesn't matter what value it has      
  70.                 s = 0f;      
  71.                 l = r; //doesn't matter if you pick r, g, or b  
  72.            
  73.         }
  74.         else
  75.         {  
  76.             l = (minColor + maxColor) / 2;    
  77.            
  78.             if(l < 0.5) s = (maxColor - minColor) / (maxColor + minColor);
  79.             else s = (maxColor - minColor) / (2f - maxColor - minColor);
  80.            
  81.             if(r == maxColor) h = (g - b) / (maxColor - minColor);
  82.             else if(g == maxColor) h = 2f + (b - r) / (maxColor - minColor);
  83.             else h = 4f + (r - g) / (maxColor - minColor);
  84.            
  85.             h /= 6; //to bring it to a number between 0 and 1
  86.             if(h < 0) h ++;
  87.         }
  88.         Color colorHSL = new Color((int)(h*255+0.5),(int)(s*255+0.5),(int)(l*255+0.5));
  89.        
  90.         return colorHSL;
  91.     }
  92.     private Color HSLtoRGB(Color colorHSL)
  93.     {
  94.         float r, g, b, h, s, l; //this function works with floats between 0 and 1
  95.         float temp1, temp2, tempr, tempg, tempb;
  96.         h = colorHSL.getRed() / 256f;
  97.         s = colorHSL.getGreen() / 256f;
  98.         l = colorHSL.getBlue() / 256f;
  99.        
  100.       //If saturation is 0, the color is a shade of gray
  101.         if(s == 0) r = g = b = l;
  102.         //If saturation > 0, more complex calculations are needed
  103.         else
  104.         {
  105.             //Set the temporary values      
  106.             if(l < 0.5) temp2 = l * (1 + s);      
  107.             else temp2 = (l + s) - (l * s);    
  108.             temp1 = 2 * l - temp2;    
  109.             tempr = h + 1 / 3f;    
  110.             if(tempr > 1) tempr--;
  111.             tempg = h;    
  112.             tempb = h - 1 / 3f;
  113.             if(tempb < 0) tempb++;
  114.            
  115.             //Red    
  116.             if(tempr < 1 / 6f) r = temp1 + (temp2 - temp1) * 6 * tempr;      
  117.             else if(tempr < 0.5) r = temp2;  
  118.             else if(tempr < 2 / 3f) r = temp1 + (temp2 - temp1) * ((2 / 3f) - tempr) * 6;
  119.             else r = temp1;
  120.            
  121.             //Green      
  122.             if(tempg < 1 / 6f) g = temp1 + (temp2 - temp1) * 6 * tempg;    
  123.             else if(tempg < 0.5) g = temp2;
  124.             else if(tempg < 2 / 3f) g = temp1 + (temp2 - temp1) * ((2 / 3f) - tempg) * 6;
  125.             else g = temp1;
  126.            
  127.             //Blue    
  128.             if(tempb < 1 / 6f) b = temp1 + (temp2 - temp1) * 6 * tempb;  
  129.             else if(tempb < 0.5) b = temp2;
  130.             else if(tempb < 2 / 3f) b = temp1 + (temp2 - temp1) * ((2 / 3f) - tempb) * 6;    
  131.             else b = temp1;
  132.         }
  133.         Color colorRGB = new Color((int)(r*255+0.5),(int)(g*255+0.5),(int)(b*255+0.5));
  134.        
  135.         return colorRGB;
  136.     }
  137.     public BufferedImage changeHSL(BufferedImage img) {
  138.         BufferedImage imgNew = new BufferedImage(img.getWidth(),img.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);    
  139.          
  140.         for(int x = 0; x < img.getWidth(); x++) {
  141.             for(int y = 0; y < img.getHeight(); y++)
  142.             {
  143.                 //store the color of the image in variables R, G and B
  144.                 colorRGB = new Color(img.getRGB(x, y));
  145.                 //calculate H, S and L out of R, G and B
  146.                 colorHSL = RGBtoHSL(colorRGB);
  147.                 //change Hue
  148.                 float h = colorHSL.getRed()/255f;
  149.                 float s = colorHSL.getGreen()/255f;
  150.                 float l = colorHSL.getBlue()/255f;
  151.                
  152.                 h += hue;
  153.               //  h += 42.5 * 1;
  154.                // h %= 255;
  155.                
  156.                 colorHSL = new Color(h,s,l);
  157.                 //convert back to RGB
  158.                 colorRGB = HSLtoRGB(colorHSL);
  159.                 //plot the pixel
  160.                 imgNew.setRGB(x, y, colorRGB.getRGB());
  161.                
  162.             }
  163.         }
  164.         return imgNew;
  165.     }
  166.     public void setHue(float hue) {
  167.         this.hue = hue;
  168.        
  169.     }
  170.    
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement