Setting limit of random colours


#1

So I have written that, when drawing a shape, the stroke and fill are random colours:

stroke(random(255),random(255),random(255));
fill(random(255),random(255),random(255));

So if I wanted to set the colours to be random between certain parameters, for example, keep it brighter than, say, 150, but not brighter than 220. How would I write that?


#2

You can use 2 parameters in the random(); functions, so random(150, 220);;


#3

thank you I didn’t realize it was that easy


#4

Even if you couldn’t supply 2 parameters, you could do something like this:

var minimum = 150;
var maximum = 220;
var value = minimum + random(maximum - minimum);

#5

A quick way to figure these things out is to use the references, it is a really good source.

For your example, you can search (ctrl + f) for random, and you will find a lot of usefull stuff.


#6

Another option is to choose a random point on a color spectrum using lerpColor() and passing it two colors and a random value between 0-1.

https://processing.org/reference/lerpColor_.html

color from = color(204, 102, 0);
color to = color(0, 102, 153);
void setup() {
  frameRate(1);
}
void draw() {
  // random background in color range
  color randc = lerpColor(from, to, random(1));
  background (randc);
  // draw color bar
  for(int i=0; i<10; i++){
    fill(lerpColor(from, to, i/10.0));
    rect(0,0,10,10);
    translate(10,0);
  }
}

39%20PM


Hue with specific colors