forked from core-plot/core-plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPTColor.m
More file actions
518 lines (418 loc) · 15.2 KB
/
CPTColor.m
File metadata and controls
518 lines (418 loc) · 15.2 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#import "CPTColor.h"
#import "CPTColorSpace.h"
#import "CPTDefinitions.h"
#import "NSCoderExtensions.h"
/** @brief An immutable color.
*
* An immutable object wrapper class around @ref CGColorRef.
* It provides convenience methods to create the same predefined colors defined by
* @if MacOnly NSColor. @endif
* @if iOSOnly UIColor. @endif
**/
@implementation CPTColor
/** @property CGColorRef cgColor
* @brief The @ref CGColorRef to wrap around.
**/
@synthesize cgColor;
/** @property BOOL opaque
* @brief If @YES, the color is completely opaque.
*/
@dynamic opaque;
#pragma mark -
#pragma mark Factory Methods
/** @brief Returns a shared instance of CPTColor initialized with a fully transparent color.
*
* @return A shared CPTColor object initialized with a fully transparent color.
**/
+(instancetype)clearColor
{
static CPTColor *color = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
CGFloat values[4] = { CPTFloat(0.0), CPTFloat(0.0), CPTFloat(0.0), CPTFloat(0.0) };
CGColorRef clear = CGColorCreate([CPTColorSpace genericRGBSpace].cgColorSpace, values);
color = [[CPTColor alloc] initWithCGColor:clear];
CGColorRelease(clear);
});
return color;
}
/** @brief Returns a shared instance of CPTColor initialized with a fully opaque white color.
*
* @return A shared CPTColor object initialized with a fully opaque white color.
**/
+(instancetype)whiteColor
{
static CPTColor *color = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
color = [self colorWithGenericGray:CPTFloat(1.0)];
});
return color;
}
/** @brief Returns a shared instance of CPTColor initialized with a fully opaque 67% gray color.
*
* @return A shared CPTColor object initialized with a fully opaque 67% gray color.
**/
+(instancetype)lightGrayColor
{
static CPTColor *color = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
color = [self colorWithGenericGray:(CGFloat)(2.0 / 3.0)];
});
return color;
}
/** @brief Returns a shared instance of CPTColor initialized with a fully opaque 50% gray color.
*
* @return A shared CPTColor object initialized with a fully opaque 50% gray color.
**/
+(instancetype)grayColor
{
static CPTColor *color = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
color = [self colorWithGenericGray:CPTFloat(0.5)];
});
return color;
}
/** @brief Returns a shared instance of CPTColor initialized with a fully opaque 33% gray color.
*
* @return A shared CPTColor object initialized with a fully opaque 33% gray color.
**/
+(instancetype)darkGrayColor
{
static CPTColor *color = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
color = [self colorWithGenericGray:(CGFloat)(1.0 / 3.0)];
});
return color;
}
/** @brief Returns a shared instance of CPTColor initialized with a fully opaque black color.
*
* @return A shared CPTColor object initialized with a fully opaque black color.
**/
+(instancetype)blackColor
{
static CPTColor *color = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
color = [self colorWithGenericGray:CPTFloat(0.0)];
});
return color;
}
/** @brief Returns a shared instance of CPTColor initialized with a fully opaque red color.
*
* @return A shared CPTColor object initialized with a fully opaque red color.
**/
+(instancetype)redColor
{
static CPTColor *color = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
color = [[CPTColor alloc] initWithComponentRed:CPTFloat(1.0)
green:CPTFloat(0.0)
blue:CPTFloat(0.0)
alpha:CPTFloat(1.0)];
});
return color;
}
/** @brief Returns a shared instance of CPTColor initialized with a fully opaque green color.
*
* @return A shared CPTColor object initialized with a fully opaque green color.
**/
+(instancetype)greenColor
{
static CPTColor *color = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
color = [[CPTColor alloc] initWithComponentRed:CPTFloat(0.0)
green:CPTFloat(1.0)
blue:CPTFloat(0.0)
alpha:CPTFloat(1.0)];
});
return color;
}
/** @brief Returns a shared instance of CPTColor initialized with a fully opaque blue color.
*
* @return A shared CPTColor object initialized with a fully opaque blue color.
**/
+(instancetype)blueColor
{
static CPTColor *color = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
color = [[CPTColor alloc] initWithComponentRed:CPTFloat(0.0)
green:CPTFloat(0.0)
blue:CPTFloat(1.0)
alpha:CPTFloat(1.0)];
});
return color;
}
/** @brief Returns a shared instance of CPTColor initialized with a fully opaque cyan color.
*
* @return A shared CPTColor object initialized with a fully opaque cyan color.
**/
+(instancetype)cyanColor
{
static CPTColor *color = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
color = [[CPTColor alloc] initWithComponentRed:CPTFloat(0.0)
green:CPTFloat(1.0)
blue:CPTFloat(1.0)
alpha:CPTFloat(1.0)];
});
return color;
}
/** @brief Returns a shared instance of CPTColor initialized with a fully opaque yellow color.
*
* @return A shared CPTColor object initialized with a fully opaque yellow color.
**/
+(instancetype)yellowColor
{
static CPTColor *color = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
color = [[CPTColor alloc] initWithComponentRed:CPTFloat(1.0) green:CPTFloat(1.0) blue:CPTFloat(0.0) alpha:CPTFloat(1.0)];
});
return color;
}
/** @brief Returns a shared instance of CPTColor initialized with a fully opaque magenta color.
*
* @return A shared CPTColor object initialized with a fully opaque magenta color.
**/
+(instancetype)magentaColor
{
static CPTColor *color = nil;
if ( nil == color ) {
color = [[CPTColor alloc] initWithComponentRed:CPTFloat(1.0) green:CPTFloat(0.0) blue:CPTFloat(1.0) alpha:CPTFloat(1.0)];
}
return color;
}
/** @brief Returns a shared instance of CPTColor initialized with a fully opaque orange color.
*
* @return A shared CPTColor object initialized with a fully opaque orange color.
**/
+(instancetype)orangeColor
{
static CPTColor *color = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
color = [[CPTColor alloc] initWithComponentRed:CPTFloat(1.0) green:CPTFloat(0.5) blue:CPTFloat(0.0) alpha:CPTFloat(1.0)];
});
return color;
}
/** @brief Returns a shared instance of CPTColor initialized with a fully opaque purple color.
*
* @return A shared CPTColor object initialized with a fully opaque purple color.
**/
+(instancetype)purpleColor
{
static CPTColor *color = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
color = [[CPTColor alloc] initWithComponentRed:CPTFloat(0.5) green:CPTFloat(0.0) blue:CPTFloat(0.5) alpha:CPTFloat(1.0)];
});
return color;
}
/** @brief Returns a shared instance of CPTColor initialized with a fully opaque brown color.
*
* @return A shared CPTColor object initialized with a fully opaque brown color.
**/
+(instancetype)brownColor
{
static CPTColor *color = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
color = [[CPTColor alloc] initWithComponentRed:CPTFloat(0.6) green:CPTFloat(0.4) blue:CPTFloat(0.2) alpha:CPTFloat(1.0)];
});
return color;
}
/** @brief Creates and returns a new CPTColor instance initialized with the provided @ref CGColorRef.
* @param newCGColor The color to wrap.
* @return A new CPTColor instance initialized with the provided @ref CGColorRef.
**/
+(instancetype)colorWithCGColor:(CGColorRef)newCGColor
{
return [[CPTColor alloc] initWithCGColor:newCGColor];
}
/** @brief Creates and returns a new CPTColor instance initialized with the provided RGBA color components.
* @param red The red component (@num{0} ≤ @par{red} ≤ @num{1}).
* @param green The green component (@num{0} ≤ @par{green} ≤ @num{1}).
* @param blue The blue component (@num{0} ≤ @par{blue} ≤ @num{1}).
* @param alpha The alpha component (@num{0} ≤ @par{alpha} ≤ @num{1}).
* @return A new CPTColor instance initialized with the provided RGBA color components.
**/
+(instancetype)colorWithComponentRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha
{
return [[CPTColor alloc] initWithComponentRed:red green:green blue:blue alpha:alpha];
}
/** @brief Creates and returns a new CPTColor instance initialized with the provided gray level.
* @param gray The gray level (@num{0} ≤ @par{gray} ≤ @num{1}).
* @return A new CPTColor instance initialized with the provided gray level.
**/
+(instancetype)colorWithGenericGray:(CGFloat)gray
{
CGFloat values[4] = { gray, gray, gray, CPTFloat(1.0) };
CGColorRef colorRef = CGColorCreate([CPTColorSpace genericRGBSpace].cgColorSpace, values);
CPTColor *color = [[CPTColor alloc] initWithCGColor:colorRef];
CGColorRelease(colorRef);
return color;
}
#pragma mark -
#pragma mark Init/Dealloc
/** @brief Initializes a newly allocated CPTColor object with the provided @ref CGColorRef.
*
* @param newCGColor The color to wrap.
* @return The initialized CPTColor object.
**/
-(instancetype)initWithCGColor:(CGColorRef)newCGColor
{
if ( (self = [super init]) ) {
CGColorRetain(newCGColor);
cgColor = newCGColor;
}
return self;
}
/** @brief Initializes a newly allocated CPTColor object with the provided RGBA color components.
*
* @param red The red component (@num{0} ≤ @par{red} ≤ @num{1}).
* @param green The green component (@num{0} ≤ @par{green} ≤ @num{1}).
* @param blue The blue component (@num{0} ≤ @par{blue} ≤ @num{1}).
* @param alpha The alpha component (@num{0} ≤ @par{alpha} ≤ @num{1}).
* @return The initialized CPTColor object.
**/
-(instancetype)initWithComponentRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha
{
CGFloat colorComponents[4];
colorComponents[0] = red;
colorComponents[1] = green;
colorComponents[2] = blue;
colorComponents[3] = alpha;
CGColorRef color = CGColorCreate([CPTColorSpace genericRGBSpace].cgColorSpace, colorComponents);
self = [self initWithCGColor:color];
CGColorRelease(color);
return self;
}
/// @cond
-(instancetype)init
{
return [self initWithComponentRed:0.0 green:0.0 blue:0.0 alpha:0.0];
}
-(void)dealloc
{
CGColorRelease(cgColor);
}
/// @endcond
#pragma mark -
#pragma mark Creating colors from other colors
/** @brief Creates and returns a new CPTColor instance having color components identical to the current object
* but having the provided alpha component.
* @param alpha The alpha component (@num{0} ≤ @par{alpha} ≤ @num{1}).
* @return A new CPTColor instance having the provided alpha component.
**/
-(instancetype)colorWithAlphaComponent:(CGFloat)alpha
{
CGColorRef newCGColor = CGColorCreateCopyWithAlpha(self.cgColor, alpha);
CPTColor *newColor = [CPTColor colorWithCGColor:newCGColor];
CGColorRelease(newCGColor);
return newColor;
}
#pragma mark -
#pragma mark Opacity
-(BOOL)isOpaque
{
return CGColorGetAlpha(self.cgColor) >= CPTFloat(1.0);
}
#pragma mark -
#pragma mark NSCoding Methods
/// @cond
-(void)encodeWithCoder:(NSCoder *)coder
{
CGColorRef theColor = self.cgColor;
[coder encodeCGColorSpace:CGColorGetColorSpace(theColor) forKey:@"CPTColor.colorSpace"];
size_t numberOfComponents = CGColorGetNumberOfComponents(theColor);
[coder encodeInt64:(int64_t)numberOfComponents forKey:@"CPTColor.numberOfComponents"];
const CGFloat *colorComponents = CGColorGetComponents(theColor);
for ( size_t i = 0; i < numberOfComponents; i++ ) {
NSString *newKey = [[NSString alloc] initWithFormat:@"CPTColor.component[%zu]", i];
[coder encodeCGFloat:colorComponents[i] forKey:newKey];
}
}
/// @endcond
/** @brief Returns an object initialized from data in a given unarchiver.
* @param coder An unarchiver object.
* @return An object initialized from data in a given unarchiver.
*/
-(instancetype)initWithCoder:(NSCoder *)coder
{
if ( (self = [super init]) ) {
CGColorSpaceRef colorSpace = [coder newCGColorSpaceDecodeForKey:@"CPTColor.colorSpace"];
size_t numberOfComponents = (size_t)[coder decodeInt64ForKey : @"CPTColor.numberOfComponents"];
CGFloat *colorComponents = malloc( numberOfComponents * sizeof(CGFloat) );
for ( size_t i = 0; i < numberOfComponents; i++ ) {
NSString *newKey = [[NSString alloc] initWithFormat:@"CPTColor.component[%zu]", i];
colorComponents[i] = [coder decodeCGFloatForKey:newKey];
}
CGColorRef color = CGColorCreate(colorSpace, colorComponents);
cgColor = color;
CGColorSpaceRelease(colorSpace);
free(colorComponents);
}
return self;
}
#pragma mark -
#pragma mark NSCopying Methods
/// @cond
-(id)copyWithZone:(NSZone *)zone
{
CGColorRef cgColorCopy = NULL;
CGColorRef myColor = self.cgColor;
if ( myColor ) {
cgColorCopy = CGColorCreateCopy(myColor);
}
CPTColor *colorCopy = [[[self class] allocWithZone:zone] initWithCGColor:cgColorCopy];
CGColorRelease(cgColorCopy);
return colorCopy;
}
/// @endcond
#pragma mark -
#pragma mark Color comparison
/// @name Comparison
/// @{
/** @brief Returns a boolean value that indicates whether the received is equal to the given object.
* Colors are equal if they have equal @ref cgColor properties.
* @param object The object to be compared with the receiver.
* @return @YES if @par{object} is equal to the receiver, @NO otherwise.
**/
-(BOOL)isEqual:(id)object
{
if ( self == object ) {
return YES;
}
else if ( [object isKindOfClass:[self class]] ) {
return CGColorEqualToColor(self.cgColor, ( (CPTColor *)object ).cgColor);
}
else {
return NO;
}
}
/// @}
/// @cond
-(NSUInteger)hash
{
// Equal objects must hash the same.
CGFloat theHash = CPTFloat(0.0);
CGFloat multiplier = CPTFloat(256.0);
CGColorRef theColor = self.cgColor;
size_t numberOfComponents = CGColorGetNumberOfComponents(theColor);
const CGFloat *colorComponents = CGColorGetComponents(theColor);
for ( NSUInteger i = 0; i < numberOfComponents; i++ ) {
theHash += multiplier * colorComponents[i];
multiplier *= CPTFloat(256.0);
}
return (NSUInteger)theHash;
}
/// @endcond
@end