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 › Background Colour Morph
Page Index Toggle Pages: 1
Background Colour Morph (Read 437 times)
Background Colour Morph
Apr 30th, 2008, 8:40pm
 
Hi,
I've got an idea for the background colour to automatically morph between a set of pre-defined colours.

But to also respond to user input, whereby they press the number 4 on their keyboard and this chooses the colour that the background colour should morph to. The trouble is i'm using hex values (as i'm used to that for web design), so this might be doing something to the values?!

I've already got a simple class set up where i'm able to change background colour when a user selects a number between 1 - 9 on the keyboard. But it's instant - there's no gradual change. I've had a good search around the site and forums for anything similar.

The colours are set to the 'color' variables where presumably it creates a number, but how can i compare two values and move between them. Printing these results in odd numbers like '-3498761'. Experiments in standard 'less than' or 'more than' if structures didn't seem to do the trick for me.

Is this a stupid idea?
Re: Background Colour Morph
Reply #1 - Apr 30th, 2008, 9:11pm
 
I think http://processing.org/reference/lerpColor_.html should make your life a lot easier.
Re: Background Colour Morph
Reply #2 - Apr 30th, 2008, 11:20pm
 
Ah... good find, thanks. I think I read in the Processing book about lerp(), but didn't know you could do this for colours.
Re: Background Colour Morph
Reply #3 - May 3rd, 2008, 5:32pm
 
The weird thing is that the float value used in lerpColour to morph between colours should be between 0.0 and 1.0, but it seems to be between 0.0 and 0.1?!

Code:

color bgColour = color (0,0,0);
color nxtColour = color (255,255,255);
float morph = 0.0;
float speed = 0.001;

void setup() {
size(300,300);
}

void draw() {
background(bgColour);

if(bgColour != nxtColour) {
if(morph < 1.0) {
println("Inequality!");
bgColour = lerpColor(bgColour, nxtColour, morph);
morph += speed;
println(morph);
}
}
}

void keyPressed() {
if(key == 'a' || key == 'A') {
nxtColour = color( random(0,255), random(0,255), random(0,255) );
morph = 0.0;
}
}
Page Index Toggle Pages: 1