CoreGraphics 学习摘记 之 Color and Color Spaces

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.

Table 4-1  Color values in different color spaces

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

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. 

You can supply an alpha value as the last color component to all routines that accept colors. You can also set the global alpha value using the  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.

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 were 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 were 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.

CGContextSetStrokeColor

CGContextSetFillColor

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


If you reuse colors in your application, the most efficient way to set fill and stroke colors is to create a CGColor object。 You can keep the CGColor object around as long as you need it. 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.

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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值