如何设置颜色的R,G,B和Alpha分量?
有3个整数值构成RGB值,我也有颜色的Alpha分量值。 如何设置这4个值以获得所需的颜色
您可以创建一个Color
对象(值应该是介于0
到255
之间的int
或者介于0f
– 1f
之间的float
:
Color c = new Color(red, green, blue, alpha);
如果要使用该颜色绘制图像:
BufferedImage image = new BufferedImage(300, 200, BufferedImage.TYPE_INT_ARGB); Graphics graphics = image.getGraphics(); graphics.setColor(c); graphics.fillRect(50, 50, 100, 100); graphics.dispose();
如果您只想设置一个像素(颜色模型必须是ARGB):
image.setRGB(50, 50, c.getRGB());
你也可以用
int colorToSet = Color.argb(alpha, red, green, blue); to set Alpha