-
Notifications
You must be signed in to change notification settings - Fork 301
/
Colours.m
executable file
·1454 lines (1190 loc) · 39.5 KB
/
Colours.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
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2013 by Benjamin Gordon
//
// Permission is hereby granted, free of charge, to any
// person obtaining a copy of this software and
// associated documentation files (the "Software"), to
// deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the
// Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall
// be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "Colours.h"
#import <objc/runtime.h>
#pragma mark - Static Block
static CGFloat (^RAD)(CGFloat) = ^CGFloat (CGFloat degree){
return degree * M_PI/180;
};
#pragma mark - Create correct iOS/OSX implementation
#if TARGET_OS_IPHONE || TARGET_OS_TV
#import <UIKit/UIKit.h>
@implementation UIColor (Colours)
#define ColorClass UIColor
#elif TARGET_OS_MAC
#import <AppKit/AppKit.h>
@implementation NSColor (Colours)
#define ColorClass NSColor
#endif
#pragma mark - Color from Hex
+ (instancetype)colorFromHexString:(NSString *)hexString
{
unsigned rgbValue = 0;
hexString = [hexString stringByReplacingOccurrencesOfString:@"#" withString:@""];
NSScanner *scanner = [NSScanner scannerWithString:hexString];
[scanner scanHexInt:&rgbValue];
return [[self class] colorWithR:((rgbValue & 0xFF0000) >> 16) G:((rgbValue & 0xFF00) >> 8) B:(rgbValue & 0xFF) A:1.0];
}
#pragma mark - Hex from Color
- (NSString *)hexString
{
NSArray *colorArray = [self rgbaArray];
int r = [colorArray[0] floatValue] * 255;
int g = [colorArray[1] floatValue] * 255;
int b = [colorArray[2] floatValue] * 255;
NSString *red = [NSString stringWithFormat:@"%02x", r];
NSString *green = [NSString stringWithFormat:@"%02x", g];
NSString *blue = [NSString stringWithFormat:@"%02x", b];
return [NSString stringWithFormat:@"#%@%@%@", red, green, blue];
}
#pragma mark - Color from RGBA
+ (instancetype)colorFromRGBAArray:(NSArray *)rgbaArray
{
if (rgbaArray.count < 4) {
return [[self class] clearColor];
}
return [[self class] colorWithRed:[rgbaArray[0] floatValue]
green:[rgbaArray[1] floatValue]
blue:[rgbaArray[2] floatValue]
alpha:[rgbaArray[3] floatValue]];
}
+ (instancetype)colorFromRGBADictionary:(NSDictionary *)rgbaDict
{
if (rgbaDict[kColoursRGBA_R] && rgbaDict[kColoursRGBA_G] && rgbaDict[kColoursRGBA_B] && rgbaDict[kColoursRGBA_A]) {
return [[self class] colorWithRed:[rgbaDict[kColoursRGBA_R] floatValue]
green:[rgbaDict[kColoursRGBA_G] floatValue]
blue:[rgbaDict[kColoursRGBA_B] floatValue]
alpha:[rgbaDict[kColoursRGBA_A] floatValue]];
}
return [[self class] clearColor];
}
#pragma mark - RGBA from Color
- (NSArray *)rgbaArray
{
CGFloat r=0,g=0,b=0,a=0;
if ([self respondsToSelector:@selector(getRed:green:blue:alpha:)]) {
[self getRed:&r green:&g blue:&b alpha:&a];
}
else {
const CGFloat *components = CGColorGetComponents(self.CGColor);
r = components[0];
g = components[1];
b = components[2];
a = components[3];
}
return @[@(r),
@(g),
@(b),
@(a)];
}
- (NSDictionary *)rgbaDictionary
{
CGFloat r=0,g=0,b=0,a=0;
if ([self respondsToSelector:@selector(getRed:green:blue:alpha:)]) {
[self getRed:&r green:&g blue:&b alpha:&a];
}
else {
const CGFloat *components = CGColorGetComponents(self.CGColor);
r = components[0];
g = components[1];
b = components[2];
a = components[3];
}
return @{kColoursRGBA_R:@(r),
kColoursRGBA_G:@(g),
kColoursRGBA_B:@(b),
kColoursRGBA_A:@(a)};
}
#pragma mark - HSBA from Color
- (NSArray *)hsbaArray
{
// Takes a [self class] and returns Hue,Saturation,Brightness,Alpha values in NSNumber form
CGFloat h=0,s=0,b=0,a=0;
if ([self respondsToSelector:@selector(getHue:saturation:brightness:alpha:)]) {
[self getHue:&h saturation:&s brightness:&b alpha:&a];
}
return @[@(h),
@(s),
@(b),
@(a)];
}
- (NSDictionary *)hsbaDictionary
{
CGFloat h=0,s=0,b=0,a=0;
if ([self respondsToSelector:@selector(getHue:saturation:brightness:alpha:)]) {
[self getHue:&h saturation:&s brightness:&b alpha:&a];
}
return @{kColoursHSBA_H:@(h),
kColoursHSBA_S:@(s),
kColoursHSBA_B:@(b),
kColoursHSBA_A:@(a)};
}
#pragma mark - Color from HSBA
+ (instancetype)colorFromHSBAArray:(NSArray *)hsbaArray
{
if (hsbaArray.count < 4) {
return [[self class] clearColor];
}
return [[self class] colorWithHue:[hsbaArray[0] doubleValue]
saturation:[hsbaArray[1] doubleValue]
brightness:[hsbaArray[2] doubleValue]
alpha:[hsbaArray[3] doubleValue]];
}
+ (instancetype)colorFromHSBADictionary:(NSDictionary *)hsbaDict
{
if (hsbaDict[kColoursHSBA_H] && hsbaDict[kColoursHSBA_S] && hsbaDict[kColoursHSBA_B] && hsbaDict[kColoursHSBA_A]) {
return [[self class] colorWithHue:[hsbaDict[kColoursHSBA_H] doubleValue]
saturation:[hsbaDict[kColoursHSBA_S] doubleValue]
brightness:[hsbaDict[kColoursHSBA_B] doubleValue]
alpha:[hsbaDict[kColoursHSBA_A] doubleValue]];
}
return [[self class] clearColor];
}
#pragma mark - LAB from Color
- (NSArray *)CIE_LabArray {
// Convert Color to XYZ format first
NSArray *rgba = [self rgbaArray];
CGFloat R = [rgba[0] floatValue];
CGFloat G = [rgba[1] floatValue];
CGFloat B = [rgba[2] floatValue];
// Create deltaR block
void (^deltaRGB)(CGFloat *R);
deltaRGB = ^(CGFloat *R) {
*R = (*R > 0.04045) ? pow((*R + 0.055)/1.055, 2.40) : (*R/12.92);
};
deltaRGB(&R);
deltaRGB(&G);
deltaRGB(&B);
CGFloat X = R*41.24 + G*35.76 + B*18.05;
CGFloat Y = R*21.26 + G*71.52 + B*7.22;
CGFloat Z = R*1.93 + G*11.92 + B*95.05;
// Convert XYZ to L*a*b*
X = X/95.047;
Y = Y/100.000;
Z = Z/108.883;
// Create deltaF block
void (^deltaF)(CGFloat *f);
deltaF = ^(CGFloat *f){
*f = (*f > pow((6.0/29.0), 3.0)) ? pow(*f, 1.0/3.0) : (1/3)*pow((29.0/6.0), 2.0) * *f + 4/29.0;
};
deltaF(&X);
deltaF(&Y);
deltaF(&Z);
NSNumber *L = @(116*Y - 16);
NSNumber *a = @(500 * (X - Y));
NSNumber *b = @(200 * (Y - Z));
return @[L,
a,
b,
rgba[3]];
}
- (NSDictionary *)CIE_LabDictionary {
NSArray *colors = [self CIE_LabArray];
return @{kColoursCIE_L:colors[0],
kColoursCIE_A:colors[1],
kColoursCIE_B:colors[2],
kColoursCIE_alpha:colors[3],};
}
#pragma mark - LCH from Color
- (NSArray*)CIE_LCHArray {
// www.brucelindbloom.com/index.html?Equations.html
NSArray *Lab = [self CIE_LabArray];
NSMutableArray *LCH = [Lab mutableCopy];
//L = L, a = a
//C
LCH[1] = @(sqrt(pow([Lab[1] doubleValue], 2) + pow([Lab[2] doubleValue], 2)));
//H
double h = atan2([Lab[2] doubleValue], [Lab[1] doubleValue]);
h = h * 180/M_PI;
if (h < 0) {
h += 360;
} else if ( h >= 360) {
h -= 360;
}
LCH[2] = @(h);
return LCH;
}
- (NSDictionary *)CIE_LCHDictionary {
NSArray *colors = [self CIE_LCHArray];
return @{kColoursCIE_L:colors[0],
kColoursCIE_C:colors[1],
kColoursCIE_H:colors[2],
kColoursCIE_alpha:colors[3],};
}
#pragma mark - Color from LAB
+ (instancetype)colorFromCIE_LabArray:(NSArray *)colors {
if (!colors || colors.count < 4) {
return [[self class] clearColor];
}
// Convert LAB to XYZ
CGFloat L = [colors[0] floatValue];
CGFloat A = [colors[1] floatValue];
CGFloat B = [colors[2] floatValue];
CGFloat Y = (L + 16.0)/116.0;
CGFloat X = A/500 + Y;
CGFloat Z = Y - B/200;
void (^deltaXYZ)(CGFloat *);
deltaXYZ = ^(CGFloat *k){
*k = (pow(*k, 3.0) > 0.008856) ? pow(*k, 3.0) : (*k - 4/29.0)/7.787;
};
deltaXYZ(&X);
deltaXYZ(&Y);
deltaXYZ(&Z);
X = X*.95047;
Y = Y*1.00000;
Z = Z*1.08883;
// Convert XYZ to RGB
CGFloat R = X*3.2406 + Y*-1.5372 + Z*-0.4986;
CGFloat G = X*-0.9689 + Y*1.8758 + Z*0.0415;
CGFloat _B = X*0.0557 + Y*-0.2040 + Z*1.0570;
void (^deltaRGB)(CGFloat *);
deltaRGB = ^(CGFloat *k){
*k = (*k > 0.0031308) ? 1.055 * (pow(*k, (1/2.4))) - 0.055 : *k * 12.92;
};
deltaRGB(&R);
deltaRGB(&G);
deltaRGB(&_B);
// return Color
return [[self class] colorFromRGBAArray:@[@(R), @(G), @(_B), colors[3]]];
}
+ (instancetype)colorFromCIE_LabDictionary:(NSDictionary *)colors {
if (!colors) {
return [[self class] clearColor];
}
if (colors[kColoursCIE_L] && colors[kColoursCIE_A] && colors[kColoursCIE_B] && colors[kColoursCIE_alpha]) {
return [self colorFromCIE_LabArray:@[colors[kColoursCIE_L],
colors[kColoursCIE_A],
colors[kColoursCIE_B],
colors[kColoursCIE_alpha]]];
}
return [[self class] clearColor];
}
#pragma mark - Color from LCH
+ (instancetype)colorFromCIE_LCHArray:(NSArray *)colors {
if (!colors) {
return [[self class] clearColor];
}
NSMutableArray *Lab = [colors mutableCopy];
double H = [colors[2] doubleValue] * M_PI/180;;
Lab[1] = @([colors[1] doubleValue] * cos(H));
Lab[2] = @([colors[1] doubleValue] * sin(H));
return [[self class] colorFromCIE_LabArray:Lab];
}
+ (instancetype)colorFromCIE_LCHDictionary:(NSDictionary *)colors {
if (!colors) {
return [[self class] clearColor];
}
if (colors[kColoursCIE_L] && colors[kColoursCIE_C] && colors[kColoursCIE_H] && colors[kColoursCIE_alpha]) {
return [self colorFromCIE_LCHArray:@[colors[kColoursCIE_L],
colors[kColoursCIE_C],
colors[kColoursCIE_H],
colors[kColoursCIE_alpha]]];
}
return [[self class] clearColor];
}
#pragma mark - Color to CMYK
- (NSArray *)cmykArray
{
// Convert RGB to CMY
NSArray *rgb = [self rgbaArray];
CGFloat C = 1 - [rgb[0] floatValue];
CGFloat M = 1 - [rgb[1] floatValue];
CGFloat Y = 1 - [rgb[2] floatValue];
// Find K
CGFloat K = MIN(1, MIN(C, MIN(Y, M)));
if (K == 1) {
C = 0;
M = 0;
Y = 0;
}
else {
void (^newCMYK)(CGFloat *);
newCMYK = ^(CGFloat *x){
*x = (*x - K)/(1 - K);
};
newCMYK(&C);
newCMYK(&M);
newCMYK(&Y);
}
return @[@(C),
@(M),
@(Y),
@(K)];
}
- (NSDictionary *)cmykDictionary
{
NSArray *colors = [self cmykArray];
return @{kColoursCMYK_C:colors[0],
kColoursCMYK_M:colors[1],
kColoursCMYK_Y:colors[2],
kColoursCMYK_K:colors[3]};
}
#pragma mark - CMYK to Color
+ (instancetype)colorFromCMYKArray:(NSArray *)cmyk
{
if (!cmyk || cmyk.count < 4) {
return [[self class] clearColor];
}
// Find CMY values
CGFloat C = [cmyk[0] floatValue];
CGFloat M = [cmyk[1] floatValue];
CGFloat Y = [cmyk[2] floatValue];
CGFloat K = [cmyk[3] floatValue];
void (^cmyTransform)(CGFloat *);
cmyTransform = ^(CGFloat *x){
*x = *x * (1 - K) + K;
};
cmyTransform(&C);
cmyTransform(&M);
cmyTransform(&Y);
// Translate CMY to RGB
CGFloat R = 1 - C;
CGFloat G = 1 - M;
CGFloat B = 1 - Y;
// return the Color
return [[self class] colorFromRGBAArray:@[@(R),
@(G),
@(B),
@(1)]];
}
+ (instancetype)colorFromCMYKDictionary:(NSDictionary *)cmyk
{
if (!cmyk) {
return [[self class] clearColor];
}
if (cmyk[kColoursCMYK_C] && cmyk[kColoursCMYK_M] && cmyk[kColoursCMYK_Y] && cmyk[kColoursCMYK_K]) {
return [[self class] colorFromCMYKArray:@[cmyk[kColoursCMYK_C],
cmyk[kColoursCMYK_M],
cmyk[kColoursCMYK_Y],
cmyk[kColoursCMYK_K]]];
}
return [[self class] clearColor];
}
#pragma mark - Color Components
- (NSDictionary *)colorComponents
{
NSMutableDictionary *components = [[self rgbaDictionary] mutableCopy];
[components addEntriesFromDictionary:[self hsbaDictionary]];
[components addEntriesFromDictionary:[self CIE_LabDictionary]];
return components;
}
- (CGFloat)red
{
return [[self rgbaArray][0] floatValue];
}
- (CGFloat)green
{
return [[self rgbaArray][1] floatValue];
}
- (CGFloat)blue
{
return [[self rgbaArray][2] floatValue];
}
- (CGFloat)hue
{
return [[self hsbaArray][0] floatValue];
}
- (CGFloat)saturation
{
return [[self hsbaArray][1] floatValue];
}
- (CGFloat)brightness
{
return [[self hsbaArray][2] floatValue];
}
- (CGFloat)alpha
{
return [[self rgbaArray][3] floatValue];
}
- (CGFloat)CIE_Lightness
{
return [[self CIE_LabArray][0] floatValue];
}
- (CGFloat)CIE_a
{
return [[self CIE_LabArray][1] floatValue];
}
- (CGFloat)CIE_b
{
return [[self CIE_LabArray][2] floatValue];
}
- (CGFloat)cyan {
return [[self cmykArray][0] floatValue];
}
- (CGFloat)magenta {
return [[self cmykArray][1] floatValue];
}
- (CGFloat)yellow {
return [[self cmykArray][2] floatValue];
}
- (CGFloat)keyBlack {
return [[self cmykArray][3] floatValue];
}
#pragma mark - Darken/Lighten
- (instancetype)darken:(CGFloat)percentage {
return [self modifyBrightnessByPercentage:1.0-percentage];
}
- (instancetype)lighten:(CGFloat)percentage {
return [self modifyBrightnessByPercentage:percentage+1.0];
}
- (instancetype)modifyBrightnessByPercentage:(CGFloat)percentage {
NSMutableDictionary *hsba = [[self hsbaDictionary] mutableCopy];
[hsba setObject:@([hsba[kColoursHSBA_B] floatValue] * percentage) forKey:kColoursHSBA_B];
return [ColorClass colorFromHSBADictionary:hsba];
}
#pragma mark - Generate Color Scheme
- (NSArray *)colorSchemeOfType:(ColorScheme)type
{
NSArray *hsbArray = [self hsbaArray];
float hue = [hsbArray[0] floatValue] * 360;
float sat = [hsbArray[1] floatValue] * 100;
float bright = [hsbArray[2] floatValue] * 100;
float alpha = [hsbArray[3] floatValue];
switch (type) {
case ColorSchemeAnalagous:
return [[self class] analagousColorsFromHue:hue saturation:sat brightness:bright alpha:alpha];
case ColorSchemeMonochromatic:
return [[self class] monochromaticColorsFromHue:hue saturation:sat brightness:bright alpha:alpha];
case ColorSchemeTriad:
return [[self class] triadColorsFromHue:hue saturation:sat brightness:bright alpha:alpha];
case ColorSchemeComplementary:
return [[self class] complementaryColorsFromHue:hue saturation:sat brightness:bright alpha:alpha];
default:
return nil;
}
}
#pragma mark - Color Scheme Generation - Helper methods
+ (NSArray *)analagousColorsFromHue:(float)h saturation:(float)s brightness:(float)b alpha:(float)a
{
return @[[[self class] colorWithHue:[[self class] addDegrees:30 toDegree:h]/360 saturation:(s-5)/100 brightness:(b-10)/100 alpha:a],
[[self class] colorWithHue:[[self class] addDegrees:15 toDegree:h]/360 saturation:(s-5)/100 brightness:(b-5)/100 alpha:a],
[[self class] colorWithHue:[[self class] addDegrees:-15 toDegree:h]/360 saturation:(s-5)/100 brightness:(b-5)/100 alpha:a],
[[self class] colorWithHue:[[self class] addDegrees:-30 toDegree:h]/360 saturation:(s-5)/100 brightness:(b-10)/100 alpha:a]];
}
+ (NSArray *)monochromaticColorsFromHue:(float)h saturation:(float)s brightness:(float)b alpha:(float)a
{
return @[[[self class] colorWithHue:h/360 saturation:(s/2)/100 brightness:(b/3)/100 alpha:a],
[[self class] colorWithHue:h/360 saturation:s/100 brightness:(b/2)/100 alpha:a],
[[self class] colorWithHue:h/360 saturation:(s/3)/100 brightness:(2*b/3)/100 alpha:a],
[[self class] colorWithHue:h/360 saturation:s/100 brightness:(4*b/5)/100 alpha:a]];
}
+ (NSArray *)triadColorsFromHue:(float)h saturation:(float)s brightness:(float)b alpha:(float)a
{
return @[[[self class] colorWithHue:[[self class] addDegrees:120 toDegree:h]/360 saturation:(7*s/6)/100 brightness:(b-5)/100 alpha:a],
[[self class] colorWithHue:[[self class] addDegrees:120 toDegree:h]/360 saturation:s/100 brightness:b/100 alpha:a],
[[self class] colorWithHue:[[self class] addDegrees:240 toDegree:h]/360 saturation:s/100 brightness:b/100 alpha:a],
[[self class] colorWithHue:[[self class] addDegrees:240 toDegree:h]/360 saturation:(7*s/6)/100 brightness:(b-5)/100 alpha:a]];
}
+ (NSArray *)complementaryColorsFromHue:(float)h saturation:(float)s brightness:(float)b alpha:(float)a
{
return @[[[self class] colorWithHue:h/360 saturation:s/100 brightness:(4*b/5)/100 alpha:a],
[[self class] colorWithHue:h/360 saturation:(5*s/7)/100 brightness:b/100 alpha:a],
[[self class] colorWithHue:[[self class] addDegrees:180 toDegree:h]/360 saturation:s/100 brightness:b/100 alpha:a],
[[self class] colorWithHue:[[self class] addDegrees:180 toDegree:h]/360 saturation:(5*s/7)/100 brightness:b/100 alpha:a]];
}
#pragma mark - Contrasting Color
- (instancetype)blackOrWhiteContrastingColor
{
NSArray *rgbaArray = [self rgbaArray];
double a = 1 - ((0.299 * [rgbaArray[0] doubleValue]) + (0.587 * [rgbaArray[1] doubleValue]) + (0.114 * [rgbaArray[2] doubleValue]));
return a < 0.5 ? [[self class] blackColor] : [[self class] whiteColor];
}
#pragma mark - Complementary Color
- (instancetype)complementaryColor
{
NSMutableDictionary *hsba = [[self hsbaDictionary] mutableCopy];
float newH = [[self class] addDegrees:180.0f toDegree:([hsba[kColoursHSBA_H] floatValue]*360.0f)];
[hsba setObject:@(newH/360.0f) forKey:kColoursHSBA_H];
return [[self class] colorFromHSBADictionary:hsba];
}
#pragma mark - Distance between Colors
- (CGFloat)distanceFromColor:(id)color
{
// Defaults to CIE94
return [self distanceFromColor:color type:ColorDistanceCIE94];
}
- (CGFloat)distanceFromColor:(id)color type:(ColorDistance)distanceType {
/**
*
* Detecting a difference in two colors is not as trivial as it sounds.
* One's first instinct is to go for a difference in RGB values, leaving
* you with a sum of the differences of each point. It looks great! Until
* you actually start comparing colors. Why do these two reds have a different
* distance than these two blues *in real life* vs computationally?
* Human visual perception is next in the line of things between a color
* and your brain. Some colors are just perceived to have larger variants inside
* of their respective areas than others, so we need a way to model this
* human variable to colors. Enter CIELAB. This color formulation is supposed to be
* this model. So now we need to standardize a unit of distance between any two
* colors that works independent of how humans visually perceive that distance.
* Enter CIE76,94,2000. These are methods that use user-tested data and other
* mathematically and statistically significant correlations to output this info.
* You can read the wiki articles below to get a better understanding historically
* of how we moved to newer and better color distance formulas, and what
* their respective pros/cons are.
*
* References:
*
* http://en.wikipedia.org/wiki/Color_difference
* http://en.wikipedia.org/wiki/Just_noticeable_difference
* http://en.wikipedia.org/wiki/CIELAB
*
*/
// Check if it's a color
if (![color isKindOfClass:[self class]]) {
// NSLog(@"Not a %@ object.", NSStringFromClass([self class]));
return MAXFLOAT;
}
// Set Up Common Variables
NSArray *lab1 = [self CIE_LabArray];
NSArray *lab2 = [color CIE_LabArray];
CGFloat L1 = [lab1[0] floatValue];
CGFloat A1 = [lab1[1] floatValue];
CGFloat B1 = [lab1[2] floatValue];
CGFloat L2 = [lab2[0] floatValue];
CGFloat A2 = [lab2[1] floatValue];
CGFloat B2 = [lab2[2] floatValue];
// CIE76 first
if (distanceType == ColorDistanceCIE76) {
CGFloat distance = sqrtf(pow((L1-L2), 2) + pow((A1-A2), 2) + pow((B1-B2), 2));
return distance;
}
// More Common Variables
CGFloat kL = 1;
CGFloat kC = 1;
CGFloat kH = 1;
CGFloat k1 = 0.045;
CGFloat k2 = 0.015;
CGFloat deltaL = L1 - L2;
CGFloat C1 = sqrt((A1*A1) + (B1*B1));
CGFloat C2 = sqrt((A2*A2) + (B2*B2));
CGFloat deltaC = C1 - C2;
CGFloat deltaH = sqrt(pow((A1-A2), 2.0) + pow((B1-B2), 2.0) - pow(deltaC, 2.0));
CGFloat sL = 1;
CGFloat sC = 1 + k1*(sqrt((A1*A1) + (B1*B1)));
CGFloat sH = 1 + k2*(sqrt((A1*A1) + (B1*B1)));
// CIE94
if (distanceType == ColorDistanceCIE94) {
return sqrt(pow((deltaL/(kL*sL)), 2.0) + pow((deltaC/(kC*sC)), 2.0) + pow((deltaH/(kH*sH)), 2.0));
}
// CIE2000
// More variables
CGFloat deltaLPrime = L2 - L1;
CGFloat meanL = (L1 + L2)/2;
CGFloat meanC = (C1 + C2)/2;
CGFloat aPrime1 = A1 + A1/2*(1 - sqrt(pow(meanC, 7.0)/(pow(meanC, 7.0) + pow(25.0, 7.0))));
CGFloat aPrime2 = A2 + A2/2*(1 - sqrt(pow(meanC, 7.0)/(pow(meanC, 7.0) + pow(25.0, 7.0))));
CGFloat cPrime1 = sqrt((aPrime1*aPrime1) + (B1*B1));
CGFloat cPrime2 = sqrt((aPrime2*aPrime2) + (B2*B2));
CGFloat cMeanPrime = (cPrime1 + cPrime2)/2;
CGFloat deltaCPrime = cPrime1 - cPrime2;
CGFloat hPrime1 = atan2(B1, aPrime1);
CGFloat hPrime2 = atan2(B2, aPrime2);
hPrime1 = fmodf(hPrime1, RAD(360.0));
hPrime2 = fmodf(hPrime2, RAD(360.0));
CGFloat deltahPrime = 0;
if (fabs(hPrime1 - hPrime2) <= RAD(180.0)) {
deltahPrime = hPrime2 - hPrime1;
}
else {
deltahPrime = (hPrime2 <= hPrime1) ? hPrime2 - hPrime1 + RAD(360.0) : hPrime2 - hPrime1 - RAD(360.0);
}
CGFloat deltaHPrime = 2 * sqrt(cPrime1*cPrime2) * sin(deltahPrime/2);
CGFloat meanHPrime = (fabs(hPrime1 - hPrime2) <= RAD(180.0)) ? (hPrime1 + hPrime2)/2 : (hPrime1 + hPrime2 + RAD(360.0))/2;
CGFloat T = 1 - 0.17*cos(meanHPrime - RAD(30.0)) + 0.24*cos(2*meanHPrime)+0.32*cos(3*meanHPrime + RAD(6.0)) - 0.20*cos(4*meanHPrime - RAD(63.0));
sL = 1 + (0.015 * pow((meanL - 50), 2))/sqrt(20 + pow((meanL - 50), 2));
sC = 1 + 0.045*cMeanPrime;
sH = 1 + 0.015*cMeanPrime*T;
CGFloat Rt = -2 * sqrt(pow(cMeanPrime, 7)/(pow(cMeanPrime, 7) + pow(25.0, 7))) * sin(RAD(60.0)* exp(-1 * pow((meanHPrime - RAD(275.0))/RAD(25.0), 2)));
// Finally return CIE2000 distance
return sqrt(pow((deltaLPrime/(kL*sL)), 2) + pow((deltaCPrime/(kC*sC)), 2) + pow((deltaHPrime/(kH*sH)), 2) + Rt*(deltaC/(kC*sC))*(deltaHPrime/(kH*sH)));
}
#pragma mark - Compare Colors
+ (NSArray *)sortColors:(NSArray *)colors withComparison:(ColorComparison)comparison {
return [colors sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [self compareColor:obj1 andColor:obj2 withComparison:comparison];
}];
}
+ (NSComparisonResult)compareColor:(id)colorA andColor:(id)colorB withComparison:(ColorComparison)comparison {
if (![colorA isKindOfClass:[self class]] || ![colorB isKindOfClass:[self class]]) {
return NSOrderedSame;
}
// Check Colors
NSString *key = @"";
boolean_t greater = true;
NSDictionary *c1 = [colorA colorsForComparison:comparison key:&key greater:&greater];
NSDictionary *c2 = [colorB colorsForComparison:comparison key:&key greater:&greater];
return [self compareValue:[c1[key] floatValue] andValue:[c2[key] floatValue] greaterThan:greater];
}
#pragma mark - System Colors
+ (instancetype)infoBlueColor
{
return [[self class] colorWithR:47 G:112 B:225 A:1.0];
}
+ (instancetype)successColor
{
return [[self class] colorWithR:83 G:215 B:106 A:1.0];
}
+ (instancetype)warningColor
{
return [[self class] colorWithR:221 G:170 B:59 A:1.0];
}
+ (instancetype)dangerColor
{
return [[self class] colorWithR:229 G:0 B:15 A:1.0];
}
#pragma mark - Whites
+ (instancetype)antiqueWhiteColor
{
return [[self class] colorWithR:250 G:235 B:215 A:1.0];
}
+ (instancetype)oldLaceColor
{
return [[self class] colorWithR:253 G:245 B:230 A:1.0];
}
+ (instancetype)ivoryColor
{
return [[self class] colorWithR:255 G:255 B:240 A:1.0];
}
+ (instancetype)seashellColor
{
return [[self class] colorWithR:255 G:245 B:238 A:1.0];
}
+ (instancetype)ghostWhiteColor
{
return [[self class] colorWithR:248 G:248 B:255 A:1.0];
}
+ (instancetype)snowColor
{
return [[self class] colorWithR:255 G:250 B:250 A:1.0];
}
+ (instancetype)linenColor
{
return [[self class] colorWithR:250 G:240 B:230 A:1.0];
}
#pragma mark - Grays
+ (instancetype)black25PercentColor
{
return [[self class] colorWithWhite:0.25 alpha:1.0];
}
+ (instancetype)black50PercentColor
{
return [[self class] colorWithWhite:0.5 alpha:1.0];
}
+ (instancetype)black75PercentColor
{
return [[self class] colorWithWhite:0.75 alpha:1.0];
}
+ (instancetype)warmGrayColor
{
return [[self class] colorWithR:133 G:117 B:112 A:1.0];
}
+ (instancetype)coolGrayColor
{
return [[self class] colorWithR:118 G:122 B:133 A:1.0];
}
+ (instancetype)charcoalColor
{
return [[self class] colorWithR:34 G:34 B:34 A:1.0];
}
#pragma mark - Blues
+ (instancetype)tealColor
{
return [[self class] colorWithR:28 G:160 B:170 A:1.0];
}
+ (instancetype)steelBlueColor
{
return [[self class] colorWithR:103 G:153 B:170 A:1.0];
}
+ (instancetype)robinEggColor
{
return [[self class] colorWithR:141 G:218 B:247 A:1.0];
}
+ (instancetype)pastelBlueColor
{
return [[self class] colorWithR:99 G:161 B:247 A:1.0];
}
+ (instancetype)turquoiseColor
{
return [[self class] colorWithR:112 G:219 B:219 A:1.0];
}
+ (instancetype)skyBlueColor
{
return [[self class] colorWithR:0 G:178 B:238 A:1.0];
}
+ (instancetype)indigoColor
{
return [[self class] colorWithR:13 G:79 B:139 A:1.0];
}
+ (instancetype)denimColor
{
return [[self class] colorWithR:67 G:114 B:170 A:1.0];
}
+ (instancetype)blueberryColor
{
return [[self class] colorWithR:89 G:113 B:173 A:1.0];
}
+ (instancetype)cornflowerColor
{
return [[self class] colorWithR:100 G:149 B:237 A:1.0];
}
+ (instancetype)babyBlueColor
{
return [[self class] colorWithR:190 G:220 B:230 A:1.0];
}
+ (instancetype)midnightBlueColor
{
return [[self class] colorWithR:13 G:26 B:35 A:1.0];
}
+ (instancetype)fadedBlueColor
{
return [[self class] colorWithR:23 G:137 B:155 A:1.0];
}
+ (instancetype)icebergColor
{
return [[self class] colorWithR:200 G:213 B:219 A:1.0];
}
+ (instancetype)waveColor
{
return [[self class] colorWithR:102 G:169 B:251 A:1.0];
}
#pragma mark - Greens
+ (instancetype)emeraldColor
{
return [[self class] colorWithR:1 G:152 B:117 A:1.0];
}
+ (instancetype)grassColor
{
return [[self class] colorWithR:99 G:214 B:74 A:1.0];
}
+ (instancetype)pastelGreenColor
{
return [[self class] colorWithR:126 G:242 B:124 A:1.0];
}
+ (instancetype)seafoamColor
{
return [[self class] colorWithR:77 G:226 B:140 A:1.0];
}
+ (instancetype)paleGreenColor
{
return [[self class] colorWithR:176 G:226 B:172 A:1.0];
}
+ (instancetype)cactusGreenColor
{
return [[self class] colorWithR:99 G:111 B:87 A:1.0];
}
+ (instancetype)chartreuseColor
{
return [[self class] colorWithR:69 G:139 B:0 A:1.0];
}
+ (instancetype)hollyGreenColor
{
return [[self class] colorWithR:32 G:87 B:14 A:1.0];
}
+ (instancetype)oliveColor
{
return [[self class] colorWithR:91 G:114 B:34 A:1.0];
}
+ (instancetype)oliveDrabColor
{
return [[self class] colorWithR:107 G:142 B:35 A:1.0];
}
+ (instancetype)moneyGreenColor
{
return [[self class] colorWithR:134 G:198 B:124 A:1.0];
}
+ (instancetype)honeydewColor
{
return [[self class] colorWithR:216 G:255 B:231 A:1.0];
}
+ (instancetype)limeColor
{
return [[self class] colorWithR:56 G:237 B:56 A:1.0];
}
+ (instancetype)cardTableColor
{
return [[self class] colorWithR:87 G:121 B:107 A:1.0];
}