PRINCE KUMAR
PRINCE KUMAR

Reputation: 9

Changing icon color in java

So I am working with a program that can change theme as per the user's requirements. Now I have got to change the icon color as per the theme. Suppose I have an icon "Cut.png" so it will have a large transparent area and some black lines So I want those lines(of any color) to change to the required color and the transparent area to be same. I am trying with a buffered image and I skipped the pixel that has 0 alpha but the output gives me a completely black icon :(

Here is some part of the code :

BufferedImage buf = ImageIO.read(Image Path);
int red = Color.RED.getRBG();
for(....)//Loop for Row
    for(....)//Loop for column
    {
      Color col = new Color(buf.getRGB(x,y));
      if(col.getAlpha() == 0) continue;
      buf.setRGB(x,y,red);
    }

My program is exactly the same and I expect that it should ignore transparent pixels and should change the colored pixels to red but the output produced is completely black :(

Thanks for your help :)

Upvotes: 0

Views: 456

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347332

Make sure your target image is also transparent

enter image description here

BufferedImage img = ImageIO.read(Main.class.getResource("/images/ArrowRight.png"));
BufferedImage coloredImg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
Color targetColor = Color.RED;
for (int y = 0; y < img.getHeight(); y++) {
    for (int x = 0; x < img.getWidth(); x++) {
        int rgb = img.getRGB(x, y);
        Color color = new Color(rgb, true);
        if (color.getAlpha() > 0) {
            Color pixelColor = new Color(targetColor.getRed(), targetColor.getGreen(), targetColor.getBlue(), color.getAlpha());
            coloredImg.setRGB(x, y, pixelColor.getRGB());
        }
    }
}

JPanel pane = new JPanel();
pane.add(new JLabel(new ImageIcon(img)));
pane.add(new JLabel(new ImageIcon(coloredImg)));

JOptionPane.showMessageDialog(null, pane);

Equally, you could also make use of Graphics API and paint to the target image directly

BufferedImage img = ImageIO.read(Main.class.getResource("/images/ArrowRight.png"));
BufferedImage coloredImg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
Color targetColor = Color.RED;

Graphics2D g2d = coloredImg.createGraphics();
g2d.setColor(targetColor);

for (int y = 0; y < img.getHeight(); y++) {
    for (int x = 0; x < img.getWidth(); x++) {
        int rgb = img.getRGB(x, y);
        Color color = new Color(rgb, true);
        if (color.getAlpha() > 0) {
            float alpha = color.getAlpha() / 255.0f;
            g2d.setComposite(AlphaComposite.SrcOver.derive(alpha));
            g2d.fillRect(x, y, 1, 1);
        }
    }
}

g2d.dispose();

JPanel pane = new JPanel();
pane.add(new JLabel(new ImageIcon(img)));
pane.add(new JLabel(new ImageIcon(coloredImg)));

JOptionPane.showMessageDialog(null, pane);

Or you could just "tint" the image instead, for example

Upvotes: 2

Related Questions