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 › Rollover based on color rather than position
Page Index Toggle Pages: 1
Rollover based on color rather than position (Read 300 times)
Rollover based on color rather than position
Oct 15th, 2008, 11:54pm
 
Hi

I have some code that looks up stock data from yahoo's site, and then creates a colored pie graph based on the fractional value of each stock versus the total.
I wanted to have a rollover to pop up specific info about the stock when the mouse rolls over a particular stocks 'wedge' of the pie.
Trying to find the precise location of the wedge in x,y coordinates seemed difficult, so I am trying to use the color of the wedge to identify which stock is being rolled over. The code below doesn't work, and I'm not clear why not....

Maybe this isn't even possible?
----------------------------------

import processing.opengl.*;

Record[] records;
int recordCount;
PFont body;
int startingEntry = 0;
int num;    
String alldata = "";
int total;
int networth = 0;
int dailych = 0;
String summary_list;
int diameter = 150;
float lastAng = 0;
color mycolor = 0;


void setup() {
 size(600,600,OPENGL);
 background(150);
 frameRate(30);
 body = loadFont("Tahoma-10.vlw");
 String[] mystocks = loadStrings("mystocks.tsv");
 num = mystocks.length;
 records = new Record[mystocks.length];
   for (int i = 1; i < mystocks.length; i++) {
     String[] pieces = split(mystocks[i], '\t');
     records[recordCount] = new Record(pieces);
     recordCount++;
   }
   for(int i=0;i<mystocks.length-1;i++) {
     networth += int(records[i].holdings);
     colormaker(i);
     dailych += int((records[i].dollar_daily_change));
     summary_list = "Today's net worth is:  $  "+ nfc(networth) + "\n\n" + "Today's change was:  $  " + nfc(dailych);
   }
}

void draw() {
 pushMatrix();
 drawpie();
 popMatrix();
}

void  colormaker(int index){
   float n1=random(255-5*index);
   float n2=random(255-5*index);
   float n3=random(255-5*index);
   mycolor = color(n1,n2,n3);
   records[index].rectcolor = mycolor;
}

void drawpie(){
   update(mouseX, mouseY);
    for (int i = 0; i < num-1; i++) {
     fill(records[i].rectcolor);
     textFont(body);
     arc(width/2, height/2, diameter, diameter, lastAng, lastAng+radians(records[i].holdings/networth*360));
     lastAng += radians(records[i].holdings/networth*360);
     rect(30,30 +i *12, 10,10);
     fill(255);
     text(records[i].symb, 55,40+i *12);
     text(records[i].dollar_daily_change, 100,40+i *12);
     }
}

void update(int x, int y){
 if(mousePressed == true)  {
   color cp = get(mouseX, mouseY);
   for (int i = 0; i < num-1; i++) {
    print(records[1].rectcolor+ records[1].symb +"\n" + cp);
   if(cp == records[i].rectcolor){
    print(records[1].symb);
   }
   }
 }
}

Thanks!

Charles Snyder


Re: Rollover based on color rather than position
Reply #1 - Oct 16th, 2008, 2:24am
 
Hm, does it work when you use P3D (even if it's ugly)? There are differences to how OpenGL does color mixing, so things might be just /slightly/ off.

You can also do println(hex(someColorVar)) to get the hex value for a color for comparison to see what's going on.
Re: Rollover based on color rather than position
Reply #2 - Oct 16th, 2008, 3:27am
 
P3D seems to work !

Thanks

cls
Re: Rollover based on color rather than position
Reply #3 - Oct 16th, 2008, 5:00pm
 
You might be interested by this thread too: cheking if mouse is inside an arc
Page Index Toggle Pages: 1