About Color and Color Spaces
A color in Quartz is represented by a set of values. The values are meaningless without a color space that dictates how to interpret color information. In Quartz, color also has an alpha value that specifies the transparency of a color.
Values | Color space | Components |
---|---|---|
240 degrees, 100%, 100% | HSB | Hue, saturation, brightness |
0, 0, 1 | RGB | Red, green, blue |
1, 1, 0, 0 | CMYK | Cyan, magenta, yellow, black |
1, 0, 0 | BGR | Blue, green, red |
The Alpha Value
Figure 4-2 A comparison of large rectangles painted using various alpha values![A comparison of large rectangles painted using various alpha values](https://i-blog.csdnimg.cn/blog_migrate/c1ed9ef42b868baa6ab393a557c7f102.gif)
In the normal blend mode (which is the default for the graphics state) Quartz performs alpha blending by combining the components of the source color with the components of the destination color using the formula:
destination = (alpha * source) + (1 - alpha) * destination
where source
is one component of the new paint color and destination
is one component of the background color.
CGContextSetAlpha
function. Keep in mind that if you set both, Quartz multiplies the alpha color component by the global alpha value.
To allow the page itself to be fully transparent, you can explicitly clear the alpha channel of the graphics context using the CGContextClearRect
function, as long as the graphics context is a window or bitmap graphics context.
Creating Color Spaces
Important: iOS does not support device-independent or generic color spaces. iOS applications must use device color spaces instead.
Creating Device Color Spaces
You create a device color space by using one of the following functions:
-
CGColorSpaceCreateDeviceGray
for a device-dependent grayscale color space. -
CGColorSpaceCreateDeviceRGB
for a device-dependent RGB color space. -
CGColorSpaceCreateDeviceCMYK
for a device-dependent CMYK color space.
Setting and Creating Colors
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.
Function | Use to set color for |
---|---|
Device RGB. At PDF-generation time, Quartz writes the colors as if they were in the corresponding generic color space. | |
Device CMYK. (Remains device CMYK at PDF-generation time.) | |
Device Gray. At PDF-generation time, Quartz writes the colors as if they were in the corresponding generic color space. | |
Any color space; you supply a CGColor object that specifies the color space. Use these functions for colors you need repeatedly. | |
The current color space. Not recommended. Instead, set color using a CGColor object and the functions |
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.
Setting Rendering Intent
To set the rendering intent, call the function CGContextSetRenderingIntent
, passing a graphics context and one of the following constants:
-
kCGRenderingIntentDefault
. Uses the default rendering intent for the context. -
kCGRenderingIntentAbsoluteColorimetric
. Maps colors outside of the gamut of the output device to the closest possible match inside the gamut of the output device. This can produce a clipping effect, where two different color values in the gamut of the graphics context are mapped to the same color value in the output device’s gamut. This is the best choice when the colors used in the graphics are within the gamut of both the source and the destination, as is often the case with logos or when spot colors are used. -
kCGRenderingIntentRelativeColorimetric
. The relative colorimetric shifts all colors (including those within the gamut) to account for the difference between the white point of the graphics context and the white point of the output device. -
kCGRenderingIntentPerceptual
. Preserves the visual relationship between colors by compressing the gamut of the graphics context to fit inside the gamut of the output device. Perceptual intent is good for photographs and other complex, detailed images. -
kCGRenderingIntentSaturation
. Preserves the relative saturation value of the colors when converting into the gamut of the output device. The result is an image with bright, saturated colors. Saturation intent is good for reproducing images with low detail, such as presentation charts and graphs.