Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Setting and Creating Colors

Quartz provides a suite of functions for setting fill color, stroke color, color spaces, and alpha. Each of these color parameters apply to the graphics state, which means that once set, that setting remains in effect until set to another value.

A color must have an associated color space. Otherwise, Quartz won’t know how to interpret color values. Further, you need to supply an appropriate color space for the drawing destination. Compare the blue fill color on the left side of Figure 4-4, which is a CMYK fill color, with the blue color shown on the right side, which is an RGB fill color. If you view the onscreen version of this document, you’ll see a large difference between the fill colors. The colors are theoretically identical, but appear identical only if the RGB color is used for an RGB device and the CMYK color is used for a CMYK device.


Figure 4-4  A CMYK fill color and an RGB stroke color

A CMYK fill color and an RGB stroke color

You can use the functions CGContextSetFillColorSpace and CGContextSetStrokeColorSpace to set the fill and stroke color spaces, or you can use one of the convenience functions (listed in Table 4-2) that set color for a device color space.

Table 4-2  Color-setting functions

Function

Use to set color for

CGContextSetRGBStrokeColor

CGContextSetRGBFillColor

Device RGB. At PDF generation time Quartz writes the colors as if they are in the corresponding generic color space.

CGContextSetCMYKStrokeColor

CGContextSetCMYKFillColor

Device CMYK. (Remains Device CMYK at PDF generation time.)

CGContextSetGrayStrokeColor

CGContextSetGrayFillColor

Device Gray. At PDF generation time Quartz writes the colors as if they are in the corresponding generic color space.

CGContextSetStrokeColorWithColor

CGContextSetFillColorWithColor

Any color space; you supply a CGColor object that specifies the color space. Use these functions for colors you need repeatedly. Available in Mac OS X v10.3 and later

CGContextSetStrokeColor

CGContextSetFillColor

The current color space. Not recommended. Instead, set color using a CGColor object and the functions CGContextSetStrokeColorWithColor and CGContextSetFillColorWithColor.

You specify the fill and stroke colors as values located within the fill and stroke color spaces. For example, a fully saturated red color in the RGB color space is specified as an array of four numbers: (1.0, 0.0, 0.0, 1.0). The first three numbers specify full red intensity and no green or blue intensity. The fourth number is the alpha value, which is used to specify the opacity of the color.

If you reuse colors in your application, the most efficient way to set fill and stroke colors is to create a CGColor object, which you then pass as a parameter to the functions CGContextSetFillColorWithColor and CGContextSetStrokeColorWithColor. You can keep the CGColor object around as long as you need it. The CGColorRef opaque data type and the functions that create and manage CGColor objects are available in Mac OS X v10.3 and later. However, the CGColorRef data type has always been used internally by Quartz to represent colors. You can improve your application’s performance by using CGColor objects directly.

You create a CGColor object by calling the function CGColorCreate, passing a CGColorspace object and an array of floating-point values that specify the intensity values for the color. The last component in the array specifies the alpha value.



< Previous PageNext Page > Hide TOC


Last updated: 2007-12-11




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice