-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSYCompositor.m
More file actions
298 lines (249 loc) · 9.28 KB
/
SYCompositor.m
File metadata and controls
298 lines (249 loc) · 9.28 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
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
289
290
291
292
293
294
295
296
297
298
//
// SYCompositor.m
// SYCompositor
//
// Created by Sam Soffes on 9/15/11.
// Copyright (c) 2011-2012 Synthetic. All rights reserved.
//
#import "SYCompositor.h"
#import "SYCache.h"
#import <UIKit/UIKit.h>
NSString *const kSYCompositorModeKey = @"mode";
NSString *const kSYCompositorRectKey = @"rect";
NSString *const kSYCompositorImageKey = @"image";
NSString *const kSYCompositorImageNameKey = @"imageName";
NSString *const kSYCompositorImagePathKey = @"imagePath";
NSString *const kSYCompositorColorKey = @"color";
NSString *const kSYCompositorColorHexKey = @"colorHex";
NSString *const kSYCompositorAlphaKey = @"alpha";
NSString *const kSYCompositorMaskImageKey = @"maskImage";
NSString *const kSYCompositorMaskImageNameKey = @"maskImageName";
NSString *const kSYCompositorMaskImagePathKey = @"maskImagePath";
NSString *const kSYCompositorPersistentMaskKey = @"persistentMask";
NSString *const kSYCompositorGradientKey = @"gradient";
NSString *const kSYCompositorGradientColorsKey = @"gradientColors";
NSString *const kSYCompositorGradientColorsHexesKey = @"gradientColorsHexes";
static CGBlendMode _CGBlendModeWithString(NSString *string) {
if ([string isEqualToString:@"Multiply"]) {
return kCGBlendModeMultiply;
} else if ([string isEqualToString:@"Screen"]) {
return kCGBlendModeScreen;
} else if ([string isEqualToString:@"Overlay"]) {
return kCGBlendModeOverlay;
} else if ([string isEqualToString:@"Darken"]) {
return kCGBlendModeDarken;
} else if ([string isEqualToString:@"Lighten"]) {
return kCGBlendModeLighten;
} else if ([string isEqualToString:@"ColorDodge"]) {
return kCGBlendModeColorDodge;
} else if ([string isEqualToString:@"ColorBurn"]) {
return kCGBlendModeColorBurn;
} else if ([string isEqualToString:@"SoftLight"]) {
return kCGBlendModeSoftLight;
} else if ([string isEqualToString:@"HardLight"]) {
return kCGBlendModeHardLight;
} else if ([string isEqualToString:@"Difference"]) {
return kCGBlendModeDifference;
} else if ([string isEqualToString:@"Exclusion"]) {
return kCGBlendModeExclusion;
} else if ([string isEqualToString:@"Hue"]) {
return kCGBlendModeHue;
} else if ([string isEqualToString:@"Saturation"]) {
return kCGBlendModeSaturation;
} else if ([string isEqualToString:@"Color"]) {
return kCGBlendModeColor;
} else if ([string isEqualToString:@"Luminosity"]) {
return kCGBlendModeLuminosity;
} else if ([string isEqualToString:@"PlusLighter"]) {
return kCGBlendModePlusLighter;
} else if ([string isEqualToString:@"PlusDarker"]) {
return kCGBlendModePlusDarker;
} else if ([string isEqualToString:@"Clear"]) {
return kCGBlendModeClear;
}
return kCGBlendModeNormal;
}
static unsigned long _integerFromHexString(NSString *string) {
unsigned long result = 0;
sscanf([string UTF8String], "%lx", &result);
return result;
}
@interface UIColor (SYCompositorAdditions)
+ (UIColor *)_colorWithHex:(NSString *)hex;
@end
@implementation UIColor (SYCompositorAdditions)
// Copied from SSToolkit
+ (UIColor *)_colorWithHex:(NSString *)hex {
// Remove `#`
if ([[hex substringWithRange:NSMakeRange(0, 1)] isEqualToString:@"#"]) {
hex = [hex substringFromIndex:1];
}
// Invalid if not 3, or 6 characters
NSUInteger length = [hex length];
if (length != 3 && length != 6) {
return nil;
}
NSUInteger digits = length / 3;
CGFloat maxValue = (digits == 1) ? 15.0f : 255.0f;
CGFloat red = _integerFromHexString([hex substringWithRange:NSMakeRange(0, digits)]) / maxValue;
CGFloat green = _integerFromHexString([hex substringWithRange:NSMakeRange(digits, digits)]) / maxValue;
CGFloat blue = _integerFromHexString([hex substringWithRange:NSMakeRange(2 * digits, digits)]) / maxValue;
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
}
@end
@interface SYCompositor ()
+ (UIImage *)_drawWithLayers:(NSArray *)layers size:(CGSize)size;
@end
@implementation SYCompositor
+ (UIImage *)imageWithKey:(NSString *)key {
return [[self cache] imageForKey:key];
}
+ (UIImage *)imageWithLayers:(NSArray *)layers size:(CGSize)size key:(NSString *)key {
UIImage *image = [self imageWithKey:key];
// If no image, draw it
if (!image) {
image = [self _drawWithLayers:layers size:size];
// If an image was rendered, save it
if (image) {
[[self cache] setImage:image forKey:key];
}
}
return image;
}
+ (NSString *)pathForImageWithKey:(NSString *)key {
return [[self cache] pathForKey:key];
}
+ (UIImage *)_drawWithLayers:(NSArray *)layers size:(CGSize)size {
if (!layers) {
return nil;
}
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, [[UIScreen mainScreen] scale]);
CGContextRef context = UIGraphicsGetCurrentContext();
// Transform coordinates
CGContextTranslateCTM(context, 0.0f, size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// Loop through blending layers
for (NSDictionary *dictionary in layers) {
if ([[dictionary objectForKey:kSYCompositorPersistentMaskKey] boolValue] == NO) {
CGContextSaveGState(context);
}
// Blend mode
NSString *modeString = [dictionary objectForKey:kSYCompositorModeKey];
CGBlendMode blendMode = modeString ? _CGBlendModeWithString(modeString) : kCGBlendModeNormal;
CGContextSetBlendMode(context, blendMode);
// Alpha
NSNumber *alphaNumber = [dictionary objectForKey:kSYCompositorAlphaKey];
CGFloat alpha = alphaNumber ? [alphaNumber floatValue] : 1.0f;
CGContextSetAlpha(context, alpha);
// Rect
id rectValue = [dictionary objectForKey:kSYCompositorRectKey];
CGRect frame = CGRectZero;
if (rectValue) {
if ([rectValue isKindOfClass:[NSValue class]]) {
frame = [rectValue CGRectValue];
} else if ([rectValue isKindOfClass:[NSString class]]) {
frame = CGRectFromString(rectValue);
} else {
frame = rect;
}
} else {
frame = rect;
}
frame = CGRectMake(frame.origin.x, (size.height - frame.size.height) - frame.origin.y,
frame.size.width, frame.size.height);
// Mask
UIImage *maskImage = nil;
NSString *maskImageName = [dictionary objectForKey:kSYCompositorMaskImageNameKey];
if (maskImageName) {
maskImage = [UIImage imageNamed:maskImageName];
} else {
NSString *maskImagePath = [dictionary objectForKey:kSYCompositorMaskImagePathKey];
if (maskImagePath) {
maskImage = [UIImage imageWithContentsOfFile:maskImagePath];
}
}
if (maskImage) {
CGContextClipToMask(context, frame, maskImage.CGImage);
}
// Draw color
UIColor *color = [dictionary objectForKey:kSYCompositorColorKey];
if (!color) {
NSString *colorString = [dictionary objectForKey:kSYCompositorColorHexKey];
if (colorString) {
color = [UIColor _colorWithHex:colorString];
}
}
if (color) {
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, frame);
}
// Draw image
UIImage *image = [dictionary objectForKey:kSYCompositorImageKey];
if (!image) {
NSString *imageName = [dictionary objectForKey:kSYCompositorImageNameKey];
if (imageName) {
image = [UIImage imageNamed:imageName];
} else {
NSString *imagePath = [dictionary objectForKey:kSYCompositorImagePathKey];
if (imagePath) {
image = [UIImage imageWithContentsOfFile:imagePath];
}
}
}
if (image) {
CGContextDrawImage(context, frame, image.CGImage);
}
// Draw gradient
NSString *gradientType = [dictionary objectForKey:kSYCompositorGradientKey];
if (gradientType) {
NSMutableArray *colors = [dictionary objectForKey:kSYCompositorGradientColorsKey];
if (!colors) {
NSArray *colorStrings = [dictionary objectForKey:kSYCompositorGradientColorsHexesKey];
colors = [NSMutableArray arrayWithCapacity:[colorStrings count]];
for (NSString *hex in colorStrings) {
[colors addObject:[UIColor _colorWithHex:hex]];
}
}
NSMutableArray *gradientColors = [[NSMutableArray alloc] initWithCapacity:[colors count]];
[colors enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) {
[gradientColors addObject:(id)[(UIColor *)object CGColor]];
}];
CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors objectAtIndex:0] CGColor]);
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)gradientColors, NULL);
[gradientColors release];
// Radial
if ([gradientType isEqualToString:@"radial"]) {
CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
CGPoint endPoint = startPoint;
CGFloat startRadius = rect.size.width;
CGFloat endRadius = 0.0f;
CGContextDrawRadialGradient(context, gradient, startPoint, startRadius, endPoint, endRadius, 0);
}
// Linear
else {
CGPoint startPoint = CGPointMake(0.0f, 0.0f);
CGPoint endPoint = CGPointMake(0.0f, rect.size.height);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
}
CGGradientRelease(gradient);
}
// Restore state
if ([[dictionary objectForKey:kSYCompositorPersistentMaskKey] boolValue] == NO) {
CGContextRestoreGState(context);
}
}
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return outputImage;
}
#pragma mark - Private
+ (SYCache *)cache {
static SYCache *cache = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
cache = [[SYCache alloc] initWithName:@"com.syntheticcorp.sycompositor"];
});
return cache;
}
@end