在Java中更改png的不透明部分的颜色 [英] Change color of non-transparent parts of png in Java
问题描述
我尝试自动更改一组图标的颜色。
每个图标都有一个白色填充层,其他部分是透明的。
下面是一个例子:(在这种情况下它是绿色的,只是为了使它可见)
I am trying to automatically change the color for a set of icons. Every icon has a white filled layer and the other part is transparent. Here is an example: (in this case it's green, just to make it visible)
我尝试过以下操作:
private static BufferedImage colorImage(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
for (int xx = 0; xx < width; xx++) {
for (int yy = 0; yy < height; yy++) {
Color originalColor = new Color(image.getRGB(xx, yy));
System.out.println(xx + "|" + yy + " color: " + originalColor.toString() + "alpha: "
+ originalColor.getAlpha());
if (originalColor.equals(Color.WHITE) && originalColor.getAlpha() == 255) {
image.setRGB(xx, yy, Color.BLUE.getRGB());
}
}
}
return image;
}
我遇到的问题是每个像素都有相同的值: / p>
The problem I have is that every pixel I get has the same value:
32|18 color: java.awt.Color[r=255,g=255,b=255]alpha: 255
所以我的结果只是一个彩色的正方形。
如何实现仅改变不透明部分的颜色?为什么呢,所有的像素都有相同的alpha值?我想我的主要问题:alpha值不能正确读取。
So my result is just a colored square. How can I achieve to change the color of the non-transparent parts only? And why is it, that all pixels have even the same alpha value? I guess that#s my main problem: That the alpha value isn't read correctly.
推荐答案
问题是, / p>
The problem is, that
Color originalColor = new Color(image.getRGB(xx, yy));
丢弃所有的alpha值。您必须使用
discards all the alpha values. Instead you have to use
Color originalColor = new Color(image.getRGB(xx, yy), true);
以保持Alpha值可用。
to keep alpha values available.
这篇关于在Java中更改png的不透明部分的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!