- Mastering openFrameworks:Creative Coding Demystified
- Denis Perevalov
- 812字
- 2021-08-06 16:54:17
Colors
Up until now we have worked with colors using the functions ofSetColor( r, g, b )
and ofBackground( r, g, b )
. By calling these functions, we specify the color of the current drawing and background as r
, g
, and b
values, corresponding to red, green, and blue components, where r
, g
and b
are integer values lying in the range 0
to 255
.
Tip
When you need to specify gray colors, you can use overloaded versions of these functions with just one argument: ofSetColor( gray )
and ofBackground( gray )
, where gray
is in the range 0
to 255
.
These functions are simple, but not enough. Sometimes, you need to pass the color as a single parameter in a function, and also do color modifications like changing the brightness. To solve this problem, openFrameworks has the class ofColor
. It lets us operate with colors as we do with single entities and modify these.
ofColor
is a class representing a color. It has four float fields: r
, g
, b
, and a
. Here r
, g
, and b
are red, green, and blue components of a color, and a
is the alpha component, which means the opacity of a color. The alpha component is related to transparency, which is discussed in detail in the Transparency section in Chapter 4, Images and Textures.
In this chapter we will not consider the alpha component. By default, its value is equal to 255
, which means truly opaque color, so all colors considered in this chapter are opaque.
Note
The ofSetColor()
, ofBackground()
, and ofColor()
functions include the alpha component as an optional last argument, so you can specify it when needed.
Operations with colors
To represent some color, just declare an object of the ofColor
class.
ofColor color;
To initialize the color, set its components.
color.r = 0.0; color.g = 128.0; color.b = 255.0;
Or, equivalently, use the constructor.
color = ofColor( 0.0, 128.0, 255.0 );
You can use color
as an argument in the functions ofSetColor()
and ofBackground()
. For example, ofSetColor( color )
and ofBackground( color )
.
openFrameworks has a number of predefined colors, including white, gray, black, red, green, blue, cyan, magenta, and yellow. See the full list of colors in the libs/openFrameworks/types/ofColors.h
file. To use the predefined colors, add the ofColor::
prefix before these names. For example, ofSetColor( ofColor::yellow )
sets the current drawing color to yellow.
You can modify the color using the following functions:
setHue( hue )
,setSaturation
( saturation )
, andsetBrightness( brightness )
: These functions change the hue, saturation, and brightness of the color to specified values. All the arguments are float values in the range0
to255
.setHsb( hue, saturation, brightness )
: This function creates a color by specifying its hue, saturation, and brightness values, where arguments are float values in the range0
to255
.getHue()
andgetSaturation()
: These functions return the hue and saturation values of the color.getBrightness()
: This function returns the brightest color component.getLightness()
: This function returns the average of the color components.invert()
: This function inverts color components; that is, ther
,g
, andb
fields of the color become255-r
,255-g
, and255-b
respectively.
Let's consider an example that demonstrates color modifications.
Color modifications example
In this example, we will modify the red color by changing its brightness, saturation, and hue through the whole range and draw three resultant stripes.
Note
This is example 02-2D/05-Colors
.
This example project is based on the openFrameworks emptyExample
project. Fill the body of the testApp::draw()
function with the following code:
ofBackground( 255, 255, 255 ); //Set white background //Changing brightness for ( int i=0; i<256; i++ ) { ofColor color = ofColor::red; //Get red color color.setBrightness( i ); //Modify brightness ofSetColor( color ); ofLine( i, 0, i, 50 ); } //Changing saturation for ( int i=0; i<256; i++ ) { ofColor color = ofColor::red; //Get red color color.setSaturation( i ); //Modify saturation ofSetColor( color ); ofLine( i, 80, i, 130 ); } //Changing hue for ( int i=0; i<256; i++ ) { ofColor color = ofColor::red; //Get red color color.setHue( i ); //Modify hue ofSetColor( color ); ofLine( i, 160, i, 210 ); }
Run the project and you will see three stripes consisting of the red color with changed brightness, saturation, and hue.

As you can see, changing brightness, saturation, and hue is similar to the color-corrections methods used in photo editors like Adobe Photoshop and Gimp. From a designer's point of view, this is a more powerful method for controlling colors as compared to directly specifying the red, green, and blue color components.
Tip
See an example of using the described color modification method end of the Defining the particle functions section in Chapter 3, Building a Simple Particle System.
Now we will consider how to perform drawings with uncleared background, which can be useful in many creative coding projects related to 2D graphics.
- 數據庫系統原理及MySQL應用教程(第2版)
- Qt 5 and OpenCV 4 Computer Vision Projects
- Learning Selenium Testing Tools with Python
- PyTorch自然語言處理入門與實戰
- Python進階編程:編寫更高效、優雅的Python代碼
- 云原生Spring實戰
- Scratch 3游戲與人工智能編程完全自學教程
- SAS數據統計分析與編程實踐
- MATLAB 2020從入門到精通
- Android項目實戰:手機安全衛士開發案例解析
- 速學Python:程序設計從入門到進階
- 深度學習原理與PyTorch實戰(第2版)
- Buildbox 2.x Game Development
- 從Excel到Python數據分析:Pandas、xlwings、openpyxl、Matplotlib的交互與應用
- STM8實戰