androidGuy
androidGuy

Reputation: 5643

How to get RGB value from hexadecimal color code in java

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?

Upvotes: 17

Views: 44718

Answers (7)

Yoav
Yoav

Reputation: 157

int color = Color.parseColor("#519c3f");

int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);

Upvotes: 1

Abhimanyu Sharma
Abhimanyu Sharma

Reputation: 443

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();

Upvotes: 2

Kevin
Kevin

Reputation: 509

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

Upvotes: 1

Daniel Ryan
Daniel Ryan

Reputation: 7100

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();

Upvotes: 9

MByD
MByD

Reputation: 137442

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;

Upvotes: 70

Lalit Poptani
Lalit Poptani

Reputation: 67296

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

Upvotes: 7

Francisco Puga
Francisco Puga

Reputation: 25178

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();

Upvotes: 7

Related Questions