How to get RGB value from hexadecimal color code i

2020-01-31 03:33发布

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?

7条回答
爷、活的狠高调
2楼-- · 2020-01-31 03:41
int color = Color.parseColor("#519c3f");

int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
查看更多
The star\"
3楼-- · 2020-01-31 03:41

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();
查看更多
冷血范
4楼-- · 2020-01-31 03:43

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();
查看更多
劫难
5楼-- · 2020-01-31 03:49

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;
查看更多
爷的心禁止访问
6楼-- · 2020-01-31 03:57
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();
查看更多
beautiful°
7楼-- · 2020-01-31 04:01

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

查看更多
登录 后发表回答