官术网_书友最值得收藏!

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 ), and setBrightness( brightness ): These functions change the hue, saturation, and brightness of the color to specified values. All the arguments are float values in the range 0 to 255.
  • setHsb( hue, saturation, brightness ): This function creates a color by specifying its hue, saturation, and brightness values, where arguments are float values in the range 0 to 255.
  • getHue() and getSaturation(): 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, the r, g, and b fields of the color become 255-r, 255-g, and 255-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.

主站蜘蛛池模板: 类乌齐县| 杂多县| 晋州市| 托里县| 南丰县| 德保县| 马关县| 塔城市| 承德县| 云林县| 论坛| 德格县| 贞丰县| 安康市| 鄯善县| 天台县| 扎赉特旗| 彰武县| 五常市| 石城县| 万盛区| 当涂县| 扎赉特旗| 离岛区| 涟源市| 南京市| 洞口县| 东辽县| 海口市| 北安市| 荥经县| 赣榆县| 永仁县| 滦平县| 喜德县| 新河县| 临桂县| 苗栗县| 绥化市| 福清市| 盈江县|