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 › Problem with color hex values
Page Index Toggle Pages: 1
Problem with color hex values (Read 435 times)
Problem with color hex values
Jan 21st, 2010, 12:12am
 
Hi everybody,

So, I made this project that implements the twitter4j library. Basically, the sketch creates a window, displaying a background that's a solid color. It also scrapes Twitter, looking for tweets with a specific hashtag. It gets rid of the hashtag, and uses the rest of the text. The idea is that users send hex values of colors, and the sketch reads the tweet and changes the color value of background() based on that.

Here's the code I have so far:

Code:


Twitter myTwitter;
int clr;
String msg;
Runnable rn;
Thread thr1;

void setup() {
 size(320, 240);
 clr = #000000;
 myTwitter = new Twitter("", "");
 rn = new Runnable(){
   public void run() {
     while (true) {
       try {
         Query query = new Query("#colorwall");
         query.setRpp(1);
         QueryResult result = myTwitter.search(query);
         ArrayList tweets = (ArrayList) result.getTweets();
         Tweet t = (Tweet) tweets.get(0);
         String user = t.getFromUser();
         msg = t.getText();
         Date d = t.getCreatedAt();
         println("Tweet by " + user + " at " + d + ": " + msg);
         msg = msg.replaceAll("#colorwall","");
         println(msg);
         try{
           clr = Integer.valueOf(msg.replaceAll("#","").trim(),16).intValue();
           println(clr);
         }
         catch(Exception e){}
         }
       catch (TwitterException te) {
         println("Couldn't connect: " + te);
       }
     }
   }
 };
 thr1 = new Thread(rn);
 thr1.start();
}

void draw() {
 background(clr);
}


Now, this works pretty well for most colors. BUT, when I try use Twitter to set background() to #0000FF, it renders as white. If it's something close, like #0000FE, it also renders as white.

I tried hardcoding it in, like this:

Code:
void draw() {
 background(#0000FF);
}


... and it works fine. But, if I hardcode it like this:

Code:
void draw() {
 background(0x0000FF);
}


... then it turns white.

The solution is probably head-smackingly simple, but I'm just not seeing it. Any help would be greatly appreciated.

Thanks!
Re: Problem with color hex values
Reply #1 - Jan 21st, 2010, 12:51am
 
Color also has an alpha component.
To get blue, you have to use: 0xFF0000FF
Page Index Toggle Pages: 1