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.
IndexDiscussionExhibition › random color
Page Index Toggle Pages: 1
random color (Read 5672 times)
random color
Jul 24th, 2008, 11:46am
 
hi all,
I'm new to processng and I would like to change the background of bouncybubbles from black to a sort of continuosly changing color and also the bubbles in various colors
I tried random() to change the color but it makes a psychedelic effect that is not so nice.
thanks
Re: random color
Reply #1 - Jul 24th, 2008, 12:08pm
 
You are looking for lerpColor():

http://processing.org/reference/lerpColor_.html
Re: random color
Reply #2 - Jul 24th, 2008, 6:28pm
 
Here are 2 solutions.

The first uses lerpColor():

color start=color(0,0,0);
color finish;
float amt = 0.0;

void setup() {
 size(400,400);
 background(start);
 finish = color(random(255),random(255),random(255));
}

void draw() {
 amt+=.01;
 if (amt >= 1) {
   amt = 0.0;
   start = finish;
   finish = color(random(255),random(255),random(255));
 }
 background(lerpColor(start,finish,amt));
}

And here is a function I came up with to colorcycle before I heard of lerpColor():

color Color;
int r_go,g_go,b_go;

void setup(){  
 size(400, 400);  
 background(0);  
 r_go = int(random(255));
 g_go = int(random(255));
 b_go = int(random(255));
 Color = color(int(random(255)),int(random(255)),int(random(255)));
}  

void draw(){
 Color = colorCycler(Color);
 background(Color);
}

color colorCycler(color Color) {
 int r = int(red(Color));
 int g = int(green(Color));
 int b = int(blue(Color));
 if (r == r_go)
   r_go = int(random(255));
 if (g == g_go)
   g_go = int(random(255));
 if (b == b_go)
   b_go = int(random(255));
 if (r < r_go)
   r++;
 if (g < g_go)
   g++;
 if (b < b_go)
   b++;
 if (r > r_go)
   r--;
 if (g > g_go)
   g--;
 if (b > b_go)
   b--;
 Color = color(r,g,b);
 return(Color);
}

They both produce a similar effect, but slightly different. The first takes the same amount of steps to fade between very similar colors and very different.  The second method might look more smooth because R,G and B are all constantly changing. Hope this helps!
Re: random color
Reply #3 - Jul 26th, 2008, 9:24am
 
thanks, I needed a background changing with somehow random colors so I solved the problem with this code:
float R = 255;
float G = 255;
float B = 0;
float incremento = +1;
float mincol = 0, maxcol = 255;

void setup() {
 size(200,150);
}
void draw()
{
 background(R,G,B);
 R += incremento;
 if (R > maxcol)
 incremento = -.5;
 else if (R < mincol)
 incremento = random(2);
   B += incremento;
 if (B > maxcol)
 incremento = -1;
 else if (B < mincol)
 incremento = +5;
   G += incremento;
 if (G > maxcol)
 incremento = -1;
 else if (G < mincol)
 incremento = +10;
 frameRate(150);
}
Re: random color
Reply #4 - Jul 26th, 2008, 9:33pm
 
If you're doing a lot of color changing I find the HSB mode better than the rgb mode.  Also nice is being able to set the range of the color mode to arbitrary values.  

Try colorMode(HSB, 3000);

for example.  http://processing.org/reference/colorMode_.html
Re: random color
Reply #5 - Jul 31st, 2008, 3:20pm
 
and if I want to change only beetween 3 different colors?
for example from orange to yellow to violet lerpColor is only for 2 colors.
Page Index Toggle Pages: 1