Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

processing hex string to color

I'm reading hex colors from a JSON file, but need to get them as an int so I can create a color.

int col=Integer.parseInt("FF0000",16); 

returns 16711680

int c = unhex("FF0000");

returns 16711680

how do I do this?

like image 710
Tessa Painter Avatar asked Feb 13 '26 09:02

Tessa Painter


2 Answers

If you want to reinvent the wheel, you could parse out the string into RGB hex values "FF", "00", and "00", convert the hex values to integers (255, 0, and 0 respectively) corresponding to int values from 0-255, and then create a Color object with those RGB values.

Personally though, I'd just use:

Color red = Color.decode("#FF0000"); //That's definitely red
like image 199
dr3wmurphy Avatar answered Feb 15 '26 23:02

dr3wmurphy


You're conflating two things:

Thing one: Hex values like #FF0000 represent integer values.

Thing two: Internally, Processing represents color values as integers.

The integers in the first concept are not the same thing as the integers in the second concept.

In fact, hex color values are a special case in Processing of the Processing editor doing some magic for you. I don't know of a way to go directly from a string value to a hex color value.

Instead, you should parse the String value into its individual components, convert them to integers, and then use the three-argument color() function to create a color.

See this question for more info: Hexadecimal to Integer in Java

like image 34
Kevin Workman Avatar answered Feb 15 '26 21:02

Kevin Workman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!