改变java中的图标颜色

发布于 2025-01-11 05:29:14 字数 537 浏览 0 评论 0原文

所以我正在使用一个可以根据用户的要求更改主题的程序。现在我必须根据主题更改图标颜色。 假设我有一个图标“Cut.png”,因此它将有一个大的透明区域和一些黑色线条,所以我希望这些线条(任何颜色)更改为所需的颜色,并且透明区域相同。我正在尝试使用缓冲图像,我跳过了 0 alpha 的像素,但输出给了我一个完全黑色的图标:(

这是代码的一部分:

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);
    }

我的程序完全相同,我希望它应该忽略透明像素应该将彩色像素更改为红色,但生成的输出是全黑的:(

感谢您的帮助:)

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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倒数 2025-01-18 05:29:14

确保您的目标图像也是透明的

在此处输入图像描述

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);

同样,您也可以使用 Graphics API 直接绘制目标图像

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);

,或者您也可以只是对图像进行“着色” ,对于 示例

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文