views:

158

answers:

3

i have color= #12FFFF . that is color in this format where 12FFFF are hexadecima numbers.Now i want to get the each of indepenent R,G,B componetents in decimal. How do i do it in java?

A: 

Use bit operations - shifts and masks:

int rgb = 0x123456;

int red = (rgb >>> 16) & 0xff;
int green = (rgb >>> 8) & 0xff;
int blue = (rgb >>> 0) & 0xff;

(Obviously the right-shift-by-0 is irrelevant, but it's nicely consistent.)

If you don't already have your RGB value as an integer, please give more details in your question.

Jon Skeet
+2  A: 

It's not clear what your question is, but assuming color is a string, then I think you can do this:

String color = "#12FFFF";
int rgb = Integer.decode(color);
Color c = new Color(rgb);
int red = c.getRed();
int green = c.getGreen();
int blue = c.getBlue();

Here's the doc for Color

job
A: 
int rgb = 0x123456;

Color c = new Color(rgb);
int red = c.getRed();
int blue = c.getBlue();
int green = c.getGreen();

If the hex is in a String you'll need to create a Long first and take the intValue() to construct the color.

artran