Simple Random Color Generator

I know this has to be so easy but I’m just stuck. I’d be so thankful for any bit of help. The project is drawing a number of circles on screen based on the input from gui. That’s working fine, but I’d like each circle to have a different random color. I know, or thought I knew, how to generate random colors - but the circles won’t stay on that color! They keep cycling through all of them. I tried getting it to work with fbo but that didn’t seem right. What am I missing??? I’ve put my code below:

.H

void atomDraw();
void generateColor();

ofxPanel gui;
ofParameter<int> dots;

ofColor color;

.CPP

//--------------------------------------------------------------

void ofApp::draw( ){

gui.draw();

atomDraw();

}

//--------------------------------------------------------------

void ofApp::generateColor(){

color = ofColor( ofRandom(0, 255),
                ofRandom(0, 255),
                ofRandom(0, 255)
                );

ofSetColor(color);

}

void ofApp::atomDraw(){

 for ( int i = 0; i < dots; i++){
     
     generateColor();
     
    ofDrawCircle(ofGetWidth()/2 +60*((dots-1)*0.5-i), ofGetHeight()/2, 30);

 }

}

creates a vector and populate it in the setup:

vector<ofColor> colors;

void setup(){
    for(int i = 0; i < NUM_CIRCLE; i++)
         colors.push_back(ofRandom(0,255),ofRandom(0,255),ofRandom(0,255));
}

void draw(){
     for(int i = 0; i < NUM_CIRCLE; i++){
         ofSetColor(colors[i]);
         ofDrawCircle(...)
         ...
     }
}

When I try using the push_back in the for loop I receive an error that says "No matching member function for call to ‘push_back’? Any ideas?

There is no error when I use one ofRandom as a parameter like:

colors.push_back(ofRandom(0,255));

But then all of the other circles disappear. This is everything I have:

h

 ofxPanel gui;
ofParameter<int> NUM_ATOM;
vector<ofColor> colors;

int size;

.cpp

void ofApp::setup()

gui.setup();

gui.setName("Atom Builder v4");
gui.setDefaultTextPadding(10);
gui.setDefaultHeight(40);
gui.add(NUM_ATOM.set("Atoms",1, 0, 10));

size = 30;


for(int i =0; i < NUM_ATOM; i++){
    colors.push_back(ofRandom(0,255));
     }

void ofApp::draw()

gui.draw();

for(int i = 0; i < NUM_ATOM; i++){
    
ofSetColor(colors[i]);
    
ofDrawCircle(
ofGetWidth()/2-size*2*((NUM_ATOM-1)*0.5-i), ofGetHeight()*.5, size);

I almost got it! Moving the color loop to ::update solved the issue, I think it’s because setup only runs once? I’m still not sure why I can’t use more than one parameter for push_back though. Right now the circles are only black, white, or some shade of grey.

quickly it looks like this:

colors.push_back(ofRandom(0,255),ofRandom(0,255),ofRandom(0,255));

should be:

colors.push_back( ofColor(ofRandom(0,255),ofRandom(0,255),ofRandom(0,255)));

also, this is cheap hack, but sometimes I use ofSeedRandom(0) at the top of draw() which means you always get the same random results. It can be problematic if you need to use ofRandom() for anything else but I find it helpful if I just need to get a bunch of non changing random colors on the screen.

(random is not really random, and setting the seed to the same amount means each time you call ofRandom you get the same values in the same order, ie, the 100th time you call it will always give the same value the 100th time).

1 Like

you can also use:

colors.emplace_back( ofRandom(0,255),ofRandom(0,255),ofRandom(0,255));

to directly create the color passing the components

8 Likes

Ah! It works! Thanks for the advice - I’m still very new at this so all of this is going right in my notes :slight_smile:

that’s super useful – didn’t know about emplace_back…

sorry, I forgot to write ofColor in push_back :slight_smile:

yeah it’s really useful, it’s also faster than push_back since it creates the object directly in the vector memory and allows to put objects that can’t be copied in a vector

Hello all,

Silly question here: how can I generate random HSB colours?

THANKS.

Figured it out: color.setHsb(ofRandom(255),ofRandom(100,255), ofRandom(255));