Quartz 2D Programming Guide (4)-color and Color Space

Source: Internet
Author: User
Tags color representation color gamut

Different devices (monitors, printers, scanners, and cameras) process different colors. Each device has a range of supported color values. Colors supported by one device may not be supported by other devices.
To effectively use the color and understand the functions used in quartz 2d for color and color space, we need to be familiar with the terms used in the color management overview document. This document discusses the color perception, color value, device dependency, device color space, color matching problem, Rendering Intent, color management module, and ColorSync.
In this chapter, we will learn how to process the color and color space of quartz and what is the Alpha component. This chapter also discusses the following issues:

  • Create Color Space
  • Create and set colors
  • Set reproduction intent

Color and Color Space
The color in quartz is represented by a set of values. The color space is used to parse the color information. For example, table 4-1 lists the values of blue in different color spaces at full brightness. If you do not know the values that the color space and the color space can accept, we cannot know the color represented by a set of values.

 
If we use the wrong color space, we may obtain completely different colors, as shown in 4-1.

 
Color Space can have different quantities of components. In the color space of Table 4-1, three of them have only three components, while CMYK has four components. The value range is related to the color space. For most color spaces, the color value range is [0.0, 1.0], and 1.0 indicates the full brightness. For example, the value of the full brightness blue value in the RGB Color Space of quartz is (0, 0, 1.0 ). In quartz, the color value also has an Alpha value to indicate transparency. This value is not listed in Table 4-1.
Alpha Value
The Alpha value is a graphical state parameter. Quartz uses it to determine how a new drawing object is mixed with an existing one. The new drawing object is not transparent at full strength. The new drawing object is completely transparent at 0 strength. Figure 4-2 shows five large squares with Alpha values 1.0, 0.75, 0.5, 0.1, and 0.0 respectively. As the square gradually becomes transparent, the small opaque square gradually appears.

 
We can draw two objects to the page, and the page can set its own transparency by setting the global graphics context before rendering. Figure 4-3 shows the effect of setting the global transparency to 0.5 and 1.0.

 
In the standard mixed mode (the default mode of the image state), quartz uses the following formula to mix the components of the source and target colors:

Copy code
  1. Destination = (alpha * Source) + (1-alpha) * destination


The source color is the new color, and the target color is the background color. This formula can be used for newly drawn shapes and images.
For object transparency, if the Alpha value is 1.0, the new object is completely opaque. If the Alpha value is 0.0, the new object is completely transparent. The value between 0.0 and 1.0 specifies the transparency of the object. We can specify an Alpha value as the last component of the color value for all programs that accept the color. You can also use the cgcontextsetalpha function to specify the global Alpha value. Remember, if both values are set, quartz will mix the global Alpha value with the Alpha value of the object.
To make the page completely transparent, we can call the cgcontextclearrect function to clear the alpha channel of the graphic context. For example, we can use this method when creating a transparent mask for the icon or making the background of the window transparent.
Create Color Space
Quartz supports the standard color space used by the color management system, as well as general color space, index color space, and pattern color space. The device color space is a simple way to represent colors between different devices. It is used to convert color data in the local color space between two different devices. The color of the device depends on the Color Space. Different devices have the same validity period, which expands the capabilities of the device. Based on this, the device depends on the color space, which is the best choice for color display.
If the application requires accurate color representation, the device must always use the color space. Generic Color Space (generic color space) is a commonly used Device Dependent color space. The general color space provides the best color space for our applications through the operating system. It enables the display to have the same effect as printing on the printer.

Reference text \ "> important: IOS does not support devices that depend on the color space or general color space. The iOS app must use the device color space ).


Create a device dependency Color Space
To create a device dependent color space, we need to provide quartz with a white reference point, a black reference point, and a gamma value for a special device. Quartz uses this information to convert the color value of the source color space to the color value of the output device color space.
Quartz allows devices to depend on the Color Space. The function for creating this space is as follows:

  • L * a * B is a non-linear conversion, which belongs to the Munsell Color symbol system (color, value, and saturation are used to specify the color ). L component represents the brightness value, a component represents the value between green and red, and B component represents the value between blue and yellow. This color space is designed to simulate human brain decoding colors. Use the cgcolorspacecreatelab function to create a cluster.
  • The Color Space is determined by the Color. The Color is configured with the color fields supported by the device. The color fields are consistent with those of other devices, therefore, this information can be used to accurately convert the color space of one device to the color space of another device. Most device manufacturers support configurations. Some color displays and printers are embedded with information for processing bitmap formats such as tiff. Use the cgcolorspacecreateiccbased function to create a cluster.
  • Standard RGB is the RGB color space that the device depends on. It represents the color relative to the white reference point (the most white color that the device can generate. Use the cgcolorspacecreatecalibratedrgb function to create an image.
  • Standard gray scale is the gray color space on which the device depends. It represents the color relative to the white reference point (the most white color that the device can generate. Use the cgcolorspacecreatecalibratedgray function to create a cluster.

Create a Universal Color Space
The color of the general color space matches the system. In most cases, the results are acceptable. As the name implies, each "generic" Color Space (generic gray, generic RGB, and generic CMYK) is a specified Device Dependent color space.
Color Space is very easy to use; we do not need to provide any reference point information. We use the function cgcolorspacecreatewithname to create a universal color space. This function can input the following constant values:

  • Kcgcolorspacegenericgray: specifies the general gray color space. The color space is monochrome and can be a color value ranging from 0.0 (pure black) to 1.0 (pure white.
  • Kcgcolorspacegenericrgb: Specifies the universal RGB color space. The color value in this color space consists of three components (red, green, blue), which are mainly used for pixels on the color display. The value range of each component in the RGB color space is [0.0, 1.0].
  • Kcgcolorspacegenericcmyk: specifies the general CMYK color space. The color value of this color space consists of four components (cyan, magenta, yellow, black), which are mainly used for printers. The value range of each component in the CMYK color space is [0.0, 1.0].

Create a device color space
The device color space is mainly used in IOS apps, because it cannot be used on IOS. In most cases, Mac OS X applications should use a Universal Color Space instead of a device color space. However, some quartz programs want the image to use the device color space. For example, if you call the cgimagecreatewithmask function to specify an image as a mask, the image must be defined in the device gray color space.
You can use the following functions to create a device color space:

  • Cgcolorspacecreatedevicegray: Creates a device that depends on a gray color space.
  • Cgcolorspacecreatedevicergb: Creates a device that depends on the RGB color space.
  • Cgcolorspacecreatedevicecmyk: Creates a CMYK color space for a device.

Create index color space and mode color space
The index color space contains a color table with 256 words, which maps to the base color space. Each word in the color table specifies a color value in the base color space. Use the cgcolorspacecreateindexed function to create an ECS instance.
The mode color space is used in the draw mode. Use the cgcolorspacecreatepattern function.
Set and create colors
Quartz provides a set of functions for setting fill colors, wireframes colors, color spaces, and Alpha values. Each color parameter is a graphical status parameter, which means that once set, the settings will be saved and affect subsequent operations until they are modified.
A color must have an associated color space. Otherwise, quartz does not know how to parse the color value. To put it further, we must provide a suitable color space for the drawing target. 4-4, the left is the blue fill color in the CMYK color space, and the right is the blue fill color in the RGB color space. These two color values are theoretically the same, but they are only displayed in the same color space.

 
You can use the cgcontextsetfillcolorspace and cgcontextsetstrokecolorspace functions to set the fill and box color spaces, or use the following convenient functions to set the color values of the device color space.
Table 4-2Color-setting functions

Function Purpose
Cgcontextsetrgbstrokecolor
Cgcontextsetrgbfillcolor
Device RGB. When a PDF file is generated, quartz writes the color as in the corresponding universal color space.
Cgcontextsetcmykstrokecolor
Cgcontextsetcmykfillcolor
CMYK. When generating PDF files, keep the device CMYK
Cgcontextsetgraystrokecolor
Cgcontextsetgrayfillcolor
Device grayscale. When a PDF file is generated, quartz writes the color as in the corresponding universal color space.
Cgcontextsetstrokecolorwithcolor
Cgcontextsetfillcolorwithcolor
Any color space; a cgcolor object that specifies the color space is provided.
Cgcontextsetstrokecolor
Cgcontextsetfillcolor
Current color space. It is not recommended. For more information, use the cgcolor object and function cgcontextsetstrokecolorwithcolor and cgcontextsetfillcolorwithcolor.

We specify the fill and box color values in the fill and box color spaces. For example, in the RGB color space, we use an array (1.0, 0.0, 0.0, 1.0) to represent red. The first three values specify the red value as the full intensity, while the Green and Blue values are zero intensity. The fourth value is Alpha, used to specify the transparency of the color.
If you need to reuse the color in the program, the most effective method is to create a cgcolor object by setting the fill color and the line color, and then pass the object to the function cgcontextsetfillcolorwithcolor and cgcontextsetstrokecolorwithcolor. We can keep the cgcolor object as needed and use it directly to improve the display of the application.
You can call the cgcolorcreate function to create a cgcolor object. This function requires two parameters: the cgcolorspace object and the color value array. The last value of the array specifies the Alpha value.
Rending intent)
"Reproduction intent" is used to specify how to map the color of the source color space to the color range of the target color space in the graphic context. If no reproduction intent is displayed, quartz uses relative colorimetric Rendering Intent to apply it to all plotting (excluding bitmap images ). For bitmap images, quartz uses perceptual to reproduce the intent by default.
You can call the cgcontextsetrenderingintent function to set the reproduction intent, and pass the graphic context and the following constant as the parameter:

  • Kcgrenderingintentdefault: use the default Rendering Intent.
  • Kcgrenderingintentabsolutecolorimetric: Specifies the absolute Color Rendering Intent. Map the colors outside the output device color field to the nearest color in the output device area. This produces a reduction because two different color values outside the color gamut may be mapped to the same color value in the color gamut. This method is the best if the color value used by the image is included in both the source Color Gamut and the target color gamut. It is often used for logo or spot color.
  • Kcgrenderingintentrelativecolorimetric: relative Color Rendering Intent. Convert all colors (including within the color gamut) to compensate for the color difference between the White points of the graphic context and the white points of the output device. Kcgrenderingintentperceptual: Specifies the Rendering Intent. You can compress the color gamut of the image context to adapt to the color gamut of the output device and maintain the relativity between colors in the source color space. The perceived Rendering Intent applies to photos and other complex high-granularity images.
  • Kcgrenderingintentsaturation: Saturation Rendering Intent. The relative saturation of colors is maintained when the color is converted to the color gamut of the output device. The result is an image that contains the brightness and saturation colors. The saturation intent applies to generating low-level images, such as descriptive charts.

Original post address: http://www.cocoachina.com/bbs/read.php? Tid = 78374

Quartz 2D Programming Guide (4)-color and Color Space

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.