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 › dynamic html color values and typecasting
Page Index Toggle Pages: 1
dynamic html color values and typecasting (Read 737 times)
dynamic html color values and typecasting
Apr 21st, 2008, 8:56pm
 
hi

i'm loading an xml with - among other information - html color codes. I use the color code in the fill function but it always shows me black. When i retrieve the color code i typecast it to String and then later i want to convert this value to color but it gives me an error, and so i typecast it to integer which seems to ruin the color code since it's always black.

I must be missing something simple.. can anyone see what i'm missing?


Code:

import processing.xml.*;

XMLElement xml;

int largenumber = 500; //hack
Circle[] circles = new Circle[largenumber];
int numCircles;


void setup() {
size(600, 600);
xml = new XMLElement(this, "2008-04-20.xml");
int numCircles = xml.getChildCount();
println(numCircles);
for (int i = 0; i < numCircles; i++) {
XMLElement kid = xml.getChild(i);
XMLElement title_el = kid.getChild("title");
String title = title_el.getContent();

XMLElement word_el = kid.getChild("word");
String word = word_el.getContent();

XMLElement guid_el = kid.getChild("guid");
String guid = guid_el.getContent();

XMLElement color_el = kid.getChild("color");
String colorrgb = "#"+color_el.getContent();

println(title + " | " + word + " | " + colorrgb);

//circles[i] = new Circle(title, word, guid, "#CC6600"); DOES NOT WORK
circles[i] = new Circle(title, word, guid, colorrgb);


}
}





class Circle
{
float x, y;
String title, word, guid;
Circle(String title, String word, String guid, String colorrgb){

//color ccode = int(#CC6600); WORKS

color ccode = int(colorrgb); // DOES NOT WORK

fill(ccode, 100);
float x = random(50, 550);
float y = random(50, 550);
smooth();
strokeWeight(1);
stroke(204, 102, 100);
ellipse(x, y, 50, 50);

}


}
Re: dynamic html color values and typecasting
Reply #1 - Apr 21st, 2008, 10:41pm
 
Processing pre-processes #123456 at compile time into a colour, however at runtime that can't happen, so you'll have to convert it to a colour by hand with:

Code:
color c=Integer.parseInt("FFFF00",16);


(Note lack of #)
Re: dynamic html color values and typecasting
Reply #2 - Apr 22nd, 2008, 1:11am
 
Thank you Smiley but i can't get it to work.. the color code being printed is something like 13395456 but i want to preserve  the html color code itself eg CC6600 or find some other way of using the color codes..

Code:

import processing.xml.*;

XMLElement xml;

int largenumber = 500; //hack
Circle[] circles = new Circle[largenumber];
int numCircles;


void setup() {
size(600, 600);
xml = new XMLElement(this, "2008-04-20.xml");
int numCircles = xml.getChildCount();
println(numCircles);
for (int i = 0; i < numCircles; i++) {
XMLElement kid = xml.getChild(i);
XMLElement title_el = kid.getChild("title");
String title = title_el.getContent();

XMLElement word_el = kid.getChild("word");
String word = word_el.getContent();

XMLElement guid_el = kid.getChild("guid");
String guid = guid_el.getContent();

XMLElement color_el = kid.getChild("color");

//color c=Integer.parseInt(color_el.getContent(),16); //DOES NOT WORK
color c=Integer.parseInt("CC6600", 16); //DOES NOT WORK

println(title + " | " + word + " | " + c);

circles[i] = new Circle(title, word, guid, c);


}
}

class Circle
{
float x, y;
String title, word, guid;
Circle(String title, String word, String guid, color colorrgb){

fill(colorrgb, 70); //DOES NOT WORK

//color ccode = int(#CC6600); WORKS
//fill(ccode, 70);

float x = random(50, 550);
float y = random(50, 550);
smooth();
strokeWeight(1);
stroke(204, 102, 100);
ellipse(x, y, 50, 50);

}

}

Page Index Toggle Pages: 1