-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path_CPFillStripes.m
More file actions
146 lines (111 loc) · 3.99 KB
/
Copy path_CPFillStripes.m
File metadata and controls
146 lines (111 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//
// _CPFillStripes.m
// CorePlot-CocoaTouch
//
// Created by admin on 3/21/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "_CPFillStripes.h"
#import "_CPFillGradient.h"
#import "CPColor.h"
#include <math.h>
@interface _CPFillStripes(private)
-(void)drawStripesInRect:(CGRect)rect inContext:(CGContextRef)ctx;
@end
@implementation _CPFillStripes
#pragma mark -
#pragma mark init/dealloc
-(id)initWithFirstColor:(CPColor *)_firstColor secondColor:(CPColor *)_secondColor stripeWidth:(NSUInteger)_stripeWidth;
{
if ( (self = [super init]) ) {
firstColor = [_firstColor retain];
secondColor = [_secondColor retain];
stripeWidth = _stripeWidth;
}
return self;
}
-(void)dealloc
{
[firstColor release];
[secondColor release];
[super dealloc];
}
#pragma mark -
#pragma mark Drawing
-(void)drawStripesInRect:(CGRect)rect inContext:(CGContextRef)ctx
{
//CGContextSetFillColorWithColor(ctx, firstColor.cgColor);
//CGContextFillRect(ctx, rect);
int nStripes = (int)ceil(rect.size.height / (2 * stripeWidth));
CGFloat yCoordIncrement = rect.size.height / nStripes;
const CGFloat* firstColorComps = CGColorGetComponents(firstColor.cgColor);
const CGFloat* secondColorComps = CGColorGetComponents(secondColor.cgColor);
CGFloat colorIncrement[4];
CGFloat fillColor[4];
colorIncrement[0] = (secondColorComps[0] - firstColorComps[0]) / nStripes;
colorIncrement[1] = (secondColorComps[1] - firstColorComps[1]) / nStripes;
colorIncrement[2] = (secondColorComps[2] - firstColorComps[2]) / nStripes;
colorIncrement[3] = (secondColorComps[3] - firstColorComps[3]) / nStripes;
memcpy(fillColor, firstColorComps, sizeof(CGFloat) * 4);
CGFloat stripeY = rect.origin.y;
for (int i = 0; i < nStripes; stripeY += yCoordIncrement, ++i) {
CGRect stripeRect = CGRectMake(rect.origin.x, stripeY, rect.size.width, stripeWidth);
CGContextSetFillColorWithColor(ctx, [CPColor colorWithComponentRed:fillColor[0] green:fillColor[1] blue:fillColor[2] alpha:fillColor[3]].cgColor);
CGContextFillRect(ctx, stripeRect);
fillColor[0] = fillColor[0] + colorIncrement[0];
fillColor[1] = fillColor[1] + colorIncrement[1];
fillColor[2] = fillColor[2] + colorIncrement[2];
fillColor[3] = fillColor[3] + colorIncrement[3];
}
}
-(void)fillRect:(CGRect)theRect inContext:(CGContextRef)theContext
{
CGContextSaveGState(theContext);
CGContextClipToRect(theContext, *(CGRect *)&theRect);
// draw here
[self drawStripesInRect:theRect inContext:theContext];
CGContextRestoreGState(theContext);
}
-(void)fillPathInContext:(CGContextRef)theContext
{
if ( !CGContextIsPathEmpty(theContext) ) {
CGContextSaveGState(theContext);
CGRect boxBounds = CGContextGetPathBoundingBox(theContext);
CGContextClip(theContext);
// draw here
[self drawStripesInRect:boxBounds inContext:theContext];
CGContextRestoreGState(theContext);
}
}
#pragma mark -
#pragma mark NSCopying methods
-(id)copyWithZone:(NSZone *)zone
{
_CPFillStripes *copy = [[[self class] allocWithZone:zone] init];
copy->firstColor = [self->firstColor copyWithZone:zone];
copy->secondColor = [self->secondColor copyWithZone:zone];
copy->stripeWidth = self->stripeWidth;
return copy;
}
#pragma mark -
#pragma mark NSCoding methods
-(Class)classForCoder
{
return [CPFill class];
}
-(void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:firstColor forKey:@"firstColor"];
[coder encodeObject:secondColor forKey:@"secondColor"];
[coder encodeObject:[NSNumber numberWithUnsignedInteger:stripeWidth] forKey:@"stripeWidth"];
}
-(id)initWithCoder:(NSCoder *)coder
{
if ( (self = [super init]) ) {
firstColor = [[coder decodeObjectForKey:@"firstColor"] retain];
secondColor = [[coder decodeObjectForKey:@"secondColor"] retain];
stripeWidth = [[coder decodeObjectForKey:@"stripeWidth"] unsignedIntegerValue];
}
return self;
}
@end