How do I set the R, G, B and Alpha components of a color?

爷,独闯天下 提交于 2019-12-09 02:53:33

问题


There are 3 integer values that makes up a RGB value, and also i have the Alpha component value of the color. how do i set these 4 values to get the desired colour


回答1:


You can create a Color object (the values should either be ints between 0-255 or floats between 0f-1f:

Color c = new Color(red, green, blue, alpha);

If you want to paint an image with that color:

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

If you only want to set a pixel (color model must be ARGB):

image.setRGB(50, 50, c.getRGB());



回答2:


you can also use

int colorToSet = Color.argb(alpha, red, green, blue); to set Alpha


来源:https://stackoverflow.com/questions/6734171/how-do-i-set-the-r-g-b-and-alpha-components-of-a-color

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!