One Quickie


Drawing a gradient (Graphics->General)
- (void) drawGradientInRect: (CGRect) rect
                 colorSpace: (CGColorSpaceRef) colorSpcae
                    context: (CGContextRef) context {
    CGFloat startComponents[4] = { 254.0 / 255.0, 254.0 / 255.0, 254.0 / 255.0, 1.0 };
    CGFloat endComponents[4] = { 206.0 / 255.0, 206.0 / 255.0, 206.0 / 255.0, 1.0 };
    CGColorRef startColor = CGColorCreate (colorSpace, startComponents);
    CGColorRef endColor = CGColorCreate (colorSpace, endComponents);

    NSArray *array = [NSArray arrayWithObjects: (id)startColor, (id)endColor, nil];

    CGGradientRef gradient = CGGradientCreateWithColors (colorSpace,
                                                         (CFArrayRef)array, NULL);
    CGPoint endPoint = rect.origin;
    endPoint.y += rect.size.height;

    // Don't let the gradient bleed all over everything
    CGContextSaveGState (context); {
        CGContextClipToRect (context, rect);
        CGContextDrawLinearGradient (context, gradient, rect.origin, endPoint, 0);
    } CGContextRestoreGState (context);

    CGGradientRelease (gradient);
    CGColorRelease (startColor);
    CGColorRelease (endColor);

} // drawGradientInRect



borkware home | products | miniblog | rants | quickies | cocoaheads
Advanced Mac OS X Programming book

webmonster@borkware.com