Java – hex color to RGB color
I have a decimal (non hexadecimal) color code. Using Java, I need to convert it to three RGB colors
For example, 16777215 (pure white) needs to be converted to red: 255 green: 255 blue: 255 65280 (pure green) needs to be converted to red: 0 green 255: Blue: 0 here is the converter for more examples
Just do some small calculations and play the calculator on the page linked above. I have determined:
>Red equals 65536 (256 ^ 2)
>(255 × 65536 = 16711680 (also known as pure red)
>Green equals 256 (256 ^ 1)
>(255 × 256 = 65280 (also known as pure green)
>Blue equals 1 (256 ^ 0)
>(255 × 1 = 255, also known as pure blue)
I can tell it's obviously related to bytes, but I missed the last point I'm not the best at the whole concept of bits / bytes / etc and how it interacts with Java, so it may be quite simple
So, who knows the best way? What is the best way to convert a single numeric decimal color to three separate RGB values using Java?
Solution
You can do it
Color color = new Color(16777215); int red = color.getRed(); int green = color.getGreen(); int blue = color.getBlue();