Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert hex color value ( #ffffff ) to integer value

I am receiving hex color values from a server (in this form, #xxxxxx , example #000000 for black)

How do I convert this to an integer value?

I tried doing Integer.valueOf("0x" + passedColor.substring(1, passedColor.length())) to get an even more hextastic 0x000000 result, but this isn't intepreted as an int here, any other suggestions?

I receive an error: 08-03 21:06:24.673: ERROR/AndroidRuntime(20231): java.lang.NumberFormatException: unable to parse '0x00C8FBFE' as integer

I am using the Android SDK for their setBackgroundColor(int color) function, which takes - as you might have guessed - an integer color value.

This is the OPPOSITE of this question: How to convert a color integer to a hex String in Android?

like image 818
CQM Avatar asked Aug 04 '11 01:08

CQM


People also ask

How do you convert HEX color 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.

Can I convert hex to Pantone?

HEX to PANTONE converter is an online tool to convert your HEX color codes to PANTONE PMS color format. All you have to do is select your color to generate the name in Pantone and match codes.


1 Answers

The real answer is to use:

Color.parseColor(myPassedColor) in Android, myPassedColor being the hex value like #000 or #000000 or #00000000.

However, this function does not support shorthand hex values such as #000.

like image 119
CQM Avatar answered Sep 18 '22 19:09

CQM