We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpIntegration › Eclipse, Color, and Hex Codes
Page Index Toggle Pages: 1
Eclipse, Color, and Hex Codes (Read 1341 times)
Eclipse, Color, and Hex Codes
Feb 28th, 2009, 4:30am
 
I've read the note about how colours in Eclipse are handled differently than in the Processing IED, but I just can't work this out. I'm trying to take a Hex code as a string and use it for a color, but I can't get it to work as it should.

This works as it should:
Code:



int colo = parent.color(0xFF119900);


parent.fill(colo);



But I'm starting from a string. So I tried this code:

Code:



String st = "0xFF119900";


int colo = parent.color(Integer.parseInt(st,16));


parent.fill(colo);


But it produces this error:
Exception in thread "Animation Thread" java.lang.NumberFormatException: For input string: "0xFF119900"

I also tried removing '0x' from the string:
Code:


String st = "FF119900";


int colo = parent.color(Integer.parseInt(st,16));


parent.fill(colo);

It produces a similar error.

However, when I change the string as below, it works:

Code:

String st = "79119900";


int colo = parent.color(Integer.parseInt(st,16));


parent.fill(colo);


...except that it's semi-transparent. If the first two characters are 79 or lower, the code works except for the transparency, but if the characters are 80 or higher, I get the exception error.

I've also tried it using parent.unhex() rather than Integer.parseInt(), but the result is the same.

Any solutions, suggestions would be greatly appreciated!


Re: Eclipse, Color, and Hex Codes
Reply #1 - Feb 28th, 2009, 9:23am
 
Ah, it is unexpected.
I see in the doc. that parseInt wants a signed integer, ie. accepting a - sign, so it expects the number to be between -maxPositiveIntInThisRadix and +maxPositiveIntInThisRadix (roughly).

If all your colors are opaque, the solution isn't too complex:
Code:
String st = "0xFF119900";
String cstr = st.substring(4);
println(cstr);
int colo = Integer.parseInt(cstr, 16) | 0xFF000000;
fill(colo); rect(10, 10, 10, 10);

Re: Eclipse, Color, and Hex Codes
Reply #2 - Feb 28th, 2009, 8:18pm
 
Ah, that works perfectly and makes sense now, but I never would have figured that out on my own. Many thanks!
Page Index Toggle Pages: 1