I have the integer pixel I got from getRGB(x,y)
, but I don't have any clue about how to convert it to RGBA format. For example, -16726016
should be Color(0,200,0,255)
. Any tips?
Asked
Active
Viewed 6.0k times
2 Answers
51
If I'm guessing right, what you get back is an unsigned integer of the form 0xAARRGGBB
, so
int b = (argb)&0xFF;
int g = (argb>>8)&0xFF;
int r = (argb>>16)&0xFF;
int a = (argb>>24)&0xFF;
would extract the color components. However, a quick look at the docs says that you can just do
Color c = new Color(argb);
or
Color c = new Color(argb, true);
if you want the alpha component in the Color as well.
UPDATE
Red and Blue components are inverted in original answer, so the right answer will be:
int r = (argb>>16)&0xFF;
int g = (argb>>8)&0xFF;
int b = (argb>>0)&0xFF;
updated also in the first piece of code
-
@Gevorg: Might be. But that's fairly obvious to notice when testing the code. :) – AKX Dec 08 '11 at 07:53
-
@AKX Unless you test it with (0,200,0,255)! ;) – Marsellus Wallace Dec 08 '11 at 15:51
-
3According to http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html#getRGB%28%29: Bits 24-31 are alpha, 16-23 are red, 8-15 are green, 0-7 are blue – Jack Apr 08 '14 at 23:40
28
Color c = new Color(-16726016, true);
System.out.println(c.getRed());
System.out.println(c.getGreen());
System.out.println(c.getBlue());
System.out.println(c.getAlpha());
prints out:
0
200
0
255
Is that what you mean?

laher
- 8,860
- 3
- 29
- 39