Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 http://www.shodor.org/stella2java/rgbint.html I would like to archive same thing in Java, on Android.

like image 701
Marek Avatar asked Jun 19 '13 05:06

Marek


People also ask

How do you convert numbers to RGB?

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.

What does a RGB value of 0 0 255 mean?

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.

What is int color?

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.

How do I convert RGB colors to decimal colors?

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 value in HTML?

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.

What is the difference between RGB and integer color values?

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)

How do you convert hex to RGB in Python?

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.


2 Answers

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.

like image 108
Francesco D.M. Avatar answered Sep 22 '22 01:09

Francesco D.M.


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()); 
like image 28
Pankaj Kumar Avatar answered Sep 21 '22 01:09

Pankaj Kumar