Drawing with Patterns


Quartz 2D treats patterns as special types of color. Once you have created a pattern, setting up a context to draw it is very similar to setting up the context to draw a color. Patterns "live" in special pattern color spaces. The first step in drawing a pattern is to create a pattern color space. You can use that pattern color space as the stroke or fill pattern in the current context. From that point, using the pattern is as easy as stroking or filling the paths you want to draw.

Pattern Color Spaces

To create a pattern color space, you call the routine CGColorSpaceCreatePattern. This routine accepts a single parameter. For colored patterns, you should pass NULL as the base color space. If you are using a stencil pattern, you should pass a traditional color space like Generic RGB or the color space of the main display. When you draw the stencil pattern you will specify the color with which Quartz should draw the stencil by specifying the components of a color from this color space.

After creating the pattern color space, your code can use CGContextSetFillCol orSpace and CGContextSetStrokeColorSpace to indicate that you want to use a pattern for fills or strokes, respectively.

Setting the Current Pattern

After you have the color space settings of the context set up, you can set the current fill pattern for the context by calling CGContextSetFillPattern and the current stroke pattern with CGContextSetStrokePattern. Each of these routines accept the context you wish to change, a CGPatternRef, and an array of floating point values.

The values you supply in the floating point array depend on what type of pattern you are working with. If you are working with a colored pattern, then you should pass in a single floating point value that indicates the alpha value you want to use for your colored pattern. For example, if you pass in 0.5 as the alpha value, the computer will draw your pattern with 50% translucency.

If you are working with a stencil pattern, the floating point array is where you specify the color Quartz should apply to the stencil. You will need to specify the components of a color in the color space you provided as a base to the pattern color space you created. For example, if you indicated that your pattern's base space was Generic CMYK, your floating point array would have five values in itone for each of the four color components and one for the alpha value.

Drawing Patterned Graphics

After calling CGContextSetFillPattern or CGContextSetStrokePattern, any strokes or fills that your code calls for will draw with the current pattern. Listing 13.6 ties together the entire process of drawing with a colored pattern. This is part of the code used to draw Figure 13.2.

Listing 13.6. Drawing a Colored Pattern

float opaque = 1.0; CGColorSpaceRef patternSpace = CGColorSpaceCreatePattern(NULL); CGContextSetFillColorSpace(inContext, patternSpace); CGContextSetFillPattern(inContext, pattern, &opaque); CGContextAddRect(inContext, bounds); CGContextFillPath(inContext); CGColorSpaceRelease(patternSpace); CGPatternRelease(pattern); 

You are drawing a colored pattern so you pass NULL to CGColorSpaceCreatePattern. You then tell the computer that we are interested in using the color space you've created as the fill color space. The pattern passed to CGContextSetFillPattern is the same pattern created in Listing 13.5. To use the pattern, you simply add a rectangular path to the current context and then call CGContextFillPath. Quartz will fill the path with your pattern. Upon returning, you clean up the pattern space and the pattern.

Listing 13.7 is a block of code that draws a stencil cell in seven different colors. This listing was taken from the Patterns code sample from the code sample CD.

Listing 13.7. Drawing a Stencil Pattern in a Variety of Colors

// To draw the pattern we need a pattern color space CGColorSpaceRef baseColorSpace = CGColorSpaceCreateWithName(         kCGColorSpaceGenericRGB); CGColorSpaceRef patternSpace = CGColorSpaceCreatePattern(         baseColorSpace); CGColorSpaceRelease(baseColorSpace); baseColorSpace = NULL; CGContextSetFillColorSpace(inContext, patternSpace); CGColorSpaceRelease(patternSpace); patternSpace = NULL; static float const rainbowColors[7][4] = {         { 1.0, 0.0, 0.0, 1.0 }, // red         { 1.0, 0.5, 0.0, 1.0 }, // orange         { 1.0, 1.0, 0.0, 1.0 }, // yellow         { 0.0, 1.0, 0.0, 1.0 }, // green         { 0.0, 0.0, 1.0, 1.0 }, // blue         { 0.5, 0.0, 1.0, 1.0 }, // indigo         { 0.5, 0.0, 0.5, 1.0 }, // violet }; // Add a path to fill with our pattern and fill it. float rectHeight = bounds.size.height / 7; for(short ctr = 0; ctr < 7; ctr++) {         CGRect patternBounds = CGRectMake(                        bounds.origin.x,                        bounds.origin.y + ctr * rectHeight,                        bounds.size.width,                        rectHeight);         CGContextSetFillPattern(inContext,                pattern, rainbowColors[ctr]);         CGContextAddRect(inContext, patternBounds);         CGContextFillPath(inContext); } CGPatternRelease(pattern); pattern = NULL; 

This code listing begins much like the previous one, with the construction of a pattern color space. In this case, because you are using a stencil pattern, you need to provide a base color space (Generic RGB has been chosen here). As before, after creating the pattern color space, you set up the context to use that the fill color space for the port.

The large array in the middle of the code example simply defines seven RGBA colors (the same ones used in the shading example). The loop at the bottom is responsible for drawing the pattern in a series of seven rectangles. To draw each rectangle, you set the current fill pattern and call CGContextSeFillPattern to pass one of the seven colors as the color the computer should apply through the stencil. The pattern is drawn, then, using CGContextFillPath.




Quartz 2D Graphics for Mac OS X Developers
Quartz 2D Graphics for Mac OS X Developers
ISBN: 0321336631
EAN: 2147483647
Year: 2006
Pages: 100

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net