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 http://www.shodor.org/stella2java/rgbint.html I would like to archive same thing in Java, on Android.
Hex to RGB conversionGet the 2 left digits of the hex color code and convert to decimal value to get the red color level. Get the 2 middle digits of the hex color code and convert to decimal value to get the green color level.
0. In RGB, a color is defined as a mixture of pure red, green, and blue lights of various strengths. Each of the red, green and blue light levels is encoded as a number in the range 0.. 255, with 0 meaning zero light and 255 meaning maximum light.
RGB Integer Functional Notation: The syntax looks like rgb(integer,integer,integer). The first integer represents the color red, the second integer the color green, and the third integer the color blue. Each integer can range in value from 0 to 255.
Enter a decimal value for Green in the G field. Enter a decimal value for Blue in the B field. Click on the "Calculate Decimal" button. Your answer will appear in the Dec field. A sample of the color will appear in the Color Swatch area. The following is a list of definitions relating to conversions between RGB and Decimal.
What is an RGB color value (RGB)? An RGB color value is used in HTML or CSS to define a color on a web page. The RGB color value is a mix of three color components. R is red, G is Green, and B is blue. Each of the color components (R, G, and B) is a value between 0 and 255 in decimal.
The value is hexadecimal, while RGB would read 255, 0, 255 and the integer is a composite color representation. Since it is unclear what is being accomplished, here are all three variations: If you have an integer for the composite color, then most color endpoints will accept it unmodified. This would be something like setBackgroundColor (colorInt)
Hex to RGB conversion Get the 2 left digits of the hex color code and convert to decimal value to get the red color level. Get the 2 middle digits of the hex color code and convert to decimal value to get the green color level. Get the 2 right digits of the hex color code and convert to decimal value to get the blue color level.
What I found to be the simplest and best solution for me was to directly use the Color class as follows:
int red = Color.red(intColor); int green = Color.green(intColor); int blue = Color.blue(intColor); int alpha = Color.alpha(intColor);
This way I could already deal with the integer values without having to handle strings. If on the other hand the string representing the rgb color is what you need, Pankaj Kumar's answer is the best. I hope this is useful to someone.
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.
Update since API 26
Since API 26, new methods Color.valueOf(....)
has been introduced to convert colors for similar reason. you can use it like
// sRGB Color opaqueRed = Color.valueOf(0xffff0000); // from a color int Color translucentRed = Color.valueOf(1.0f, 0.0f, 0.0f, 0.5f); // Wide gamut color ColorSpace sRgb = ColorSpace.get(ColorSpace.Named.SRGB); @ColorLong long p3 = Color.pack(1.0f, 1.0f, 0.0f, 1.0f, sRgb); Color opaqueYellow = Color.valueOf(p3); // from a color long // CIE L*a*b* color space ColorSpace lab = ColorSpace.get(Named.CIE_LAB); Color green = Color.valueOf(100.0f, -128.0f, 128.0f, 1.0f, lab); mView.setBackgroundColor(opaqueRed.toArgb()); mView2.setBackgroundColor(green.toArgb()); mView3.setBackgroundColor(translucentRed.toArgb()); mView4.setBackgroundColor(opaqueYellow.toArgb());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With