forked from tidev/titanium-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TiGradient.m
288 lines (247 loc) · 7.26 KB
/
TiGradient.m
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
#import "TiGradient.h"
#import "TiUtils.h"
@implementation TiGradientLayer
@synthesize gradient;
- (void)dealloc
{
[gradient release];
[super dealloc];
}
- (void)drawInContext:(CGContextRef)ctx
{
[gradient paintContext:ctx bounds:[self bounds]];
}
@end
@implementation TiGradient
@synthesize backfillStart, backfillEnd;
- (void)ensureOffsetArraySize:(NSUInteger)newSize
{
if (newSize <= arraySize) {
return;
}
colorOffsets = realloc(colorOffsets, (sizeof(CGFloat) * newSize));
for (NSUInteger i = arraySize; i < newSize; i++) {
colorOffsets[i] = -1;
}
arraySize = newSize;
}
- (CGGradientRef)cachedGradient
{
if ((cachedGradient == NULL) && (colorValues != NULL)) {
CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
BOOL needsFreeing = NO;
CGFloat *tempOffsets;
if (offsetsDefined == CFArrayGetCount(colorValues)) {
tempOffsets = colorOffsets;
} else {
tempOffsets = NULL;
}
//TODO: Between these extremes, we should do intelligent gradient computation.
cachedGradient = CGGradientCreateWithColors(rgb, colorValues, tempOffsets);
if (needsFreeing) {
free(tempOffsets);
}
CGColorSpaceRelease(rgb);
}
return cachedGradient;
}
- (void)clearCache
{
if (cachedGradient != NULL) {
CGGradientRelease(cachedGradient);
cachedGradient = NULL;
}
}
- (void)dealloc
{
if (colorValues != NULL) {
CFRelease(colorValues);
}
[endPoint release];
[startPoint release];
[self clearCache];
free(colorOffsets);
[super dealloc];
}
- (id)type
{
switch (type) {
case TiGradientTypeRadial:
return @"radial";
default: {
break;
}
}
return @"linear";
}
- (void)setType:(id)newType
{
ENSURE_TYPE(newType, NSString);
[self clearCache];
[self replaceValue:newType forKey:@"type" notification:NO];
if ([newType compare:@"linear" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
type = TiGradientTypeLinear;
return;
}
if ([newType compare:@"radial" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
type = TiGradientTypeRadial;
return;
}
[self throwException:TiExceptionInvalidType subreason:@"Must be either 'linear' or 'radial'" location:CODELOCATION];
}
- (void)setStartPoint:(id)newStart
{
if (startPoint == nil) {
startPoint = [[TiPoint alloc] initWithObject:newStart];
} else {
[startPoint setValues:newStart];
}
[self replaceValue:newStart forKey:@"startPoint" notification:NO];
}
- (void)setEndPoint:(id)newEnd
{
if (endPoint == nil) {
endPoint = [[TiPoint alloc] initWithObject:newEnd];
} else {
[endPoint setValues:newEnd];
}
[self replaceValue:newEnd forKey:@"endPoint" notification:NO];
}
- (void)setStartRadius:(id)newRadius
{
startRadius = [TiUtils dimensionValue:newRadius];
[self replaceValue:newRadius forKey:@"startRadius" notification:NO];
}
- (void)setEndRadius:(id)newRadius
{
endRadius = [TiUtils dimensionValue:newRadius];
[self replaceValue:newRadius forKey:@"endRadius" notification:NO];
}
- (id)endRadius
{
return [self valueForUndefinedKey:@"endRadius"];
}
- (id)startRadius
{
return [self valueForUndefinedKey:@"startRadius"];
}
- (void)setColors:(NSArray *)newColors;
{
ENSURE_TYPE(newColors, NSArray);
if (colorValues == NULL) {
colorValues = CFArrayCreateMutable(NULL, [newColors count], &kCFTypeArrayCallBacks);
} else {
CFArrayRemoveAllValues(colorValues);
}
[self ensureOffsetArraySize:[newColors count]];
int currentIndex = 0;
offsetsDefined = 0;
Class dictClass = [NSDictionary class];
for (id thisEntry in newColors) {
CGFloat thisOffset = -1;
if ([thisEntry isKindOfClass:dictClass]) {
thisOffset = [TiUtils floatValue:@"offset" properties:thisEntry def:-1];
thisEntry = [thisEntry objectForKey:@"color"];
}
UIColor *thisColor = [[TiUtils colorValue:thisEntry] _color];
if (thisColor == nil) {
[self throwException:TiExceptionInvalidType
subreason:
@"Colors must be an array of colors or objects with a color property"
location:CODELOCATION];
}
CGColorSpaceRef colorspace = CGColorGetColorSpace([thisColor CGColor]);
if (CGColorSpaceGetModel(colorspace) == kCGColorSpaceModelMonochrome) //Colorize this! Where's Ted Turner?
{
const CGFloat *components = CGColorGetComponents([thisColor CGColor]);
thisColor = [UIColor colorWithRed:components[0]
green:components[0]
blue:components[0]
alpha:components[1]];
}
colorOffsets[currentIndex] = thisOffset;
if (thisOffset != -1) {
offsetsDefined++;
}
CFArrayAppendValue(colorValues, [thisColor CGColor]);
currentIndex++;
}
[self clearCache];
[self replaceValue:newColors forKey:@"colors" notification:NO];
}
#define PYTHAG(bounds) sqrt(bounds.width *bounds.width + bounds.height * bounds.height) / 2
- (void)paintContext:(CGContextRef)context bounds:(CGRect)bounds
{
CGGradientDrawingOptions options = 0;
if (backfillStart) {
options |= kCGGradientDrawsBeforeStartLocation;
}
if (backfillEnd) {
options |= kCGGradientDrawsAfterEndLocation;
}
switch (type) {
case TiGradientTypeLinear:
CGContextDrawLinearGradient(context, [self cachedGradient],
[TiUtils pointValue:startPoint
bounds:bounds
defaultOffset:CGPointZero],
[TiUtils pointValue:endPoint
bounds:bounds
defaultOffset:CGPointMake(0, 1)],
options);
break;
case TiGradientTypeRadial: {
CGFloat startRadiusPixels;
CGFloat endRadiusPixels;
switch (startRadius.type) {
case TiDimensionTypeDip:
startRadiusPixels = startRadius.value;
break;
case TiDimensionTypePercent:
startRadiusPixels = startRadius.value * PYTHAG(bounds.size);
break;
default:
startRadiusPixels = 0;
}
switch (endRadius.type) {
case TiDimensionTypeDip:
endRadiusPixels = endRadius.value;
break;
case TiDimensionTypePercent:
endRadiusPixels = endRadius.value * PYTHAG(bounds.size);
break;
default:
endRadiusPixels = PYTHAG(bounds.size);
}
CGContextDrawRadialGradient(context, [self cachedGradient],
[TiUtils pointValue:startPoint
bounds:bounds
defaultOffset:CGPointMake(0.5, 0.5)],
startRadiusPixels,
[TiUtils pointValue:endPoint
bounds:bounds
defaultOffset:CGPointMake(0.5, 0.5)],
endRadiusPixels,
options);
} break;
}
}
+ (TiGradient *)gradientFromObject:(id)value proxy:(TiProxy *)proxy
{
if ([value isKindOfClass:[NSDictionary class]]) {
id<TiEvaluator> context = ([proxy executionContext] == nil) ? [proxy pageContext] : [proxy executionContext];
TiGradient *newGradient = [[[TiGradient alloc] _initWithPageContext:context] autorelease];
[newGradient _initWithProperties:value];
return newGradient;
} else if ([value isKindOfClass:[TiGradient class]]) {
return value;
}
return nil;
}
@end