How to get RGB value from hexadecimal color code in java
If you have a string this way is a lot nicer:
Color color = Color.decode("0xFF0000");
int red = color.getRed();
int blue = color.getBlue();
int green = color.getGreen();
If you have a number then do it this way:
Color color = new Color(0xFF0000);
Then of course to get the colours you just do:
float red = color.getRed();
float green = color.getGreen();
float blue = color.getBlue();
Assuming this is a string:
// edited to support big numbers bigger than 0x80000000
int color = (int)Long.parseLong(myColorString, 16);
int r = (color >> 16) & 0xFF;
int g = (color >> 8) & 0xFF;
int b = (color >> 0) & 0xFF;
I'm not sure about your exact need. However some tips.
Integer class can transform a decimal number to its hexadecimal representation with the method:
Integer.toHexString(yourNumber);
To get the RGB you can use the class Color:
Color color = new Color(4898901);
float r = color.getRed();
float g = color.getGreen();
float b = color.getBlue();