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 & HelpSyntax Questions › Convert String to Int for Color
Page Index Toggle Pages: 1
Convert String to Int for Color? (Read 907 times)
Convert String to Int for Color?
Mar 17th, 2010, 5:48am
 
I'm getting the hex String from an xml file. But I can't seem to convert the String to an Int for a color fill.

This is an example
Code:
String c = "#FF7733";
fill( int(c) );
rect(10,10,10,10);


it comes up black which is incorrect.
Any alternative will be appreciated.
Re: Convert String to Int for Color?
Reply #1 - Mar 17th, 2010, 5:56am
 
color() rather than int()

the # chokes int conversion, but color expects it.
Re: Convert String to Int for Color?
Reply #2 - Mar 17th, 2010, 6:45am
 
i tried that too but color() expects an int, there's the rub.
so
this does not work
Code:

String c = "#FF7733";
fill( color(c) );
rect(10,10,10,10);


nor this

Code:

String c = "#FF7733";
fill( color(int(c)) );
rect(10,10,10,10);

Re: Convert String to Int for Color?
Reply #3 - Mar 17th, 2010, 7:10am
 
Code:
String c = "#FF7733";
c = "FF" + c.substring(1);
fill(unhex(c));
rect(10, 10, 10, 10);

Add FF to have no alpha transparency.
Re: Convert String to Int for Color?
Reply #4 - Mar 17th, 2010, 7:35am
 
ahhhh, thanks!
the mysterious unhex().
Page Index Toggle Pages: 1