Random color selection from array of colors


#1

Hello - I’m trying to create an array of colors and then have shapes randomly be one of those colors but I cannot find the right syntax. I found two older posts about an array of colors but I can’t get one to work and the other seems to randomly choose R, G, and B from each color?

Any help appreciated.

I’m used to Processing - something like
colors[i] = Color[int(random(Color.length))];

for this so far in oF

I have tried:

in the header: ofColor *colors;

void ofApp::setup(){
colors = new ofColor[5];
//pink
colors[0].r = 234;
colors[0].g = 84;
colors[0].b = 223;
//purple
colors[1].r = 188;
colors[1].g = 75;
colors[1].b = 180;
//light acqua
colors[2].r = 36;
colors[2].g = 211;
colors[2].b = 211;
//deep acqua
colors[2].r = 26;
colors[2].g = 253;
colors[2].b = 146;
//navy
colors[2].r = 55;
colors[2].g = 87;
colors[2].b = 165;
}

void ofApp::draw(){
for(int i = 0; i < 10; i++)
{
ofSetColor(colors[i].r, colors[i].g, colors[i].b);
ofDrawRectangle(100 * i, 100, 100, 100);
}
}

included screenshot shows colors not in the array.

THANKS,
Rachel


#2

hey! a few bugs that might be tripping you up

a) you set colors[2] several times int the above code but you don’t set colors[3] or colors[4], etc.
b) you allocate an array of 5 elements

colors = new ofColor[5];

but in your for loop you draw ten elements

for(int i = 0; i < 10; i++)

hope that helps!

PS: it may be simpler to use a vector (which is similar to a growable array – in java arrayList). also ofColor has a set function so you can do:

colors[3].set(100,200,0);

finally ofColor has named colors, you may find it easier to write:

colors[3] = ofColor::aqua;

the named colors are based on w3c names colors

https://www.spycolor.com/w3c-colors


#3

Thanks Zach! I can’t believe I forgot to change the color numbers. Have done that and things are better.

If I had 5 colors defined but wanted 15 shapes - how would you suggest I approach having!the shapes be randomly chosen from that array of 5 colors?

As it is now, anything above 5 (because I’ve defined 5 colors) is randomly generated from I’m not sure where.

Thanks much!
Rachel


#4

if you do this:

for(int i = 0; i < 10; i++){
ofSetColor(colors[ ofRandom(0,5)]);
ofDrawRectangle(100 * i, 100, 100, 100);
}

it will flicker. there’s a few solutions – you can seed the random number generator:

ofSeedRandom(0);
for(int i = 0; i < 10; i++){
ofSetColor(colors[ (int)ofRandom(0,5)]);
ofDrawRectangle(100 * i, 100, 100, 100);
}

so you always choose the same random numbers… other options involve putting the colors in a vector and shuffling them or making an array of random ints that you set once in setup which refer to which color to draw.


#5

ps: if you call elements in the array beyond what you’ve allocated (ie, if you’ve allocated 5 but ask for element 10), you can get undefined results and your app can even crash :slight_smile: you are basically asking for memory beyond what you’ve asked for.


#6

THANKS ZACH! Very helpful.

I am trying the Vector approach. Brain melt :slight_smile: