[英]Apply tint to grayscale png image with transparency in Java
I need to apply a tint to a grayscale BufferedImage with transparency without influencing the transparency itself, this is the method i use, but when i try to draw it on top of another image inside a canvas, the transparency of the original image is gone, leaving a square of the raw selected color... 我需要在不影响透明度本身的情况下对具有透明度的灰度BufferedImage施加色调,这是我使用的方法,但是当我尝试将其绘制在画布内的另一个图像之上时,原始图像的透明度消失了,留下原始所选颜色的正方形...
private BufferedImage colorize(BufferedImage original,Color tint) { //TODO fix!!!
BufferedImage img = new BufferedImage(original.getWidth(), original.getHeight(),BufferedImage.TYPE_4BYTE_ABGR);
int redVal=tint.getRed();
int greenVal=tint.getGreen();
int blueVal=tint.getBlue();
for (int x=0;x<original.getWidth();x++) for (int y=0;y<original.getHeight();y++) {
Color pixelVal=new Color(original.getRGB(x, y));
int grayValue=pixelVal.getRed(); //Any basic color works the same
int alpha=pixelVal.getAlpha();
int newRed= (redVal*(grayValue))/255;
int newGreen= (greenVal*grayValue)/255;
int newBlue= (blueVal*grayValue)/255;
img.setRGB(x, y, new Color(newRed,newGreen,newBlue,alpha).getRGB());
}
return img;}
Any hint? 有什么提示吗?
Use the Color
constuctor that takes two arguments : 使用带有两个参数的
Color
构造函数 :
Color pixelVal = new Color(original.getRGB(x, y), true);
This constructor creates a Color
with an ARGB color, if the hasAlpha
parameter is true
. 如果
hasAlpha
参数为true
,则此构造方法将创建具有ARGB颜色的Color
。
The single argument constructor Color(int)
does not take transparency into account. 单参数的构造
Color(int)
不采取透明度考虑。 From the JavaDoc: 从JavaDoc:
Creates an opaque sRGB color with the specified combined RGB value [...] Alpha is defaulted to 255.
使用指定的组合RGB值创建不透明的sRGB颜色。Alpha默认为255。
Apart from that, your code should probably work. 除此之外,您的代码可能应该可以工作。 :-)
:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.