Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pokus;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.image.BufferedImage;
- import javax.swing.JPanel;
- public class Renderer extends JPanel{
- private static final long serialVersionUID = -4803438613828508577L;
- private BufferedImage img;
- private Color colorRGB;
- private Color colorHSL;
- private float hue;
- private float saturation;
- private float lightness;
- public Renderer(int width, int height) {
- setPreferredSize(new Dimension(width,height));
- }
- public void paint(Graphics g) {
- g.drawImage(img, 0, 0, null);
- }
- public BufferedImage changeColor(BufferedImage img) {
- BufferedImage imgNew = new BufferedImage(img.getWidth(),img.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
- for(int y = 0; y < img.getHeight(); y++){
- for(int x = 0; x < img.getWidth(); x++){
- Color c = new Color(img.getRGB(x, y));
- int a = c.getAlpha();
- int r = 255 - c.getRed();
- int g = 255 - c.getGreen();
- int b = 255 - c.getBlue();
- Color newC = new Color(r,g,b,a);
- imgNew.setRGB(x, y, newC.getRGB());
- /*
- System.out.println("Zaklad:"+c);
- HSLColor color;
- color = new HSLColor(c);
- // System.out.println(color);
- System.out.println(RGBtoHSL(HSLtoRGB(c)));*/
- }
- }
- return imgNew;
- }
- public BufferedImage getImg() {
- return img;
- }
- public void setImg(BufferedImage img) {
- this.img = img;
- }
- private Color RGBtoHSL(Color colorRGB)
- {
- float r, g, b, h, s, l; //this function works with floats between 0 and 1
- r = colorRGB.getRed() / 256f;
- g = colorRGB.getGreen() / 256f;
- b = colorRGB.getBlue() / 256f;
- float maxColor = Math.max(r, Math.max(g, b));
- float minColor = Math.min(r, Math.min(g, b));
- if (minColor == maxColor) {
- //R == G == B, so it's a shade of gray
- h = 0f; //it doesn't matter what value it has
- s = 0f;
- l = r; //doesn't matter if you pick r, g, or b
- }
- else
- {
- l = (minColor + maxColor) / 2;
- if(l < 0.5) s = (maxColor - minColor) / (maxColor + minColor);
- else s = (maxColor - minColor) / (2f - maxColor - minColor);
- if(r == maxColor) h = (g - b) / (maxColor - minColor);
- else if(g == maxColor) h = 2f + (b - r) / (maxColor - minColor);
- else h = 4f + (r - g) / (maxColor - minColor);
- h /= 6; //to bring it to a number between 0 and 1
- if(h < 0) h ++;
- }
- Color colorHSL = new Color((int)(h*255+0.5),(int)(s*255+0.5),(int)(l*255+0.5));
- return colorHSL;
- }
- private Color HSLtoRGB(Color colorHSL)
- {
- float r, g, b, h, s, l; //this function works with floats between 0 and 1
- float temp1, temp2, tempr, tempg, tempb;
- h = colorHSL.getRed() / 256f;
- s = colorHSL.getGreen() / 256f;
- l = colorHSL.getBlue() / 256f;
- //If saturation is 0, the color is a shade of gray
- if(s == 0) r = g = b = l;
- //If saturation > 0, more complex calculations are needed
- else
- {
- //Set the temporary values
- if(l < 0.5) temp2 = l * (1 + s);
- else temp2 = (l + s) - (l * s);
- temp1 = 2 * l - temp2;
- tempr = h + 1 / 3f;
- if(tempr > 1) tempr--;
- tempg = h;
- tempb = h - 1 / 3f;
- if(tempb < 0) tempb++;
- //Red
- if(tempr < 1 / 6f) r = temp1 + (temp2 - temp1) * 6 * tempr;
- else if(tempr < 0.5) r = temp2;
- else if(tempr < 2 / 3f) r = temp1 + (temp2 - temp1) * ((2 / 3f) - tempr) * 6;
- else r = temp1;
- //Green
- if(tempg < 1 / 6f) g = temp1 + (temp2 - temp1) * 6 * tempg;
- else if(tempg < 0.5) g = temp2;
- else if(tempg < 2 / 3f) g = temp1 + (temp2 - temp1) * ((2 / 3f) - tempg) * 6;
- else g = temp1;
- //Blue
- if(tempb < 1 / 6f) b = temp1 + (temp2 - temp1) * 6 * tempb;
- else if(tempb < 0.5) b = temp2;
- else if(tempb < 2 / 3f) b = temp1 + (temp2 - temp1) * ((2 / 3f) - tempb) * 6;
- else b = temp1;
- }
- Color colorRGB = new Color((int)(r*255+0.5),(int)(g*255+0.5),(int)(b*255+0.5));
- return colorRGB;
- }
- public BufferedImage changeHSL(BufferedImage img) {
- BufferedImage imgNew = new BufferedImage(img.getWidth(),img.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
- for(int x = 0; x < img.getWidth(); x++) {
- for(int y = 0; y < img.getHeight(); y++)
- {
- //store the color of the image in variables R, G and B
- colorRGB = new Color(img.getRGB(x, y));
- //calculate H, S and L out of R, G and B
- colorHSL = RGBtoHSL(colorRGB);
- //change Hue
- float h = colorHSL.getRed()/255f;
- float s = colorHSL.getGreen()/255f;
- float l = colorHSL.getBlue()/255f;
- h += hue;
- // h += 42.5 * 1;
- // h %= 255;
- colorHSL = new Color(h,s,l);
- //convert back to RGB
- colorRGB = HSLtoRGB(colorHSL);
- //plot the pixel
- imgNew.setRGB(x, y, colorRGB.getRGB());
- }
- }
- return imgNew;
- }
- public void setHue(float hue) {
- this.hue = hue;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement