17

I have a decimal color code (eg: 4898901). I am converting it into a hexadecimal equivalent of that as 4ac055. How to get the red, green and blue component value from the hexadecimal color code?

jrudolph
  • 8,167
  • 4
  • 31
  • 50
androidGuy
  • 5,747
  • 11
  • 35
  • 55

7 Answers7

69

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;
MByD
  • 133,244
  • 25
  • 260
  • 270
  • I used this code in my app and when run that on android 2.3 I get excaption for myColorString: "unable to parse '4282291905' as integer". Could you tell why? – NrNazifi Feb 23 '13 at 12:07
  • @ProSoft - the code above will not fit for you, as the number 4282291905 overflows integer boundaries. You can use Long instead (I will fix the answer) – MByD Feb 23 '13 at 12:43
9

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();
Daniel Ryan
  • 6,772
  • 5
  • 42
  • 60
  • 3
    I cannot find this class in `Android` - `android.graphics.Color` holds no such methods. – PPartisan Sep 30 '15 at 08:22
  • It's been a while since I played around with this code. I believe this was using the standard Java Color class. http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html – Daniel Ryan Sep 30 '15 at 21:05
7

Try this,

colorStr e.g. "#FFFFFF"

public static Color hex2Rgb(String colorStr) {
    return new Color(
            Integer.valueOf( colorStr.substring( 1, 3 ), 16 ),
            Integer.valueOf( colorStr.substring( 3, 5 ), 16 ),
            Integer.valueOf( colorStr.substring( 5, 7 ), 16 ) );
}

For using Color class you have to use java-rt-jar-stubs-1.5.0.jar as Color class is from java.awt.Color

Lalit Poptani
  • 66,608
  • 21
  • 157
  • 241
7

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();
Francisco Puga
  • 22,373
  • 4
  • 48
  • 60
2
String hex1 = "#FF00FF00";    //BLUE with Alpha value = #AARRGGBB

int a = Integer.valueOf( hex1.substring( 1, 3 ), 16 );
int r = Integer.valueOf( hex1.substring( 3, 5 ), 16 );
int g = Integer.valueOf( hex1.substring( 5, 7 ), 16 );
int b = Integer.parseInt( hex1.substring( 7, 9 ), 16 );

Toast.makeText(getApplicationContext(), "ARGB: " + a + " , " + r + " ,  "+ g + " , "+ b , Toast.LENGTH_SHORT).show();

String hex1 = "#FF0000";    //RED with NO Alpha = #RRGGBB

int r = Integer.valueOf( hex1.substring( 1, 3 ), 16 );
int g = Integer.valueOf( hex1.substring( 3, 5 ), 16 );
int b = Integer.parseInt( hex1.substring( 5, 7 ), 16 );

Toast.makeText(getApplicationContext(), "RGB: " + r + " ,  "+ g + " , "+ b , Toast.LENGTH_SHORT).show();
1
int color = Color.parseColor("#519c3f");

int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
Yoav
  • 152
  • 3
  • 9
1

When you have the hex-code : 4ac055. The first two letters are the color red. The next two are green and the two latest letters are for the color blue. So When you have the hex-code of the color red you must convert it to dez back. In these example where red 4a = 74. Green c0 = 192 and blue = 85..

Try to make a function which split the hexcode and then give back the rgb code

Dhanuka
  • 2,776
  • 5
  • 26
  • 37
Kevin
  • 359
  • 2
  • 3
  • 15