Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void CGContextFillPathWithLinearGradientVertical(CGContextRef context, CGPathRef pathRef, CGColorRef startColor, CGColorRef endColor)
- {
- CGContextSaveGState(context);
- enum { kNumLocations = 2 };
- CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
- CGFloat locations[kNumLocations] = { 0.0f, 1.0f };
- CGRect boundingBox = CGPathGetBoundingBox(pathRef);
- CGPoint pointTop = CGPointMake(CGRectGetMidX(boundingBox), CGRectGetMinY(boundingBox));
- CGPoint pointBottom = CGPointMake(CGRectGetMidX(boundingBox), CGRectGetMaxY(boundingBox));
- CGFloat r1, g1, b1, a1, r2, g2, b2, a2;
- CGColorToRGBA(startColor, &r1, &g1, &b1, &a1);
- CGColorToRGBA(endColor, &r2, &g2, &b2, &a2);
- ZRGBA components[kNumLocations] = { { r1, g1, b1, a1 }, { r2, g2, b2, a2 } };
- CGContextAddPath(context, pathRef);
- CGContextClip(context);
- CGGradientRef gradientRef = CGGradientCreateWithColorComponents(colorSpaceRef, (CGFloat *)components, locations, kNumLocations);
- CGContextDrawLinearGradient(context, gradientRef, pointTop, pointBottom, kCGGradientDrawsBeforeStartLocation);
- CGGradientRelease(gradientRef);
- CGColorSpaceRelease(colorSpaceRef);
- CGContextRestoreGState(context);
- }
Add Comment
Please, Sign In to add comment