简体   繁体   中英

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

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

You can create a Color object (the values should either be int s between 0 - 255 or float s 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());

you can also use

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM