2

my hex color string : #ffffff

i want simple way to convert string #rrggbb to int r; int g; int b;

int color = (int)Long.parseLong(myHexColor, 16);
int r = (color >> 16) & 0xFF;
int g = (color >> 8) & 0xFF;
int b = (color >> 0) & 0xFF;

this method is true?

thanks.

Edit:______________________________

String colorStr = "#ffffff";
int  r=  Integer.valueOf( colorStr.substring( 1, 3 ), 16 );
int  g=  Integer.valueOf( colorStr.substring( 3, 5 ), 16 );
int  b=  Integer.valueOf( colorStr.substring( 5, 7 ), 16 );
Farzad
  • 1,975
  • 1
  • 25
  • 47
  • possible duplicate of [How to convert hex to rgb using Java?](http://stackoverflow.com/questions/4129666/how-to-convert-hex-to-rgb-using-java) – Ormoz Jul 05 '15 at 04:06
  • @Ormoz i want Separately int r / int g / int b – Farzad Jul 05 '15 at 04:12

1 Answers1

3

You can try:

    int  r=  Integer.valueOf( colorStr.substring( 1, 3 ), 16 );
    int  g=  Integer.valueOf( colorStr.substring( 3, 5 ), 16 );
    int  b=  Integer.valueOf( colorStr.substring( 5, 7 ), 16 );
Ormoz
  • 2,975
  • 10
  • 35
  • 50