Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

image

rgb

rgba

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

like image 422
Illep Avatar asked Jul 18 '11 14:07

Illep


People also ask

How are RGB values written?

The format of the RGB Value The format of an RGB value in the functional notation is 'rgb(' followed by a comma-separated list of three numerical values (three integer values(0-255, 0-255, 0-255)) followed by ')'.

What does R G and B in RGB value of HTML color mean?

RGB (red, green, and blue) refers to a system for representing the colors to be used on a computer display. Red, green, and blue can be combined in various proportions to obtain any color in the visible spectrum. Levels of R, G, and B can each range from 0 to 100 percent of full intensity.

What is the alpha component of a color?

The alpha component specifies the transparency of the color: 0 is fully transparent, and 255 is fully opaque. Likewise, an A value of 255 represents an opaque color. An A value from 1 through 254 represents a semitransparent color. The color becomes more opaque as A approaches 255.


1 Answers

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());
like image 62
dacwe Avatar answered Oct 12 '22 00:10

dacwe