Coloring of Shapes

I would like to know if their is another more efficient way to color shapes. Currently I am drawing a black rectangle on the canvas using the following code:

ofSetColor(0, 0, 0);  
ofRect(0, 236, 1024, 30);  

However by doing this it is drawing the whole canvas black. I want to be able to define the colour of only the rectangle rather than everything. For example if I could assign the rectangle to a instance then I could say


Is this sort of method possible?
Any ideas. Thanks.

OF way of drawing follows OpenGL, so, for example, ofSetColor() will affect all the stuff that follows until you call the function again.

also ofRect() is just a drawing function: to do something like what you described you’ll have to make a rectangle object and define the needed functions.

also you’ll be able to do this in 007 using ofPath, you define a path and then assign a color to it like:

ofPath circle;  

but it’s not really efficient to do this for something so simple as a rectangle, it’s meant to be used when you have complex shapes and you want to store them instead of generate the vertexes once and again.

Thank you. How would I create a rectangle object. What do I need to declare it as? Cheers

you can just create a class that has a draw method and a setColor method then in the draw method it will set the color and draw a rectangle. You can use ofPushStyle and ofPopStyle to store the old color value and recover it after drawing the rectangle

Thank you. To put this into context, the problem I have is that I am loading an image onto the canvas:

if (snapCounter++) {  
		loader.loadImage(ofToString(loadCounter, 0)+".jpg");  
		//cout << loader.loadImage(ofToString(3, 0)+".jpg") << endl;  

I then want to overlay a rectangle on top of this image:

ofSetColor(0, 0, 0);  
ofRect(0, 236, 1024, 30);  

When the rectangle code is in place the image loads up on the screen for less than a second with the black rectangle on top, but then the screen immediately turns black again.

When I comment out the rectangle code the image loads and stays there. Why is the rectangle causing the canvas to turn black?

Thank you.

when you call:

ofSetColor(0, 0, 0);  

you are setting that color forever for everything that is after that. so probably you just need to call:


brefore drawing you image

Yeah thats done it. Thank you. I’m slowly learning.