Multimedia Programming with Pure Data
上QQ阅读APP看书,第一时间看更新

Working with digital color

We have seen how to specify the window background color and the world light color. For all the 2D and 3D graphics, we can also individually specify the color information with RGB values. The coming example will show us how to specify color for the primitives.

Create an empty patch and save it with name gem004.pd in your folder. Put the gemwin object, create and destroy messages, and the toggle box for rendering. In addition, put a gemhead object with a sphere object.

Between the gemhead and the sphere objects, insert a color object. For color specification, it will be a message of three numbers: red, green, and blue. Clicking on the message will change the color of the sphere:

We can also use another object colorRGB to specify color for the graphical shapes. It requires three (RGB) to four (RGB and alpha) number boxes to specify the color. Remember that each number ranges from 0 to 1. If you press the Shift key and click-and-drag a number box upward or downward, you can alter the number in decimal point, in a step of 0.01:

Color specification is straightforward in GEM. By using color or colorRGB, you alter the default color (white) according to the number values. You can also use both the world_light color and the object color together:

Some color input requires three individual numbers. Some color input requires a message list of three numbers. We can combine them together such that the user interface is consistent, that is, three number boxes:

The new object is pack. It simply packs three numbers together. Note the parameters f f f. Each f represents a floating-point number. The number of f corresponds to the number of inlets of the pack object. In this example, three numbers are packed together to form a list for the right-hand inlet of the color object. Nevertheless, this patch is not perfect. If you play around with the number boxes, you will notice that only the red color box (hot inlet) will send out the color list message. The rest (green, blue) will just store the new values and wait until there is a change in the red number box. If you remember how we handled the synchronization of hot and cold inlets, we make use of the trigger object:

For the green and blue color number boxes, they send out the number to a trigger object, trigger b f. Each trigger box will first send out the number to the corresponding number inlet of the pack object and then send out a bang message to the hot inlet of the pack object to output the color number list to the color object. In this case, changes in the green and blue numbers will also go immediately to the sphere object without waiting.