Chapter 17. Color

Although we have used color in most of our examples, we have used it in only a cursory fashion — just black and white. That might have been fine back in the 1980s and early 1990s, when lots of people still had monochrome displays, but these days almost every computer has a full-color display. As you might expect, Cocoa’s Quartz drawing environment makes drawing in color beautiful and easy.

Colors and Color Objects

Cocoa colors have two components: color and alpha. Color is the way that the color looks when it is displayed on an empty background. Alpha is a measure of the color’s transparency — it tells Quartz how to blend a color with the colors already present in the background when the color is displayed. Alpha is measured on a scale from 0.0 to 1.0. An alpha of 0.0 is completely transparent; an alpha of 1.0 is opaque (nothing shows through it).

Cocoa enables you to specify the color using several different models, called color spaces . As with most computer systems, one option is to specify values for the amount of red, green, and blue (RGB). RGB is an additive color model — the colors are added together like colored lights (mix them all in equal amounts and you get white). Alternatively, you can specify a color by specifying a hue, saturation, and brightness (HSB). You can also specify the amount of cyan, yellow, magenta, and black (CMYK) “inks” to mix at any point. CMYK is called a subtractive color model because the colors are subtracted from white; mix ...

Get Building Cocoa Applications: A Step by Step Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.