We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, I am new and I want my drawing to change color over time with smooth transitions (not random). I know this involves the HSB color mode but that is all I know. I am very new to this and I find this whole thing very confusing. If someone could take me through a step by step process that would really be helpful.
Here is what I have and I'd like the points to have a smooth color change as it continues to draw. (Basically if you google image search "rainbow gradient" – I want the fill of the points to cycle through all of those colors)
int number = 0;
void setup() {
size(1000,500);
background(20);
colorMode(HSB)
}
void draw() {
stroke(255);
fill(255);
strokeWeight(5);
translate(width/2,height/2);
point(x(t),y(t));
t++;
}
float x(float t) {
return sin(t * 2) * 100;
}
float y(float t) {
return cos(t / 10) * 100;
}
Answers
So what's changed? For one, I set the "maximum value" for HSB mode to 200. Then I modified the stroke color to depend on t (the variable that increases every frame). When t is 0 (at the start), you get a red color. As t increases, it turns to orange, then yellow, green, blue, purple, and back to red. When t is 200, I want to go back to using red again, so I use the mod function (%) (that is, x%y is the remainder of x/y). So when t is 201, I use the value of 1 again. And so on.
Use lerpColor(). It takes an amount amt between 0-1. For a simple color cycle, use the sketch clock as input. So if you want a color cycle that repeats every 5 seconds:
@jeremydouglass
where do I insert that?
@TfGuy44
thank you for the response! it worked but I am still unclear about what you mean by "mod function (%) (that is, x%y is the remainder of x/y)"
@hmgo, the Processing reference has an explanation of
%
function -- also called mod, or modulo -- here: % (modulo).So as t++ counts up, the answer to t%200 cycles between 0-199 over and over, as @TfGuy44 's example shows. This gives you a color hue between 0-199 -- or all the colors of the rainbow when the
colorMode(HSB,200)
command set the number of colors to 200.Re: a different approach with
lerpColor()
. This method lets you pick two specific colors to cycle between explicitly. In the sketch below based on @TfGuy44 's solution colors are computed based on the clock rather than how often drawing happens. So if you changed the frameRate(), @TfGuy44 's sketch would change color faster or slower -- this one will draw points more slowly, but they will change color at the same speed. Which kind of color mixing and which kind of timing you want to use depends on what you are trying to do.