-
Notifications
You must be signed in to change notification settings - Fork 16
/
UIColor.m
284 lines (226 loc) · 7.38 KB
/
UIColor.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
//
// UIColor.m
// UIKit
//
// Created by Shaun Harrison on 7/19/09.
// Copyright 2009 enormego. All rights reserved.
//
#import "UIColor.h"
CGPatternRef CreateImagePattern(CGImageRef image);
CGColorRef CreatePatternColor(CGImageRef image);
@implementation UIColor
// Convenience methods for creating autoreleased colors
+ (UIColor *)colorWithWhite:(CGFloat)white alpha:(CGFloat)alpha {
return [[[UIColor alloc] initWithWhite:white alpha:alpha] autorelease];
}
+ (UIColor *)colorWithHue:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)brightness alpha:(CGFloat)alpha {
return [[[UIColor alloc] initWithHue:hue saturation:saturation brightness:brightness alpha:alpha] autorelease];
}
+ (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha {
return [[[UIColor alloc] initWithRed:red green:green blue:blue alpha:alpha] autorelease];
}
+ (UIColor *)colorWithCGColor:(CGColorRef)cgColor {
return [[[UIColor alloc] initWithCGColor:cgColor] autorelease];
}
+ (UIColor *)colorWithPatternImage:(UIImage *)image {
return [[[UIColor alloc] initWithPatternImage:image] autorelease];
}
+ (UIColor *)colorWithPatternImageRef:(CGImageRef)imageRef {
return [[[UIColor alloc] initWithPatternImageRef:imageRef] autorelease];
}
// Initializers for creating non-autoreleased colors
- (UIColor *)initWithWhite:(CGFloat)white alpha:(CGFloat)alpha {
if((self = [super init])) {
CGColor = CGColorCreateGenericGray(white, alpha);
}
return self;
}
- (UIColor *)initWithHue:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)brightness alpha:(CGFloat)alpha {
hue = roundf(hue * 100) / 100;
saturation = roundf(saturation * 100) / 100;
brightness = roundf(brightness * 100) / 100;
NSInteger hexBrightness = (NSInteger)roundf(brightness * 2.55f);
NSInteger red = 0;
NSInteger green = 0;
NSInteger blue = 0;
if (saturation == 0.0f) {
red = green = blue = hexBrightness;
} else {
NSInteger Hi = floor(hue / 60);
NSInteger f = hue / 60 - Hi;
NSInteger p = round(brightness * (100 - saturation) * .0255);
NSInteger q = round(brightness * (100 - f * saturation) * .0255);
NSInteger t = round(brightness * (100 - (1 - f) * saturation) * .0255);
switch (Hi) {
case 0:
red = hexBrightness;
green = t;
blue = p;
break;
case 1:
red = q;
green = hexBrightness;
blue = p;
break;
case 2:
red = p;
green = hexBrightness;
blue = t;
break;
case 3:
red = p;
green = q;
blue = hexBrightness;
break;
case 4:
red = t;
green = p;
blue = hexBrightness;
break;
case 5:
red = hexBrightness;
green = p;
blue = q;
}
}
return [self initWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:alpha];
}
- (UIColor *)initWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha {
if((self = [super init])) {
CGColor = CGColorCreateGenericRGB(red, green, blue, alpha);
}
return self;
}
- (UIColor *)initWithCGColor:(CGColorRef)cgColor {
if((self = [super init])) {
CGColor = CGColorRetain(cgColor);
}
return self;
}
- (UIColor *)initWithPatternImage:(UIImage*)image {
return nil;
}
- (UIColor *)initWithPatternImageRef:(CGImageRef)imageRef {
if((self = [super init])) {
CGColor = CreatePatternColor(imageRef);
}
return self;
}
+ (UIColor *)blackColor {
return [UIColor colorWithWhite:0.0f alpha:1.0f];
}
+ (UIColor *)darkGrayColor {
return [UIColor colorWithWhite:0.333f alpha:1.0f];
}
+ (UIColor *)lightGrayColor {
return [UIColor colorWithWhite:0.667f alpha:1.0f];
}
+ (UIColor *)whiteColor {
return [UIColor colorWithWhite:1.0f alpha:1.0f];
}
+ (UIColor *)grayColor {
return [UIColor colorWithWhite:0.5f alpha:1.0f];
}
+ (UIColor *)redColor {
return [UIColor colorWithRed:1.0f green:0.0f blue:0.0f alpha:1.0f];
}
+ (UIColor *)greenColor {
return [UIColor colorWithRed:0.0f green:1.0f blue:0.0f alpha:1.0f];
}
+ (UIColor *)blueColor {
return [UIColor colorWithRed:0.0f green:0.0f blue:1.0f alpha:1.0f];
}
+ (UIColor *)cyanColor {
return [UIColor colorWithRed:0.0f green:1.0f blue:1.0f alpha:1.0f];
}
+ (UIColor *)yellowColor {
return [UIColor colorWithRed:1.0f green:1.0f blue:0.0f alpha:1.0f];
}
+ (UIColor *)magentaColor {
return [UIColor colorWithRed:1.0f green:0.0f blue:1.0f alpha:1.0f];
}
+ (UIColor *)orangeColor {
return [UIColor colorWithRed:1.0f green:0.5f blue:0.0f alpha:1.0f];
}
+ (UIColor *)purpleColor {
return [UIColor colorWithRed:0.5f green:0.0f blue:0.5f alpha:1.0f];
}
+ (UIColor *)brownColor {
return [UIColor colorWithRed:0.6f green:0.4f blue:0.2f alpha:1.0f];
}
+ (UIColor *)clearColor {
return [UIColor colorWithWhite:0.0f alpha:0.0f];
}
// Set the color: Sets the fill and stroke colors in the current drawing context. Should be implemented by subclassers.
- (void)set {
// Not sure
}
// Set the fill or stroke colors individually. These should be implemented by subclassers.
- (void)setFill {
// Not sure
}
- (void)setStroke {
// Not sure
}
// Returns a color in the same color space as the receiver with the specified alpha component.
- (UIColor *)colorWithAlphaComponent:(CGFloat)alpha {
CGColorRef cgColor = CGColorCreateCopyWithAlpha(self.CGColor, alpha);
UIColor* color = [UIColor colorWithCGColor:cgColor];
CGColorRelease(cgColor);
return color;
}
- (CGColorRef)CGColor {
return CGColor;
}
- (NSColor*)NSColor {
NSColorSpace* colorSpace = [[[NSColorSpace alloc] initWithCGColorSpace:CGColorGetColorSpace(self.CGColor)] autorelease];
const CGFloat* components = (const CGFloat*)CGColorGetComponents(self.CGColor);
NSInteger numberOfComponents = CGColorGetNumberOfComponents(self.CGColor);
return [NSColor colorWithColorSpace:colorSpace components:components count:numberOfComponents];
}
- (BOOL)isEqual:(UIColor*)aColor {
if(![aColor isKindOfClass:[UIColor class]]) return NO;
return CGColorEqualToColor(self.CGColor, aColor.CGColor);
}
- (void)dealloc {
CGColorRelease(CGColor);
[super dealloc];
}
@end
/*
* @see http://developer.apple.com/mac/library/samplecode/GeekGameBoard/listing38.html
*/
// callback for CreateImagePattern.
static void drawPatternImage (void *info, CGContextRef ctx) {
CGImageRef image = (CGImageRef) info;
CGContextDrawImage(ctx,
CGRectMake(0,0, CGImageGetWidth(image),CGImageGetHeight(image)),
image);
}
// callback for CreateImagePattern.
static void releasePatternImage( void *info ) {
CGImageRelease( (CGImageRef)info );
}
CGPatternRef CreateImagePattern(CGImageRef image) {
NSCParameterAssert(image);
int width = CGImageGetWidth(image);
int height = CGImageGetHeight(image);
static const CGPatternCallbacks callbacks = {0, &drawPatternImage, &releasePatternImage};
return CGPatternCreate (image,
CGRectMake (0, 0, width, height),
CGAffineTransformMake (1, 0, 0, 1, 0, 0),
width,
height,
kCGPatternTilingConstantSpacing,
true,
&callbacks);
}
CGColorRef CreatePatternColor(CGImageRef image) {
CGPatternRef pattern = CreateImagePattern(image);
CGColorSpaceRef space = CGColorSpaceCreatePattern(NULL);
CGFloat components[1] = {1.0};
CGColorRef color = CGColorCreateWithPattern(space, pattern, components);
CGColorSpaceRelease(space);
CGPatternRelease(pattern);
return color;
}