-
Notifications
You must be signed in to change notification settings - Fork 0
/
MNLColor.m
870 lines (763 loc) · 33.9 KB
/
MNLColor.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
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
#import "MNLColorInternal.h"
#import <CoreGraphics/CGColorSpace.h>
static MNLColor *MNLClearColor;
static MNLColorSpace MNLDefaultMonochromeColorSpace = MNLColorSpaceGenericGrayGamma2_2;
static MNLColorSpace MNLDefaultRGBColorSpace = MNLColorSpaceSRGB;
static MNLColorSpace MNLDefaultCMYKColorSpace = MNLColorSpaceGenericCMYK;
const CFStringRef CGColorSpaceNameForMNLColorSpace(MNLColorSpace);
MNLColorSpace MNLColorSpaceForCGColorSpaceName(const CFStringRef);
@implementation MNLColor
{
CGColorRef _color;
}
+ (BOOL)supportsSecureCoding {
return YES;
}
+ (MNLColorSpace)defaultMonochromeColorSpace {
return MNLDefaultMonochromeColorSpace;
}
+ (void)setDefaultMonochromeColorSpace:(MNLColorSpace)defaultMonochromeColorSpace {
MNLDefaultMonochromeColorSpace = defaultMonochromeColorSpace;
}
+ (MNLColorSpace)defaultRGBColorSpace {
return MNLDefaultRGBColorSpace;
}
+ (void)setDefaultRGBColorSpace:(MNLColorSpace)defaultRGBColorSpace {
MNLDefaultRGBColorSpace = defaultRGBColorSpace;
}
+ (MNLColorSpace)defaultCMYKColorSpace {
return MNLDefaultCMYKColorSpace;
}
+ (void)setDefaultCMYKColorSpace:(MNLColorSpace)defaultCMYKColorSpace {
MNLDefaultCMYKColorSpace = defaultCMYKColorSpace;
}
+ (MNLColor *)clearColor {
MNLReturnSingletonColor([[MNLColor alloc] init], MNLClearColor);
}
+ (MNLColor *)colorWithWhite:(CGFloat)white alpha:(CGFloat)alpha colorSpace:(MNLColorSpace)colorSpace {
return [[(MNLColor *)[self alloc] initWithWhite:white alpha:alpha colorSpace:colorSpace] autorelease];
}
+ (MNLColor *)colorWithWhite:(CGFloat)white colorSpace:(MNLColorSpace)colorSpace {
return [[(MNLColor *)[self alloc] initWithWhite:white colorSpace:colorSpace] autorelease];
}
+ (MNLColor *)colorWithWhite:(CGFloat)white alpha:(CGFloat)alpha {
return [[(MNLColor *)[self alloc] initWithWhite:white alpha:alpha] autorelease];
}
+ (MNLColor *)colorWithWhite:(CGFloat)white {
return [[(MNLColor *)[self alloc] initWithWhite:white] autorelease];
}
+ (MNLColor *)colorWithHexadecimalWhite:(NSInteger)white alpha:(CGFloat)alpha colorSpace:(MNLColorSpace)colorSpace {
return [[(MNLColor *)[self alloc] initWithHexadecimalWhite:white alpha:alpha colorSpace:colorSpace] autorelease];
}
+ (MNLColor *)colorWithHexadecimalWhite:(NSInteger)white colorSpace:(MNLColorSpace)colorSpace {
return [[(MNLColor *)[self alloc] initWithHexadecimalWhite:white colorSpace:colorSpace] autorelease];
}
+ (MNLColor *)colorWithHexadecimalWhite:(NSInteger)white alpha:(CGFloat)alpha {
return [[(MNLColor *)[self alloc] initWithHexadecimalWhite:white alpha:alpha] autorelease];
}
+ (MNLColor *)colorWithHexadecimalWhite:(NSInteger)white {
return [[(MNLColor *)[self alloc] initWithHexadecimalWhite:white] autorelease];
}
+ (MNLColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha colorSpace:(MNLColorSpace)colorSpace {
return [[(MNLColor *)[self alloc] initWithRed:red green:green blue:blue alpha:alpha colorSpace:colorSpace] autorelease];
}
+ (MNLColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue colorSpace:(MNLColorSpace)colorSpace {
return [[(MNLColor *)[self alloc] initWithRed:red green:green blue:blue colorSpace:colorSpace] autorelease];
}
+ (MNLColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha {
return [[(MNLColor *)[self alloc] initWithRed:red green:green blue:blue alpha:alpha] autorelease];
}
+ (MNLColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue {
return [[(MNLColor *)[self alloc] initWithRed:red green:green blue:blue] autorelease];
}
+ (MNLColor *)colorWithHexadecimalRed:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue alpha:(CGFloat)alpha colorSpace:(MNLColorSpace)colorSpace {
return [[(MNLColor *)[self alloc] initWithHexadecimalRed:red green:green blue:blue alpha:alpha colorSpace:colorSpace] autorelease];
}
+ (MNLColor *)colorWithHexadecimalRed:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue colorSpace:(MNLColorSpace)colorSpace {
return [[(MNLColor *)[self alloc] initWithHexadecimalRed:red green:green blue:blue colorSpace:colorSpace] autorelease];
}
+ (MNLColor *)colorWithHexadecimalRed:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue alpha:(CGFloat)alpha {
return [[(MNLColor *)[self alloc] initWithHexadecimalRed:red green:green blue:blue alpha:alpha] autorelease];
}
+ (MNLColor *)colorWithHexadecimalRed:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue {
return [[(MNLColor *)[self alloc] initWithHexadecimalRed:red green:green blue:blue] autorelease];
}
+ (MNLColor *)colorWithCyan:(CGFloat)cyan magenta:(CGFloat)magenta yellow:(CGFloat)yellow black:(CGFloat)black alpha:(CGFloat)alpha colorSpace:(MNLColorSpace)colorSpace {
return [[(MNLColor *)[self alloc] initWithCyan:cyan magenta:magenta yellow:yellow black:black alpha:alpha colorSpace:colorSpace] autorelease];
}
+ (MNLColor *)colorWithCyan:(CGFloat)cyan magenta:(CGFloat)magenta yellow:(CGFloat)yellow black:(CGFloat)black colorSpace:(MNLColorSpace)colorSpace {
return [[(MNLColor *)[self alloc] initWithCyan:cyan magenta:magenta yellow:yellow black:black colorSpace:colorSpace] autorelease];
}
+ (MNLColor *)colorWithCyan:(CGFloat)cyan magenta:(CGFloat)magenta yellow:(CGFloat)yellow black:(CGFloat)black alpha:(CGFloat)alpha {
return [[(MNLColor *)[self alloc] initWithCyan:cyan magenta:magenta yellow:yellow black:black alpha:alpha] autorelease];
}
+ (MNLColor *)colorWithCyan:(CGFloat)cyan magenta:(CGFloat)magenta yellow:(CGFloat)yellow black:(CGFloat)black {
return [[(MNLColor *)[self alloc] initWithCyan:cyan magenta:magenta yellow:yellow black:black] autorelease];
}
+ (MNLColor *)colorWithCGColor:(CGColorRef)cgColor {
return [[(MNLColor *)[self alloc] initWithCGColor:cgColor] autorelease];
}
+ (MNLColor *)colorWithColor:(MNLColor *)color {
return [[color retain] autorelease];
}
- (CGColorRef)CGColor {
return _color;
}
- (BOOL)isMonochrome {
CGColorRetain(_color);
CGColorSpaceRef cgColorSpace = CGColorGetColorSpace(_color);
CGColorRelease(_color);
CGColorSpaceModel cgColorSpaceModel = CGColorSpaceGetModel(cgColorSpace);
return cgColorSpaceModel == kCGColorSpaceModelMonochrome;
}
- (BOOL)isRGB {
CGColorRetain(_color);
CGColorSpaceRef cgColorSpace = CGColorGetColorSpace(_color);
CGColorRelease(_color);
CGColorSpaceModel cgColorSpaceModel = CGColorSpaceGetModel(cgColorSpace);
return cgColorSpaceModel == kCGColorSpaceModelRGB;
}
- (BOOL)isCMYK {
CGColorRetain(_color);
CGColorSpaceRef cgColorSpace = CGColorGetColorSpace(_color);
CGColorRelease(_color);
CGColorSpaceModel cgColorSpaceModel = CGColorSpaceGetModel(cgColorSpace);
return cgColorSpaceModel == kCGColorSpaceModelCMYK;
}
- (MNLColorSpace)colorSpace {
CGColorRetain(_color);
CGColorSpaceRef cgColorSpace = CGColorGetColorSpace(_color);
CGColorRelease(_color);
CFStringRef colorSpaceName = CGColorSpaceCopyName(cgColorSpace);
MNLColorSpace colorSpace;
if (colorSpaceName) {
colorSpace = MNLColorSpaceForCGColorSpaceName(colorSpaceName);
CFRelease(colorSpaceName);
}
else
colorSpace = MNLColorSpaceUnknown;
return colorSpace;
}
- (NSUInteger)numberOfComponents {
CGColorRetain(_color);
size_t numberOfComponents = CGColorGetNumberOfComponents(_color) + (/* alpha */ 1);
CGColorRelease(_color);
return numberOfComponents;
}
- (instancetype)init {
CGColorSpaceRef cgColorSpace = CGColorSpaceCreateDeviceGray();
CGFloat components[] = {0, 0};
if (self = [super init])
_color = CGColorCreate(cgColorSpace, components);
CGColorSpaceRelease(cgColorSpace);
return self;
}
- (instancetype)initWithWhite:(CGFloat)white alpha:(CGFloat)alpha colorSpace:(MNLColorSpace)colorSpace {
const CFStringRef cgColorSpaceName = CGColorSpaceNameForMNLColorSpace(colorSpace);
CGColorSpaceRef cgColorSpace;
if (cgColorSpaceName)
cgColorSpace = CGColorSpaceCreateWithName(cgColorSpaceName);
else
switch (colorSpace) {
case MNLColorSpaceDeviceGray:
cgColorSpace = CGColorSpaceCreateDeviceGray();
break;
case MNLColorSpaceDeviceRGB:
cgColorSpace = CGColorSpaceCreateDeviceRGB();
break;
case MNLColorSpaceDeviceCMYK:
cgColorSpace = CGColorSpaceCreateDeviceCMYK();
break;
default:
[self release];
return nil;
}
if (CGColorSpaceGetModel(cgColorSpace) != kCGColorSpaceModelMonochrome) {
CGColorSpaceRelease(cgColorSpace);
[self release];
return nil;
}
CGFloat components[] = {white, alpha};
if (self = [super init])
_color = CGColorCreate(cgColorSpace, components);
CGColorSpaceRelease(cgColorSpace);
return self;
}
- (instancetype)initWithWhite:(CGFloat)white colorSpace:(MNLColorSpace)colorSpace {
return [self initWithWhite:white alpha:1 colorSpace:colorSpace];
}
- (instancetype)initWithWhite:(CGFloat)white alpha:(CGFloat)alpha {
return [self initWithWhite:white alpha:alpha colorSpace:[[self class] defaultMonochromeColorSpace]];
}
- (instancetype)initWithWhite:(CGFloat)white {
return [self initWithWhite:white alpha:1 colorSpace:[[self class] defaultMonochromeColorSpace]];
}
- (instancetype)initWithHexadecimalWhite:(NSInteger)white alpha:(CGFloat)alpha colorSpace:(MNLColorSpace)colorSpace {
CFStringRef cgColorSpaceName = CGColorSpaceNameForMNLColorSpace(colorSpace);
CGColorSpaceRef cgColorSpace;
if (cgColorSpaceName)
cgColorSpace = CGColorSpaceCreateWithName(cgColorSpaceName);
else
switch (colorSpace) {
case MNLColorSpaceDeviceGray:
cgColorSpace = CGColorSpaceCreateDeviceGray();
break;
case MNLColorSpaceDeviceRGB:
cgColorSpace = CGColorSpaceCreateDeviceRGB();
break;
case MNLColorSpaceDeviceCMYK:
cgColorSpace = CGColorSpaceCreateDeviceCMYK();
break;
default:
[self release];
return nil;
}
if (CGColorSpaceGetModel(cgColorSpace) != kCGColorSpaceModelMonochrome) {
CGColorSpaceRelease(cgColorSpace);
[self release];
return nil;
}
CGFloat components[] = {white / (CGFloat)255, alpha};
if (self = [super init])
_color = CGColorCreate(cgColorSpace, components);
CGColorSpaceRelease(cgColorSpace);
return self;
}
- (instancetype)initWithHexadecimalWhite:(NSInteger)white colorSpace:(MNLColorSpace)colorSpace {
return [self initWithHexadecimalWhite:white alpha:1 colorSpace:colorSpace];
}
- (instancetype)initWithHexadecimalWhite:(NSInteger)white alpha:(CGFloat)alpha {
return [self initWithHexadecimalWhite:white alpha:alpha colorSpace:[[self class] defaultMonochromeColorSpace]];
}
- (instancetype)initWithHexadecimalWhite:(NSInteger)white {
return [self initWithHexadecimalWhite:white alpha:1 colorSpace:[[self class] defaultMonochromeColorSpace]];
}
- (instancetype)initWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha colorSpace:(MNLColorSpace)colorSpace {
CFStringRef cgColorSpaceName = CGColorSpaceNameForMNLColorSpace(colorSpace);
CGColorSpaceRef cgColorSpace;
if (cgColorSpaceName)
cgColorSpace = CGColorSpaceCreateWithName(cgColorSpaceName);
else
switch (colorSpace) {
case MNLColorSpaceDeviceGray:
cgColorSpace = CGColorSpaceCreateDeviceGray();
break;
case MNLColorSpaceDeviceRGB:
cgColorSpace = CGColorSpaceCreateDeviceRGB();
break;
case MNLColorSpaceDeviceCMYK:
cgColorSpace = CGColorSpaceCreateDeviceCMYK();
break;
default:
[self release];
return nil;
}
if (CGColorSpaceGetModel(cgColorSpace) != kCGColorSpaceModelRGB) {
CGColorSpaceRelease(cgColorSpace);
[self release];
return nil;
}
CGFloat components[] = {red, green, blue, alpha};
if (self = [super init])
_color = CGColorCreate(cgColorSpace, components);
CGColorSpaceRelease(cgColorSpace);
return self;
}
- (instancetype)initWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue colorSpace:(MNLColorSpace)colorSpace {
return [self initWithRed:red green:green blue:blue alpha:1 colorSpace:colorSpace];
}
- (instancetype)initWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha {
return [self initWithRed:red green:green blue:blue alpha:alpha colorSpace:[[self class] defaultRGBColorSpace]];
}
- (instancetype)initWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue {
return [self initWithRed:red green:green blue:blue alpha:1 colorSpace:[[self class] defaultRGBColorSpace]];
}
- (instancetype)initWithHexadecimalRed:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue alpha:(CGFloat)alpha colorSpace:(MNLColorSpace)colorSpace {
CFStringRef cgColorSpaceName = CGColorSpaceNameForMNLColorSpace(colorSpace);
CGColorSpaceRef cgColorSpace;
if (cgColorSpaceName)
cgColorSpace = CGColorSpaceCreateWithName(cgColorSpaceName);
else
switch (colorSpace) {
case MNLColorSpaceDeviceGray:
cgColorSpace = CGColorSpaceCreateDeviceGray();
break;
case MNLColorSpaceDeviceRGB:
cgColorSpace = CGColorSpaceCreateDeviceRGB();
break;
case MNLColorSpaceDeviceCMYK:
cgColorSpace = CGColorSpaceCreateDeviceCMYK();
break;
default:
[self release];
return nil;
}
if (CGColorSpaceGetModel(cgColorSpace) != kCGColorSpaceModelRGB) {
CGColorSpaceRelease(cgColorSpace);
[self release];
return nil;
}
CGFloat components[] = {red / (CGFloat)255, green / (CGFloat)255, blue / (CGFloat)255, alpha};
if (self = [super init])
_color = CGColorCreate(cgColorSpace, components);
CGColorSpaceRelease(cgColorSpace);
return self;
}
- (instancetype)initWithHexadecimalRed:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue colorSpace:(MNLColorSpace)colorSpace {
return [self initWithHexadecimalRed:red green:green blue:blue alpha:1 colorSpace:colorSpace];
}
- (instancetype)initWithHexadecimalRed:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue alpha:(CGFloat)alpha {
return [self initWithHexadecimalRed:red green:green blue:blue alpha:alpha colorSpace:[[self class] defaultRGBColorSpace]];
}
- (instancetype)initWithHexadecimalRed:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue {
return [self initWithHexadecimalRed:red green:green blue:blue alpha:1 colorSpace:[[self class] defaultRGBColorSpace]];
}
- (instancetype)initWithCyan:(CGFloat)cyan magenta:(CGFloat)magenta yellow:(CGFloat)yellow black:(CGFloat)black alpha:(CGFloat)alpha colorSpace:(MNLColorSpace)colorSpace {
CFStringRef cgColorSpaceName = CGColorSpaceNameForMNLColorSpace(colorSpace);
CGColorSpaceRef cgColorSpace;
if (cgColorSpaceName)
cgColorSpace = CGColorSpaceCreateWithName(cgColorSpaceName);
else
switch (colorSpace) {
case MNLColorSpaceDeviceGray:
cgColorSpace = CGColorSpaceCreateDeviceGray();
break;
case MNLColorSpaceDeviceRGB:
cgColorSpace = CGColorSpaceCreateDeviceRGB();
break;
case MNLColorSpaceDeviceCMYK:
cgColorSpace = CGColorSpaceCreateDeviceCMYK();
break;
default:
[self release];
return nil;
}
if (CGColorSpaceGetModel(cgColorSpace) != kCGColorSpaceModelCMYK) {
CGColorSpaceRelease(cgColorSpace);
[self release];
return nil;
}
CGFloat components[] = {cyan, magenta, yellow, black, alpha};
if (self = [super init])
_color = CGColorCreate(cgColorSpace, components);
CGColorSpaceRelease(cgColorSpace);
return self;
}
- (instancetype)initWithCyan:(CGFloat)cyan magenta:(CGFloat)magenta yellow:(CGFloat)yellow black:(CGFloat)black colorSpace:(MNLColorSpace)colorSpace {
return [self initWithCyan:cyan magenta:magenta yellow:yellow black:black alpha:1 colorSpace:colorSpace];
}
- (instancetype)initWithCyan:(CGFloat)cyan magenta:(CGFloat)magenta yellow:(CGFloat)yellow black:(CGFloat)black alpha:(CGFloat)alpha {
return [self initWithCyan:cyan magenta:magenta yellow:yellow black:black alpha:alpha colorSpace:[[self class] defaultCMYKColorSpace]];
}
- (instancetype)initWithCyan:(CGFloat)cyan magenta:(CGFloat)magenta yellow:(CGFloat)yellow black:(CGFloat)black {
return [self initWithCyan:cyan magenta:magenta yellow:yellow black:black alpha:1 colorSpace:[[self class] defaultCMYKColorSpace]];
}
- (instancetype)initWithColorSpace:(MNLColorSpace)colorSpace, ... {
CFStringRef cgColorSpaceName = CGColorSpaceNameForMNLColorSpace(colorSpace);
CGColorSpaceRef cgColorSpace;
if (cgColorSpaceName)
cgColorSpace = CGColorSpaceCreateWithName(cgColorSpaceName);
else
switch (colorSpace) {
case MNLColorSpaceDeviceGray:
cgColorSpace = CGColorSpaceCreateDeviceGray();
break;
case MNLColorSpaceDeviceRGB:
cgColorSpace = CGColorSpaceCreateDeviceRGB();
break;
case MNLColorSpaceDeviceCMYK:
cgColorSpace = CGColorSpaceCreateDeviceCMYK();
break;
default:
[self release];
return nil;
}
size_t numberOfComponents = CGColorSpaceGetNumberOfComponents(cgColorSpace);
CGFloat *components = calloc(numberOfComponents, sizeof(CGFloat));
if (!components) {
CGColorSpaceRelease(cgColorSpace);
[self release];
return nil;
}
CGFloat *component = components;
va_list args;
va_start(args, colorSpace);
for (int i = 0; i <= numberOfComponents; i++)
*component++ = va_arg(args, CGFloat);
va_end(args);
if (self = [super init])
if (!(_color = CGColorCreate(cgColorSpace, components))) {
free(components);
CGColorSpaceRelease(cgColorSpace);
[self release];
return nil;
}
free(components);
CGColorSpaceRelease(cgColorSpace);
return self;
}
- (instancetype)initWithCGColor:(CGColorRef)cgColor {
if (self = [super init])
_color = CGColorRetain(cgColor);
return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder {
if (self = [super init]) {
MNLColorSpace colorSpace = [coder decodeIntegerForKey:@"colorSpace"];
CFStringRef cgColorSpaceName = CGColorSpaceNameForMNLColorSpace(colorSpace);
CGColorSpaceRef cgColorSpace = CGColorSpaceCreateWithName(cgColorSpaceName);
if (!cgColorSpace) {
[self release];
return nil;
}
CFArrayRef componentsArray = [coder decodeObjectOfClass:[NSArray class] forKey:@"components"];
CFIndex componentsCount = CFArrayGetCount(componentsArray);
CFNumberRef *componentNumbers = malloc(componentsCount * sizeof(CFNumberRef));
CGFloat *cgColorComponents = malloc(componentsCount * sizeof(CGFloat));
CFArrayGetValues(componentsArray, CFRangeMake(0, componentsCount), componentNumbers);
CFIndex i;
for (i = 0; i < componentsCount; i++)
CFNumberGetValue(componentNumbers[i], kCFNumberCGFloatType, cgColorComponents + i);
free(componentNumbers);
_color = CGColorCreate(cgColorSpace, cgColorComponents);
CGColorSpaceRelease(cgColorSpace);
free(cgColorComponents);
if (!_color) {
[self release];
return nil;
}
}
return self;
}
- (void)dealloc {
CGColorRelease(_color);
[super dealloc];
}
- (id)copyWithZone:(NSZone *)zone {
if (zone) {
CGColorRetain(_color);
id copy = [[[self class] allocWithZone:zone] initWithCGColor:_color];
CGColorRelease(_color);
return copy;
}
else
return [self retain];
}
- (void)encodeWithCoder:(NSCoder *)coder {
[coder retain];
CGColorRetain(_color);
CGColorSpaceRef cgColorSpace = CGColorGetColorSpace(_color);
CFStringRef cgColorSpaceName = CGColorSpaceCopyName(cgColorSpace);
MNLColorSpace colorSpace = MNLColorSpaceForCGColorSpaceName(cgColorSpaceName);
CFRelease(cgColorSpaceName);
size_t numberOfComponents = CGColorGetNumberOfComponents(_color) + (/* alpha */ 1);
const CGFloat *cgColorComponents = CGColorGetComponents(_color);
[coder encodeInteger:colorSpace forKey:@"colorSpace"];
CFMutableArrayRef componentsArray = CFArrayCreateMutable(kCFAllocatorDefault, numberOfComponents, &kCFTypeArrayCallBacks);
while (numberOfComponents-- > 0) {
CFNumberRef componentNumber = CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, cgColorComponents++);
CFArrayAppendValue(componentsArray, &componentNumber);
}
CGColorRelease(_color);
[coder encodeObject:componentsArray forKey:@"components"];
CFRelease(componentsArray);
[coder release];
}
- (BOOL)monochromeColorComponents:(MNLMonochromeColorComponents *)components {
if (![self isMonochrome])
return NO;
CGColorRetain(_color);
const CGFloat *cgComponents = CGColorGetComponents(_color);
memcpy(components, cgComponents, sizeof(CGFloat) * (CGColorGetNumberOfComponents(_color) + (/* alpha */ 1)));
CGColorRelease(_color);
return YES;
}
- (BOOL)RGBColorComponents:(MNLRGBColorComponents *)components {
if (![self isRGB])
return NO;
CGColorRetain(_color);
const CGFloat *cgComponents = CGColorGetComponents(_color);
memcpy(components, cgComponents, sizeof(CGFloat) * (CGColorGetNumberOfComponents(_color) + (/* alpha */ 1)));
CGColorRelease(_color);
return YES;
}
- (BOOL)CMYKColorComponents:(MNLCMYKColorComponents *)components {
if (![self isCMYK])
return NO;
CGColorRetain(_color);
const CGFloat *cgComponents = CGColorGetComponents(_color);
memcpy(components, cgComponents, sizeof(CGFloat) * (CGColorGetNumberOfComponents(_color) + (/* alpha */ 1)));
CGColorRelease(_color);
return YES;
}
- (instancetype)colorWithAlpha:(CGFloat)alpha {
CGColorRetain(_color);
CGColorRef cgColor = CGColorCreateCopyWithAlpha(_color, alpha);
CGColorRelease(_color);
id color = [[[self class] alloc] initWithCGColor:cgColor];
CGColorRelease(cgColor);
return [color autorelease];
}
- (instancetype)colorWithColorSpace:(MNLColorSpace)colorSpace renderingIntent:(MNLColorRenderingIntent)renderingIntent {
CGColorSpaceRef cgColorSpace = CGColorSpaceCreateWithName(CGColorSpaceNameForMNLColorSpace(colorSpace));
CGColorRenderingIntent cgColorRenderingIntent;
switch (renderingIntent) {
case MNLColorRenderingIntentDefault:
cgColorRenderingIntent = kCGRenderingIntentDefault;
break;
case MNLColorRenderingIntentAbsoluteColorimetric:
cgColorRenderingIntent = kCGRenderingIntentAbsoluteColorimetric;
break;
case MNLColorRenderingIntentRelativeColorimetric:
cgColorRenderingIntent = kCGRenderingIntentRelativeColorimetric;
break;
case MNLColorRenderingIntentPerceptual:
cgColorRenderingIntent = kCGRenderingIntentPerceptual;
break;
case MNLColorRenderingIntentSaturation:
cgColorRenderingIntent = kCGRenderingIntentSaturation;
}
CGColorRetain(_color);
CGColorRef cgColor = CGColorCreateCopyByMatchingToColorSpace(cgColorSpace, cgColorRenderingIntent, _color, NULL);
CGColorRelease(_color);
CGColorSpaceRelease(cgColorSpace);
id color = [[[self class] alloc] initWithCGColor:cgColor];
CGColorRelease(cgColor);
return [color autorelease];
}
- (void)getMonochromeColorComponents:(MNLMonochromeColorComponents *)components {
static MNLMonochromeColorComponents nac = {NAN, NAN};
if (![self isMonochrome]) {
*components = nac;
return;
}
CGColorRetain(_color);
const CGFloat *cgComponents = CGColorGetComponents(_color);
memcpy(components, cgComponents, sizeof(CGFloat) * 2);
CGColorRelease(_color);
}
- (void)getRGBColorComponents:(MNLRGBColorComponents *)components {
static MNLRGBColorComponents nac = {NAN, NAN, NAN, NAN};
if (![self isRGB]) {
*components = nac;
return;
}
CGColorRetain(_color);
const CGFloat *cgComponents = CGColorGetComponents(_color);
memcpy(components, cgComponents, sizeof(CGFloat) * 4);
CGColorRelease(_color);
}
- (void)getCMYKColorComponents:(MNLCMYKColorComponents *)components {
static MNLCMYKColorComponents nac = {NAN, NAN, NAN, NAN, NAN};
if (![self isCMYK]) {
*components = nac;
return;
}
CGColorRetain(_color);
const CGFloat *cgComponents = CGColorGetComponents(_color);
memcpy(components, cgComponents, sizeof(CGFloat) * 5);
CGColorRelease(_color);
}
- (void)getColorComponents:(CGFloat *)components {
CGColorRetain(_color);
size_t numberOfComponents = CGColorGetNumberOfComponents(_color) + (/* alpha */ 1);
const CGFloat *cgColorComponents = CGColorGetComponents(_color);
memcpy(components, cgColorComponents, numberOfComponents * sizeof(CGFloat));
CGColorRelease(_color);
}
@end
#if !TARGET_OS_WATCH
@implementation MNLColor (CIColor)
+ (MNLColor *)colorWithCIColor:(CIColor *)ciColor {
return [[[MNLColor alloc] initWithCIColor:ciColor] autorelease];
}
- (CIColor *)CIColor {
return [CIColor colorWithCGColor:_color];
}
- (instancetype)initWithCIColor:(CIColor *)ciColor {
if (self = [super init])
_color = CGColorCreate([ciColor colorSpace], [ciColor components]);
return self;
}
@end
#endif
#if TARGET_OS_IPHONE // TARGET_OS_IOS | TARGET_OS_TV | TARGET_OS_WATCH
@implementation MNLColor (UIColor)
+ (MNLColor *)colorWithUIColor:(UIColor *)uiColor {
return [[[MNLColor alloc] initWithUIColor:uiColor] autorelease];
}
- (UIColor *)UIColor {
return [UIColor colorWithCGColor:_color];
}
- (instancetype)initWithUIColor:(UIColor *)uiColor {
if (self = [super init])
_color = CGColorCreateCopy([uiColor CGColor]);
return self;
}
@end
#endif
#if TARGET_OS_OSX
@implementation MNLColor (NSColor)
+ (MNLColor *)colorWithNSColor:(NSColor *)nsColor {
return [[[MNLColor alloc] initWithNSColor:nsColor] autorelease];
}
- (NSColor *)NSColor {
return [NSColor colorWithCGColor:_color];
}
- (instancetype )initWithNSColor:(NSColor *)nsColor {
if (self = [super init])
_color = CGColorCreateCopy([nsColor CGColor]);
return self;
}
@end
#endif
const CFStringRef CGColorSpaceNameForMNLColorSpace(MNLColorSpace cs) {
if (@available(iOS 9.0, macOS 10.4, tvOS 9.0, watchOS 2.0, macCatalyst 13.0, *))
switch (cs) {
case MNLColorSpaceGenericGray:
return kCGColorSpaceGenericGray;
case MNLColorSpaceGenericRGB:
return kCGColorSpaceGenericRGB;
case MNLColorSpaceGenericCMYK:
return kCGColorSpaceGenericCMYK;
}
if (@available(iOS 9.0, macOS 10.5, tvOS 9.0, watchOS 2.0, macCatalyst 13.0, *))
switch (cs) {
case MNLColorSpaceGenericRGBLinear:
return kCGColorSpaceGenericRGBLinear;
case MNLColorSpaceAdobeRGB1998:
return kCGColorSpaceAdobeRGB1998;
case MNLColorSpaceSRGB:
return kCGColorSpaceSRGB;
}
if (@available(iOS 9.0, macOS 10.6, tvOS 9.0, watchOS 2.0, macCatalyst 13.0, *))
switch (cs) {
case MNLColorSpaceGenericGrayGamma2_2:
return kCGColorSpaceGenericGrayGamma2_2;
}
if (@available(iOS 9.0, macOS 10.11, tvOS 9.0, watchOS 2.0, macCatalyst 13.0, *))
switch (cs) {
case MNLColorSpaceGenericXYZ:
return kCGColorSpaceGenericXYZ;
case MNLColorSpaceACESCGLinear:
return kCGColorSpaceACESCGLinear;
case MNLColorSpaceITUR_709:
return kCGColorSpaceITUR_709;
case MNLColorSpaceITUR_2020:
return kCGColorSpaceITUR_2020;
case MNLColorSpaceROMMRGB:
return kCGColorSpaceROMMRGB;
case MNLColorSpaceDCIP3:
return kCGColorSpaceDCIP3;
}
if (@available(iOS 9.3, macOS 10.11.2, tvOS 9.3, watchOS 2.3, macCatalyst 13.0, *))
switch (cs) {
case MNLColorSpaceDisplayP3:
return kCGColorSpaceDisplayP3;
}
if (@available(iOS 10.0, macOS 10.12, tvOS 10.0, watchOS 3.0, macCatalyst 13.0, *))
switch (cs) {
case MNLColorSpaceLinearGray:
return kCGColorSpaceLinearGray;
case MNLColorSpaceExtendedSRGB:
return kCGColorSpaceExtendedSRGB;
case MNLColorSpaceLinearSRGB:
return kCGColorSpaceLinearSRGB;
case MNLColorSpaceExtendedLinearSRGB:
return kCGColorSpaceExtendedLinearSRGB;
case MNLColorSpaceExtendedGray:
return kCGColorSpaceExtendedGray;
case MNLColorSpaceExtendedLinearGray:
return kCGColorSpaceExtendedLinearGray;
}
if (@available(iOS 12.3, macOS 10.14.3, tvOS 12.3, watchOS 5.3, macCatalyst 13.0, *))
switch (cs) {
case MNLColorSpaceExtendedLinearDisplayP3:
return kCGColorSpaceExtendedLinearDisplayP3;
case MNLColorSpaceExtendedLinearITUR_2020:
return kCGColorSpaceExtendedLinearITUR_2020;
}
if (@available(iOS 13.0, macOS 10.14.6, tvOS 13.0, watchOS 6.0, macCatalyst 13.0, *))
switch (cs) {
case MNLColorSpaceDisplayP3_PQ_EOTF:
return kCGColorSpaceDisplayP3_PQ_EOTF;
case MNLColorSpaceDisplayP3_HLG:
return kCGColorSpaceDisplayP3_HLG;
case MNLColorSpaceITUR_2020_PQ_EOTF:
return kCGColorSpaceITUR_2020_PQ_EOTF;
case MNLColorSpaceITUR_2020_HLG:
return kCGColorSpaceITUR_2020_HLG;
}
return NULL;
}
MNLColorSpace MNLColorSpaceForCGColorSpaceName(const CFStringRef s) {
if (@available(iOS 9.0, macOS 10.4, tvOS 9.0, watchOS 2.0, macCatalyst 13.0, *)) {
if (CFStringCompare(s, kCGColorSpaceGenericGray, kNilOptions) == kCFCompareEqualTo)
return MNLColorSpaceGenericGray;
if (CFStringCompare(s, kCGColorSpaceGenericRGB, kNilOptions) == kCFCompareEqualTo)
return MNLColorSpaceGenericRGB;
if (CFStringCompare(s, kCGColorSpaceGenericCMYK, kNilOptions) == kCFCompareEqualTo)
return MNLColorSpaceGenericCMYK;
}
if (@available(iOS 9.0, macOS 10.5, tvOS 9.0, watchOS 2.0, macCatalyst 13.0, *)) {
if (CFStringCompare(s, kCGColorSpaceGenericRGBLinear, kNilOptions) == kCFCompareEqualTo)
return MNLColorSpaceGenericRGBLinear;
if (CFStringCompare(s, kCGColorSpaceAdobeRGB1998, kNilOptions) == kCFCompareEqualTo)
return MNLColorSpaceAdobeRGB1998;
if (CFStringCompare(s, kCGColorSpaceSRGB, kNilOptions) == kCFCompareEqualTo)
return MNLColorSpaceSRGB;
}
if (@available(iOS 9.0, macOS 10.6, tvOS 9.0, watchOS 2.0, macCatalyst 13.0, *)) {
if (CFStringCompare(s, kCGColorSpaceGenericGrayGamma2_2, kNilOptions) == kCFCompareEqualTo)
return MNLColorSpaceGenericGrayGamma2_2;
}
if (@available(iOS 9.0, macOS 10.11, tvOS 9.0, watchOS 2.0, macCatalyst 13.0, *)) {
if (CFStringCompare(s, kCGColorSpaceGenericXYZ, kNilOptions) == kCFCompareEqualTo)
return MNLColorSpaceGenericXYZ;
if (CFStringCompare(s, kCGColorSpaceACESCGLinear, kNilOptions) == kCFCompareEqualTo)
return MNLColorSpaceACESCGLinear;
if (CFStringCompare(s, kCGColorSpaceITUR_709, kNilOptions) == kCFCompareEqualTo)
return MNLColorSpaceITUR_709;
if (CFStringCompare(s, kCGColorSpaceITUR_2020, kNilOptions) == kCFCompareEqualTo)
return MNLColorSpaceITUR_2020;
if (CFStringCompare(s, kCGColorSpaceROMMRGB, kNilOptions) == kCFCompareEqualTo)
return MNLColorSpaceROMMRGB;
if (CFStringCompare(s, kCGColorSpaceDCIP3, kNilOptions) == kCFCompareEqualTo)
return MNLColorSpaceDCIP3;
}
if (@available(iOS 9.3, macOS 10.11.2, tvOS 9.3, watchOS 2.3, macCatalyst 13.0, *)) {
if (CFStringCompare(s, kCGColorSpaceDisplayP3, kNilOptions) == kCFCompareEqualTo)
return MNLColorSpaceDisplayP3;
}
if (@available(iOS 10.0, macOS 10.12, tvOS 10.0, watchOS 3.0, macCatalyst 13.0, *)) {
if (CFStringCompare(s, kCGColorSpaceLinearGray, kNilOptions) == kCFCompareEqualTo)
return MNLColorSpaceLinearGray;
if (CFStringCompare(s, kCGColorSpaceExtendedSRGB, kNilOptions) == kCFCompareEqualTo)
return MNLColorSpaceExtendedSRGB;
if (CFStringCompare(s, kCGColorSpaceLinearSRGB, kNilOptions) == kCFCompareEqualTo)
return MNLColorSpaceLinearSRGB;
if (CFStringCompare(s, kCGColorSpaceExtendedLinearSRGB, kNilOptions) == kCFCompareEqualTo)
return MNLColorSpaceExtendedLinearSRGB;
if (CFStringCompare(s, kCGColorSpaceExtendedGray, kNilOptions) == kCFCompareEqualTo)
return MNLColorSpaceExtendedGray;
if (CFStringCompare(s, kCGColorSpaceExtendedLinearGray, kNilOptions) == kCFCompareEqualTo)
return MNLColorSpaceExtendedLinearGray;
}
if (@available(iOS 12.3, macOS 10.14.3, tvOS 12.3, watchOS 5.3, macCatalyst 13.0, *)) {
if (CFStringCompare(s, kCGColorSpaceExtendedLinearDisplayP3, kNilOptions) == kCFCompareEqualTo)
return MNLColorSpaceExtendedLinearDisplayP3;
if (CFStringCompare(s, kCGColorSpaceExtendedLinearITUR_2020, kNilOptions) == kCFCompareEqualTo)
return MNLColorSpaceExtendedLinearITUR_2020;
}
if (@available(iOS 13.0, macOS 10.14.6, tvOS 13.0, watchOS 6.0, macCatalyst 13.0, *)) {
if (CFStringCompare(s, kCGColorSpaceDisplayP3_PQ_EOTF, kNilOptions) == kCFCompareEqualTo)
return MNLColorSpaceDisplayP3_PQ_EOTF;
if (CFStringCompare(s, kCGColorSpaceDisplayP3_HLG, kNilOptions) == kCFCompareEqualTo)
return MNLColorSpaceDisplayP3_HLG;
if (CFStringCompare(s, kCGColorSpaceITUR_2020_PQ_EOTF, kNilOptions) == kCFCompareEqualTo)
return MNLColorSpaceITUR_2020_PQ_EOTF;
if (CFStringCompare(s, kCGColorSpaceITUR_2020_HLG, kNilOptions) == kCFCompareEqualTo)
return MNLColorSpaceITUR_2020_HLG;
}
return MNLColorSpaceUnknown;
}