Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format of TYPE_INT_RGB and TYPE_INT_ARGB

Could anyone explain for me how java stores color in TYPE_INT_RGB and TYPE_INT_ARGB ?
Do these lines of code work properly for calculating red, green and blue ?

int red= (RGB>>16)&255;
int green= (RGB>>8)&255;
int blue= (RGB)&255;

And what about TYPE_INT_ARGB ? How can I get red, green and blue from TYPE_INT_ARGB?

like image 959
Pro.Hessam Avatar asked May 14 '11 10:05

Pro.Hessam


People also ask

What is Type_int_argb?

TYPE_INT_ARGB. Represents an image with 8-bit RGBA color components packed into integer pixels. static int. TYPE_INT_ARGB_PRE. Represents an image with 8-bit RGBA color components packed into integer pixels.

What is a buffered image?

A BufferedImage is essentially an Image with an accessible data buffer. It is therefore more efficient to work directly with BufferedImage. A BufferedImage has a ColorModel and a Raster of image data. The ColorModel provides a color interpretation of the image's pixel data.


2 Answers

The TYPE_INT_ARGB represents Color as an int (4 bytes) with alpha channel in bits 24-31, red channels in 16-23, green in 8-15 and blue in 0-7.

The TYPE_INT_RGB represents Color as an int (4 bytes) int the same way of TYPE_INT_ARGB, but the alpha channel is ignored (or the bits 24-31 are 0).

Look the javadoc of java.awt.Color and java.awt.image.BufferedImage.

like image 179
Alberto Avatar answered Sep 19 '22 15:09

Alberto


You are correct for TYPE_INT_RGB. The equivalent TYPE_INT_ARGB code would be:

int rgb = rgbColor.getRGB(); //always returns TYPE_INT_ARGB
int alpha = (rgb >> 24) & 0xFF;
int red =   (rgb >> 16) & 0xFF;
int green = (rgb >>  8) & 0xFF;
int blue =  (rgb      ) & 0xFF;

Spelling out the color elements for the bytes from most significant to least significant, you get ARGB, hence the name.

like image 27
Falkreon Avatar answered Sep 19 '22 15:09

Falkreon