Adding Color

In recent years, color printers have become more common... to the point where it is almost impossible to buy a black-and-white printer. Fortunately, PostScript handles color documents quite easily, and you do not have to learn much to add color to your documents.

There are many, many ways to specify color. The science of color reproduction and perception is very complex and encompasses physics, chemistry, physiology, and psychology. PostScript provides a great deal of flexibility on this front, providing several different methods for specifying color to let you get as close as possible to the color you want specifying it in the way that is most natural in your application. I will discuss only two methods here, however, because—frankly—I know as much about color as a bee knows about ancient Phoenician.

RGB

The first method I will mention is the so-called RGB colorspace. In this model, you specify the red, green, and blue components of the color you want to reproduce. This is model specifies color using the additive primaries and will be very familiar to those who work with monitors and computer graphics.

To specify a color using the RGB color space, you can use the setrgbcolor operator. This operator takes three operands: the red, green, and blue components of the color you want (0 means none, 1 means maximum). For example, to specify red, you can write 1 0 0 setrgbcolor; and to get a dark yellow, you can write 0.5 0 0.5 setrgbcolor. Black is 0 0 0 setrgbcolor, and white is 1 1 1 setrgbcolor.

CMYK

Another way to specify color is through the CMYK colorspace. In this model, you specify color using the subtractive primaries more common in the printing world: cyan, magenta, and yellow. To the primaries, a black component is added, to allow you to control the tone of the color (this is not necessary in the RGB model, since you are controlling the intensity of the light inherently).

To specify a color using the CYMK color space, you can use the setcymkcolor operator. This operator takes four operands: the cyan, yellow, magenta, and black components of the color you want (0 means none, 1 means maximum). For example, to specify red, you can write 0 1 1 0 setcymkcolor; and to get a dark yellow, you can write 0 1 0 0.5 setcymkcolor.

One convenient property of PostScript is that color is handled as a property of the ink in the graphics state just like the gray level. In fact, the gray level set by setgray is really just color. What this means is that you can set the color of a line, or the color of a fill in exactly the same way as you set the gray level. You just call the operator before you stroke the line or fill the path. You can even use it to set the color of text by calling the appropriate operator before you call show.

Since color works in such a similar way to the setgray operator, I am providing you with a complete example, but I will not break it down here.

A Warning

I have really simplified things considerably in this discussion. There are more than just two color spaces in PostScript, and there are several features in PostScript to allow for accurate color rendition. One thing that I’ve glossed over is that different devices reproduce color with different degrees of accuracy (and different pigments not only have different color-accuracy but will vary over time). Another thing that I have glossed over is the fact that these two models are focused around color reproduction: it is also possible to specify color based on how it is perceived.

If you do not care about color reproduction to that level of precision, then you do not need to worry about it. The color operators I have described are sufficient. If you do, you know far more about the matter than I do and will likely find the features you need described in the PLRM.