# Not sure how to slow down the randomizing color

I have this snip it from my code below, and I would like to slow down the rate at which the color randomly changes. I know that is the point of random, but I will be presenting this in class and anyone who has photosensitive epilepsy may get a seizure due to the quickly changing colors so I want to slow it down. This is the part that changes the color so I only posted only those but if I should post more code please let me know. This code is for a shape I have in the middle of my animation that changes color. I have tried looking up random and saw that I can put random(high) and random(low) but that didn’t work. Any suggestions? Thanks.

`````` void show() {

float temp= random(0, 100);

//if the speed of the stars is less than 50 then we display random colors for the stars with no outline which is the noStroke
if (temp > 10 )

fill(random(0,255), random(0,255), random(0,255));
else

fill(random(0,255), random(0,255), random(0,255));
noStroke();

// map will give new star positions
// the division x / z get a number between 0 and a very high number,
// map this number (proportionally to a range of 0 - 1), inside a range of 0 - width/2.
//this makes sure the new coordinates "sx" and "sy" move faster at each frame
// and which they finish their travel outside of the canvas (they finish when "z" is less than a).

float sx = map(x / z, 0, 1, 0, width/2);
float sy = map(y / z, 0, 1, 0, height/2);

//the z value to increase the star size between a range from 0 to 16.
float r = map(z, 0, width/2, 16, 0);
ellipse(sx, sy, r, r);

//use the "pz" valute to get the previous position of the stars,
//to draw a line from the previous position to the new (current) one.
float px = map(x / pz, 0, 1, 0, width/2);
float py = map(y / pz, 0, 1, 0, height/2);

pz = z;

stroke(255);
line(px, py, sx, sy);

}
}
``````
1 Like

Hi, this is my attempt, what i’m doing is using a timer to change the color every often.

``````long previousTime = 0;
long currentTime = 0;
boolean changeColor = false;

void setup() {
size (600, 600);
background(255);
}

void draw() {
background(0);
currentTime = millis();
if (currentTime - previousTime > 1000) {
changeColor = true;
}

if (changeColor) {
fill(random(255), random(255), random(255));
changeColor = false;
previousTime = currentTime;
}

rect(100, 100, width/2, height/2);
}

``````
1 Like

Thanks! I tried entering the code you suggested by but the colors are still changing fast, not sure what I’m doing wrong, here is my code:

``````void draw(){
currentTime = millis();
if (currentTime - previousTime > 1000) {
changeColor = true;
}
if (changeColor) {
fill(random(255), random(255), random(255));
changeColor = false;
previousTime = currentTime;
}
//link the value of the speed variable to the mouse position
{
speed = map(mouseX, 0, width, 0, 20);
background(0);

//moving the center of the screen from the top left corner to the center of the canvas
translate(width/2, height/2);

//draw each star, running the updated method to its position and show method to show it on the canvas
for (int i = 0; i < stars.length; i++) {
stars[i].update();
stars[i].show();

}
``````

Probably you’re calling the function fill again, right before you draw your figures. i don’t know if in the sections

Thats code that I have for stars in the background of my animation so I can’t take that out. Any other way to slow it down? I know there is random(high) and random(low) and I tried that but it didnt work.

All your stars are the same color, right? So let’s make this super simple and have one color variable that stores the current color of the stars.

``````color current_star_color = color(random(255),random(255),random(255));
``````

Now, when you go to draw a star, you will now use this color:

``````fill( current_star_color ); // Set fill color immediately before drawing the star.
ellipse( sx, sy, r, r ); // Draw the star.
``````

Still with me?

Finally, you need to store a new color in the current star’s color every so often:

`````` currentTime = millis();
if (currentTime - previousTime > 1000) {
current_star_color = color(random(255),random(255),random(255)); // <-- LOOK!
previousTime = currentTime;
}
``````

Notice that this is not setting the fill color any more. It’s storing a new value into the color variable that you are using for the stars. One that you are sure you will use for the stars because you set that color as the fill color immediately before drawing them.

2 Likes