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 & HelpPrograms › Math Relation: defining a color
Page Index Toggle Pages: 1
Math Relation: defining a color (Read 1529 times)
Math Relation: defining a color
Mar 22nd, 2010, 11:09am
 
Hallo,

before I ask my question, I shell say that my math knowledge and subsequently my logical thinking is very poor.

I have a sphere. this sphere spins with a certain speed. When the speed gets higher, the sphere should "get hotter" and therefor more red. When slowing down, it "cools down" and gets more blueish.

so how do I relate the color with speed?

green = speed*pointSize/255); // smaller points get darker
red = green + abs(sin(speed)*255/pointSize;
blue = green - abs(sin(speed)*255/pointSize;

I have no idea how to solve this problem mathematically.  Huh  Embarrassed

Thanks for any help!  
Re: Math Relation: defining a color
Reply #1 - Mar 22nd, 2010, 12:35pm
 
Assuming the speed is somewhat bounded, you may want to consider the use of the lerpColor function (http://processing.org/reference/lerpColor_.html). See a possible implementation in the following example on OpenProcessing http://www.openprocessing.org/visuals/?visualID=4869 which you may want to adapt to your use case.
Re: Math Relation: defining a color
Reply #2 - Mar 23rd, 2010, 8:36am
 
I found it very useful to change the colormode from RGB to HSB (Hue Saturation Brightness) and then only vary the H value in the interval red-yellow.
Re: Math Relation: defining a color
Reply #3 - Mar 23rd, 2010, 12:55pm
 
and how can I control the Hue value for Red+Yellow only? And this in relation to the speed?

thx!
Re: Math Relation: defining a color
Reply #4 - Mar 23rd, 2010, 1:40pm
 
it doesnt work like that.
there is no hue value for red+yellow or any other color.
hue is the color.
so when defining a color that should be between red and yellow use hue values between 0 and 60

this only works because red and yellow are next to each other, to get frmo red to green without yellow for example, you should use lerpcolor again.

so in your case you can use,

color(map(speed,0,maxSpeed,0,60),100,100);
when in HSB Mode.
Re: Math Relation: defining a color
Reply #5 - Mar 27th, 2010, 3:59am
 
Hi Cedric,

em, when I use
Code:
color(map(speed,0,maxSpeed,0,60),100,100); 



I only get a greish color, that doesnt change at all.
my speed parameter is diffrent for every sphere and it varies between 0.001 and 0.01;  max speed is then 0.01

where could I be wrong?


This lerpColor function sound very promising because of the percentage.
I used my speed variable (0.001, 0.01)  to get a percantage number between 1, 100 ==> colorPercent = sin(speed*100)*100;

fill(lerpColor(cblue, cred, colorPercent, HSB));

it appears not to work, because the color still dont match the speed Sad


--------------------------------

I^ve just tried something else. Create a colorShift variable from the speed(0.001,0.01)  ---> sin(speed*100); so it should give a number between 0 and 1

this I then use to determine how much of red or blue is to be taken.

fill(255*colorShifter, 0, 255-(255*colorShifter));

makes sense to me, but still no effect Sad

Ich bin tiefst verzweifelt.

Re: Math Relation: defining a color
Reply #6 - Mar 27th, 2010, 5:50am
 
kein grund zu verzweifeln. can you post a part of your code, i will make it work Smiley
Re: Math Relation: defining a color
Reply #7 - Mar 27th, 2010, 5:57am
 
Smiley

give me a second to tidy it up a bit.

thank you in advance!
Re: Math Relation: defining a color
Reply #8 - Mar 27th, 2010, 6:16am
 
Thats just a selection of the application that I use to test the functions on.

Now I`m struggling with the color-change and the rotation in a determined number of cycles, in which the speed decreases/increases to min/max speed.

Code:
float rotation;
float speed, speedMin, speedMax,speedAvrg;
int cycles;
float radius;
int wRings, hRings;
float angle;

void setup()
{
size(400,400);
colorMode(RGB);
background(0);

radius = height/2;
wRings = 30;
hRings = 30;

cycles = 10;
speedMin = 0;
speedMax = 0.1;
speedAvrg = 0.05;

speed = 0.001;

}


void draw()
{
noStroke();
fill(0,40);
rect(0,0,width,height);
drawSphere();
rotateSphere();
}


void drawSphere()
{
float posx = mouseX;
float posy = mouseY;

for(int i=1; i<wRings; i++)
{
float phase = i*1./wRings;
float ringCenterPosX = posx;
float ringCenterPosY = posy + (sin(phase*PI+HALF_PI)*.5+.5)*radius-height/4;
float ringSizeX = sin(phase*PI)*radius/2;
float ringSizeY = ringSizeX / 4;

drawRing(ringCenterPosX, ringCenterPosY, ringSizeX, ringSizeY);
}

}

void drawRing(float posx, float posy, float sizex, float sizey)
{

for(int i=0; i<hRings;i++)
{
float phase = i*.4/hRings - rotation;

// color red = #ff0000;
// color blue = #0000ff;
// float colorPercent = sin(speed*100)*100;
// fill(lerpColor(blue, red, colorPercent, HSB));

// c = color(abs(sin((speed))*255)/pointSize, 255-abs(cos((speed))*255)/pointSize, 255.0/(speed));
// color c = color(map(speed,0,speedMax,0,60),0,0);

float colorShifter = sin(speed*100);
float cRed = 255*colorShifter;
float cBlue = 255-255*colorShifter;
// println("RED: " + cRed + "BLUE: " + cBlue);
fill(cRed,255,cBlue);

float pointSize = abs(sin(phase+rotation)*TWO_PI);
ellipse(posx + sizex*cos(phase*TWO_PI), posy + sizey*sin(phase*TWO_PI),pointSize,pointSize);
}
rotation += speed;
}


void rotateSphere(float speed, int cycles)
{
for(int i=0;i<cycles+1;i++)
{
float difference = speed*i/cycles; // DECREASING SPEED to SPEEDMIN
//float difference = (speedMax-speed)*i/cycles; //INCREASING SPEED TO SPEEDMAX

for(int j=0; j<360; j++)
{

speed -= difference/360;
rotation -=speed;
println("CYCLE: " + i + "| GRAD: " + j + " @ SPEED: " + speed);
}
}
}

float count = 0;

void rotateSphere()
{
for(int j=0;j<10;j++){

for(int i=0; i<360; i++)
{
count = (count%1)+0.1;
float phase = sin(count)*TWO_PI;
angle = (angle+degrees(phase));
}
}
}


It`s messy, but thats my  'draft' where I can experiment, without ruining my main app.

Ich hoff du kannst da durchblicken  Undecided
Re: Math Relation: defining a color
Reply #9 - Mar 27th, 2010, 6:21am
 
the speed actually doesnt change in this one.
how do you want the speed to change ?

edit: ahh rotation is the actual speed
Re: Math Relation: defining a color
Reply #10 - Mar 27th, 2010, 6:30am
 
well I`ve been trying to make a function, that spins the sphere a certain number of times (n*360) and while spinning loose or gain speed.

void rotateSphere(float speed, int cycles)
{
 for(int i=0;i<cycles+1;i++)
 {
   float difference = speed*i/cycles;  // DECREASING SPEED to SPEEDMIN
   //float difference = (speedMax-speed)*i/cycles;  //INCREASING SPEED TO SPEEDMAX

   for(int j=0; j<360; j++)
   {

     speed -= difference/360;
     rotation -=speed;
     println("CYCLE: " + i + "| GRAD: " + j + " @ SPEED: " + speed);    
   }
 }
}

If the speed, at which the sphere is 'born' is under the average speed, it gets accelerated to speedMax; and vice verca.  When accelerating, the temperature gets higher, hence the color > red. And vice verca.

In this code there is no function that creates a sphere using the constructor. I just adjust the numbers manually to see what happens whit the color and so on.

The code for my main app is to long to post in here...
Re: Math Relation: defining a color
Reply #11 - Mar 27th, 2010, 6:39am
 
maybe we should just use sin() to make the sphere speed up and slow down.
i will make an example. give me some sec
Re: Math Relation: defining a color
Reply #12 - Mar 27th, 2010, 6:58am
 
i like it Smiley
some color change is somehow tricky, cause lerpcolor gives you just a two color gradient, and the HSB idea only works going from red,yellow,green to blue...

if you want a gradient from blue to red, orange, yellow, white maybe. you have to think about different ways.

but hope that helps.

Code:
float rotation;
float speed;

float radius;
int wRings, hRings;
float angle;

void setup()
{
size(400,400);
colorMode(HSB,360,100,100);
background(0);

radius = height/2;
wRings = 30;
hRings = 30;

}


void draw()
{
noStroke();
fill(0,40);
rect(0,0,width,height);
drawSphere();
rotateSphere();

}


void drawSphere()
{
float posx = mouseX;
float posy = mouseY;

for(int i=1; i<wRings; i++)
{
float phase = i*1./wRings;
float ringCenterPosX = posx;
float ringCenterPosY = posy + (sin(phase*PI+HALF_PI)*.5+.5)*radius-height/4;
float ringSizeX = sin(phase*PI)*radius/2;
float ringSizeY = ringSizeX / 4;

drawRing(ringCenterPosX, ringCenterPosY, ringSizeX, ringSizeY);
}

}

void drawRing(float posx, float posy, float sizex, float sizey)
{

for(int i=0; i<hRings;i++)
{
float phase = i*.4/hRings - rotation;

fill(180-(180*speed*100),100,100);

float pointSize = abs(sin(phase+rotation)*TWO_PI);
ellipse(posx + sizex*cos(phase*TWO_PI), posy + sizey*sin(phase*TWO_PI),pointSize,pointSize);
}
rotation -= speed;

speed=map(sin(frameCount/100.0),-1,1,0,0.01);

}


float count = 0;

void rotateSphere()
{
for(int j=0;j<10;j++){

for(int i=0; i<360; i++)
{
count = (count%1)+0.1;
float phase = sin(count)*TWO_PI;
angle = (angle+degrees(phase));
}
}
}



Re: Math Relation: defining a color
Reply #13 - Mar 27th, 2010, 7:07am
 
Uau!

thank you cedric ! I`ll try now to map this code to my main app and I`ll let you know what the results are Smiley


herzlichen dank!

Re: Math Relation: defining a color
Reply #14 - Mar 27th, 2010, 9:58am
 
if you ad this line to your drawRing function

   hRings = (int) map(speed,0,0.01,2,40);

it looks more realistic as the trail get shorter when slowing down.
Not perfect but you get the idea.
Page Index Toggle Pages: 1