Marek Marek - 4 months ago 13x
Java Question

Convert integer color value to RGB

I am trying to modify a third party software. I want to use a color which is returned by some methods (which I cant modifiy) as an integer. However, I would like to use RGB format, like #FF00FF. How can I make a conversion?

Here is an HTML example
I would like to archive same thing in Java, on Android.


Use this

String hexColor = String.format("#%06X", (0xFFFFFF & intColor));

We know lenght of color value in HEX is 6. So you see 6 here. %06X matches the result coming from (0xFFFFFF & intColor) and if length is less than 6, it makes result with 6 by appending ZERO to left side of result. And you see #, so this # char gets appended to result and finally you get a HEX COLOR value.