This repository has been archived by the owner on Jun 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
UIColor+Chameleon.m
executable file
·1086 lines (786 loc) · 36.2 KB
/
UIColor+Chameleon.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
// UIColor+Chameleon.m
/*
The MIT License (MIT)
Copyright (c) 2014-2015 Vicc Alexander.
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 "UIColor+Chameleon.h"
#import "UIColor+ChameleonPrivate.h"
#import "ChameleonMacros.h"
#import <objc/runtime.h>
@implementation UIColor (Chameleon)
@dynamic gradientImage;
#pragma mark - Chameleon - Getter & Setter Methods for Instance Variables
+ (void)setGradientImage:(UIImage *)gradientImage {
objc_setAssociatedObject(self, @selector(gradientImage), gradientImage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
+ (UIImage *)gradientImage {
return objc_getAssociatedObject(self, @selector(gradientImage));
}
#pragma mark - Chameleon - Light Shades
+ (UIColor *)flatBlackColor {
return hsb(0, 0, 17);
}
+ (UIColor *)flatBlueColor {
return hsb(224, 50, 63);
}
+ (UIColor *)flatBrownColor {
return hsb(24, 45, 37);
}
+ (UIColor *)flatCoffeeColor {
return hsb(25, 31, 64);
}
+ (UIColor *)flatForestGreenColor {
return hsb(138, 45, 37);
}
+ (UIColor *)flatGrayColor {
return hsb(184, 10, 65);
}
+ (UIColor *)flatGreenColor {
return hsb(145, 77, 80);
}
+ (UIColor *)flatLimeColor {
return hsb(74, 70, 78);
}
+ (UIColor *)flatMagentaColor {
return hsb(283, 51, 71);
}
+ (UIColor *)flatMaroonColor {
return hsb(5, 65, 47);
}
+ (UIColor *)flatMintColor {
return hsb(168, 86, 74);
}
+ (UIColor *)flatNavyBlueColor {
return hsb(210, 45, 37);
}
+ (UIColor *)flatOrangeColor {
return hsb(28, 85, 90);
}
+ (UIColor *)flatPinkColor {
return hsb(324, 49, 96);
}
+ (UIColor *)flatPlumColor {
return hsb(300, 45, 37);
}
+ (UIColor *)flatPowderBlueColor {
return hsb(222, 24, 95);
}
+ (UIColor *)flatPurpleColor {
return hsb(253, 52, 77);
}
+ (UIColor *)flatRedColor {
return hsb(6, 74, 91);
}
+ (UIColor *)flatSandColor {
return hsb(42, 25, 94);
}
+ (UIColor *)flatSkyBlueColor {
return hsb(204, 76, 86);
}
+ (UIColor *)flatTealColor {
return hsb(195, 55, 51);
}
+ (UIColor *)flatWatermelonColor {
return hsb(356, 53, 94);
}
+ (UIColor *)flatWhiteColor {
return hsb(192, 2, 95);
}
+ (UIColor *)flatYellowColor {
return hsb(48, 99, 100);
}
#pragma mark - Chameleon - Dark Shades
+ (UIColor *)flatBlackDarkColor {
return hsb(0, 0, 15);
}
+ (UIColor *)flatBlueDarkColor {
return hsb(224, 56, 51);
}
+ (UIColor *)flatBrownDarkColor {
return hsb(25, 45, 31);
}
+ (UIColor *)flatCoffeeDarkColor {
return hsb(25, 34, 56);
}
+ (UIColor *)flatForestGreenDarkColor {
return hsb(135, 44, 31);
}
+ (UIColor *)flatGrayDarkColor {
return hsb(184, 10, 55);
}
+ (UIColor *)flatGreenDarkColor {
return hsb(145, 78, 68);
}
+ (UIColor *)flatLimeDarkColor {
return hsb(74, 81, 69);
}
+ (UIColor *)flatMagentaDarkColor {
return hsb(282, 61, 68);
}
+ (UIColor *)flatMaroonDarkColor {
return hsb(4, 68, 40);
}
+ (UIColor *)flatMintDarkColor {
return hsb(168, 86, 63);
}
+ (UIColor *)flatNavyBlueDarkColor {
return hsb(210, 45, 31);
}
+ (UIColor *)flatOrangeDarkColor {
return hsb(24, 100, 83);
}
+ (UIColor *)flatPinkDarkColor {
return hsb(327, 57, 83);
}
+ (UIColor *)flatPlumDarkColor {
return hsb(300, 46, 31);
}
+ (UIColor *)flatPowderBlueDarkColor {
return hsb(222, 28, 84);
}
+ (UIColor *)flatPurpleDarkColor {
return hsb(253, 56, 64);
}
+ (UIColor *)flatRedDarkColor {
return hsb(6, 78, 75);
}
+ (UIColor *)flatSandDarkColor {
return hsb(42, 30, 84);
}
+ (UIColor *)flatSkyBlueDarkColor {
return hsb(204, 78, 73);
}
+ (UIColor *)flatTealDarkColor {
return hsb(196, 54, 45);
}
+ (UIColor *)flatWatermelonDarkColor {
return hsb(358, 61, 85);
}
+ (UIColor *)flatWhiteDarkColor {
return hsb(204, 5, 78);
}
+ (UIColor *)flatYellowDarkColor {
return hsb(40, 100, 100);
}
#pragma mark - Chameleon - "Color With" Methods
+ (UIColor *)colorWithAverageColorFromImage:(UIImage *)image {
return [self colorWithAverageColorFromImage:image withAlpha:1.0];
}
+ (UIColor *)colorWithAverageColorFromImage:(UIImage *)image withAlpha:(CGFloat)alpha {
//Work within the RGB colorspoace
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char rgba[4];
CGContextRef context = CGBitmapContextCreate(rgba, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
//Draw our image down to 1x1 pixels
CGContextDrawImage(context, CGRectMake(0, 0, 1, 1), image.CGImage);
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
//Check if image alpha is 0
if (rgba[3] == 0) {
CGFloat imageAlpha = ((CGFloat)rgba[3])/255.0;
CGFloat multiplier = imageAlpha/255.0;
UIColor *averageColor = [UIColor colorWithRed:((CGFloat)rgba[0])*multiplier green:((CGFloat)rgba[1])*multiplier blue:((CGFloat)rgba[2])*multiplier alpha:imageAlpha];
//Improve color
averageColor = [averageColor colorWithMinimumSaturation:0.15];
//Return average color
return averageColor;
}
else {
//Get average
UIColor *averageColor = [UIColor colorWithRed:((CGFloat)rgba[0])/255.0 green:((CGFloat)rgba[1])/255.0 blue:((CGFloat)rgba[2])/255.0 alpha:alpha];
//Improve color
averageColor = [averageColor colorWithMinimumSaturation:0.15];
//Return average color
return averageColor;
}
}
+ (UIColor *)colorWithComplementaryFlatColorOf:(UIColor *)color {
return [[self class] colorWithComplementaryFlatColorOf:color withAlpha:1.0];
}
+ (UIColor *)colorWithComplementaryFlatColorOf:(UIColor *)color withAlpha:(CGFloat)alpha {
//Check if input UIColor is a gradient aka a pattern
if (CGColorGetPattern(color.CGColor)) {
//Let's find the average color of the image and contrast against that.
CGSize size = {1, 1};
//Create a 1x1 bitmap context
UIGraphicsBeginImageContext(size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
//Set the interpolation quality to medium
CGContextSetInterpolationQuality(ctx, kCGInterpolationMedium);
//Draw image scaled down to this 1x1 pixel
[[self gradientImage] drawInRect:(CGRect){.size = size} blendMode:kCGBlendModeCopy alpha:1];
//Read the RGB values from the context's buffer
uint8_t *data = CGBitmapContextGetData(ctx);
color = [UIColor colorWithRed:data[2] / 255.0f
green:data[1] / 255.0f
blue:data[0] / 255.0f
alpha:1];
UIGraphicsEndImageContext();
}
//Extract & Check to make sure we have an actual color to work with (Clear returns clear)
CGFloat hue, saturation, brightness, alpha1;
[color getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha1];
//Check if color is transparent
if (alpha1 == 0) {
return [UIColor clearColor];
}
//Multiply our value by their max values to convert
hue *= 360;
saturation *= 100;
brightness *= 100;
//Select a color with a hue 180 degrees away on the colorwheel (i.e. for 50 it would be 230).
hue += 180.0f;
if (hue > 360.f) {
hue -= 360.0f;
}
//Round to the nearest whole number after multiplying
hue = roundf(hue);
saturation = roundf(saturation);
brightness = roundf(brightness);
//Store complimentary nonflat color
UIColor *complimentaryNonFlatColor = [UIColor colorWithHue:hue/360.0
saturation:saturation/100.0
brightness:brightness/100.0
alpha:alpha];
//Retrieve LAB values from our complimentary nonflat color & return nearest flat color
return [self colorWithFlatVersionOf:complimentaryNonFlatColor withAlpha:alpha];
}
+ (UIColor *)colorWithFlatVersionOf:(UIColor *)color {
//Return flat version with default alpha of 1.0
return [[self class] colorWithFlatVersionOf:color withAlpha:1.0];
}
+ (UIColor *)colorWithFlatVersionFrom:(UIColor *)color {
CGFloat colorAlpha = 0;
[color getLightness:nil valueForA:nil valueForB:nil alpha:&colorAlpha];
colorAlpha = colorAlpha > 0.0 ? colorAlpha : 1.0;
//Return flat version with default alpha of 1.0
return [[self class] colorWithFlatVersionOf:color withAlpha:colorAlpha];
}
+ (UIColor * _Nullable)colorWithFlatVersionOf:(UIColor *)color withAlpha:(CGFloat)alpha {
//Check if input UIColor is a gradient aka a pattern
if (CGColorGetPattern(color.CGColor)) {
//Let's find the average color of the image and contrast against that.
CGSize size = {1, 1};
//Create a 1x1 bitmap context
UIGraphicsBeginImageContext(size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
//Set the interpolation quality to medium
CGContextSetInterpolationQuality(ctx, kCGInterpolationMedium);
//Draw image scaled down to this 1x1 pixel
[[self gradientImage] drawInRect:(CGRect){.size = size} blendMode:kCGBlendModeCopy alpha:1];
//Read the RGB values from the context's buffer
uint8_t *data = CGBitmapContextGetData(ctx);
color = [UIColor colorWithRed:data[2] / 255.0f
green:data[1] / 255.0f
blue:data[0] / 255.0f
alpha:1];
UIGraphicsEndImageContext();
}
//Create CGFloats to hold our color values
CGFloat L, A, B, alpha1;
//Get LAB values for our color
[color getLightness:&L valueForA:&A valueForB:&B alpha:&alpha1];
if (![color getLightness:&L valueForA:&A valueForB:&B alpha:&alpha1]) {
return nil;
}
//Find the nearest flat color
return [self nearestFlatColorForL:L A:A B:B alpha:alpha];
}
+ (UIColor *)colorWithContrastingBlackOrWhiteColorOn:(UIColor *)backgroundColor isFlat:(BOOL)flat {
//Return color with default alpha value of 1.0
return [[self class] colorWithContrastingBlackOrWhiteColorOn:backgroundColor isFlat:flat alpha:1.0];
}
+ (UIColor *)colorWithContrastingBlackOrWhiteColorOn:(UIColor *)backgroundColor
isFlat:(BOOL)flat
alpha:(CGFloat)alpha {
//Check if UIColor is a gradient aka a pattern
if (CGColorGetPattern(backgroundColor.CGColor)) {
//Let's find the average color of the image and contrast against that.
CGSize size = {1, 1};
//Create a 1x1 bitmap context
UIGraphicsBeginImageContext(size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
//Set the interpolation quality to medium
CGContextSetInterpolationQuality(ctx, kCGInterpolationMedium);
//Draw image scaled down to this 1x1 pixel
[[self gradientImage] drawInRect:(CGRect){.size = size} blendMode:kCGBlendModeCopy alpha:1];
//Read the RGB values from the context's buffer
uint8_t *data = CGBitmapContextGetData(ctx);
backgroundColor = [UIColor colorWithRed:data[2] / 255.0f
green:data[1] / 255.0f
blue:data[0] / 255.0f
alpha:1];
UIGraphicsEndImageContext();
}
//Calculate Luminance
CGFloat luminance;
CGFloat red = 0.0, green = 0.0, blue = 0.0, alpha1 = 0.0;
[backgroundColor getRed:&red green:&green blue:&blue alpha:&alpha1];
//Check if color is transparent
if (alpha == 0) {
return [UIColor clearColor];
}
// Relative luminance in colorimetric spaces - http://en.wikipedia.org/wiki/Luminance_(relative)
red *= 0.2126f; green *= 0.7152f; blue *= 0.0722f;
luminance = red + green + blue;
if (flat == NO) {
return (luminance > 0.6f) ? rgba(0, 0, 0, alpha) : rgba(255, 255, 255, alpha);
} else {
return (luminance > 0.6f) ? hsba(0, 0, 15, alpha) : hsba(192, 2, 95, alpha);
}
}
+ (UIColor *)colorWithGradientStyle:(UIGradientStyle)gradientStyle withFrame:(CGRect)frame andColors:(NSArray<UIColor *> * _Nonnull)colors; {
//Create our background gradient layer
CAGradientLayer *backgroundGradientLayer = [CAGradientLayer layer];
//Set the frame to our object's bounds
backgroundGradientLayer.frame = frame;
//To simplfy formatting, we'll iterate through our colors array and create a mutable array with their CG counterparts
NSMutableArray *cgColors = [[NSMutableArray alloc] init];
for (UIColor *color in colors) {
[cgColors addObject:(id)[color CGColor]];
}
switch (gradientStyle) {
case UIGradientStyleLeftToRight: {
//Set out gradient's colors
backgroundGradientLayer.colors = cgColors;
//Specify the direction our gradient will take
[backgroundGradientLayer setStartPoint:CGPointMake(0.0, 0.5)];
[backgroundGradientLayer setEndPoint:CGPointMake(1.0, 0.5)];
//Convert our CALayer to a UIImage object
UIGraphicsBeginImageContextWithOptions(backgroundGradientLayer.bounds.size,NO, [UIScreen mainScreen].scale);
[backgroundGradientLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *backgroundColorImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self setGradientImage:backgroundColorImage];
return [UIColor colorWithPatternImage:backgroundColorImage];
}
case UIGradientStyleRadial: {
UIGraphicsBeginImageContextWithOptions(frame.size,NO, [UIScreen mainScreen].scale);
//Specific the spread of the gradient (For now this gradient only takes 2 locations)
CGFloat locations[2] = {0.0, 1.0};
//Default to the RGB Colorspace
CGColorSpaceRef myColorspace = CGColorSpaceCreateDeviceRGB();
CFArrayRef arrayRef = (__bridge CFArrayRef)cgColors;
//Create our Fradient
CGGradientRef myGradient = CGGradientCreateWithColors(myColorspace, arrayRef, locations);
// Normalise the 0-1 ranged inputs to the width of the image
CGPoint myCentrePoint = CGPointMake(0.5 * frame.size.width, 0.5 * frame.size.height);
float myRadius = MIN(frame.size.width, frame.size.height) * 0.5;
// Draw our Gradient
CGContextDrawRadialGradient (UIGraphicsGetCurrentContext(), myGradient, myCentrePoint,
0, myCentrePoint, myRadius,
kCGGradientDrawsAfterEndLocation);
// Grab it as an Image
UIImage *backgroundColorImage = UIGraphicsGetImageFromCurrentImageContext();
// Clean up
CGColorSpaceRelease(myColorspace); // Necessary?
CGGradientRelease(myGradient); // Necessary?
UIGraphicsEndImageContext();
[self setGradientImage:backgroundColorImage];
return [UIColor colorWithPatternImage:backgroundColorImage];
}
case UIGradientStyleDiagonal: {
//Set out gradient's colors
backgroundGradientLayer.colors = cgColors;
//Specify the direction our gradient will take
[backgroundGradientLayer setStartPoint:CGPointMake(0.0, 1.0)];
[backgroundGradientLayer setEndPoint:CGPointMake(1.0, 0.0)];
//Convert our CALayer to a UIImage object
UIGraphicsBeginImageContextWithOptions(backgroundGradientLayer.bounds.size,NO, [UIScreen mainScreen].scale);
[backgroundGradientLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *backgroundColorImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self setGradientImage:backgroundColorImage];
return [UIColor colorWithPatternImage:backgroundColorImage];
}
case UIGradientStyleTopToBottom:
default: {
//Set out gradient's colors
backgroundGradientLayer.colors = cgColors;
//Convert our CALayer to a UIImage object
UIGraphicsBeginImageContextWithOptions(backgroundGradientLayer.bounds.size,NO, [UIScreen mainScreen].scale);
[backgroundGradientLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *backgroundColorImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self setGradientImage:backgroundColorImage];
return [UIColor colorWithPatternImage:backgroundColorImage];
}
}
}
+ (UIColor * _Nullable)colorWithHexString:(NSString * _Nonnull)string {
//Color with string and a defualt alpha value of 1.0
return [self colorWithHexString:string withAlpha:1.0];
}
+ (UIColor * _Nullable)colorWithHexString:(NSString * _Nonnull)string withAlpha:(CGFloat)alpha {
//Quick return in case string is empty
if (string.length == 0) {
return nil;
}
//Check to see if we need to add a hashtag
if('#' != [string characterAtIndex:0]) {
string = [NSString stringWithFormat:@"#%@", string];
}
//Make sure we have a working string length
if (string.length != 7 && string.length != 4) {
#ifdef DEBUG
NSLog(@"Unsupported string format: %@", string);
#endif
return nil;
}
//Check for short hex strings
if(string.length == 4) {
//Convert to full length hex string
string = [NSString stringWithFormat:@"#%@%@%@%@%@%@",
[string substringWithRange:NSMakeRange(1, 1)],[string substringWithRange:NSMakeRange(1, 1)],
[string substringWithRange:NSMakeRange(2, 1)],[string substringWithRange:NSMakeRange(2, 1)],
[string substringWithRange:NSMakeRange(3, 1)],[string substringWithRange:NSMakeRange(3, 1)]];
}
NSString *redHex = [NSString stringWithFormat:@"0x%@", [string substringWithRange:NSMakeRange(1, 2)]];
unsigned red = [[self class] hexValueToUnsigned:redHex];
NSString *greenHex = [NSString stringWithFormat:@"0x%@", [string substringWithRange:NSMakeRange(3, 2)]];
unsigned green = [[self class] hexValueToUnsigned:greenHex];
NSString *blueHex = [NSString stringWithFormat:@"0x%@", [string substringWithRange:NSMakeRange(5, 2)]];
unsigned blue = [[self class] hexValueToUnsigned:blueHex];
return [UIColor colorWithRed:(float)red/255 green:(float)green/255 blue:(float)blue/255 alpha:alpha];
}
+ (unsigned)hexValueToUnsigned:(NSString *)hexValue {
//Define default unsigned value
unsigned value = 0;
//Scan unsigned value
NSScanner *hexValueScanner = [NSScanner scannerWithString:hexValue];
[hexValueScanner scanHexInt:&value];
//Return found value
return value;
}
#pragma mark - Chameleon - Random Color Methods
+ (NSInteger)generateRandomNumberWithMax:(NSInteger)max {
//Choose a random number between 0 and our number of available colors
return arc4random_uniform((UInt32)max);
}
+ (UIColor *)randomFlatColor {
//Number of colors to choose from
const uint32_t numberOfPossibleColors = 48;
//Chose one of those colors at random
NSInteger randomColorChosen = [[self class] generateRandomNumberWithMax:numberOfPossibleColors];
//Check if a previous random number exists
if (![[NSUserDefaults standardUserDefaults] integerForKey:@"previousRandomNumber"]) {
//If no previous number exists, save it as such and find the matching color
[[NSUserDefaults standardUserDefaults] setInteger:randomColorChosen forKey:@"previousRandomNumber"];
[[NSUserDefaults standardUserDefaults] synchronize];
} else {
//Keep generating a random number until it is different than the one generated last time
while (randomColorChosen == [[NSUserDefaults standardUserDefaults] integerForKey:@"previousRandomNumber"]) {
randomColorChosen = [[self class] generateRandomNumberWithMax:numberOfPossibleColors];
}
//Once a new number has been generated then store it as the previous number for next time and proceed
[[NSUserDefaults standardUserDefaults] setInteger:randomColorChosen forKey:@"previousRandomNumber"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
return [self flatColors][randomColorChosen];
}
+ (UIColor * _Nullable)colorWithRandomColorInArray:(NSArray<UIColor *> *)colors {
UIColor *randomColor;
if (colors.count) {
//Pick a random index
NSInteger randomIndex = arc4random() % colors.count;
//Return the color at the random index
randomColor = colors[randomIndex];
} else {
return nil;
}
NSAssert([randomColor isKindOfClass:[UIColor class]], @"Hmm... one of your objects in your 'colors' array is not a UIColor object.");
//Return
return randomColor;
}
+ (UIColor *)colorWithRandomFlatColorExcludingColorsInArray:(NSArray<UIColor *> *)colors {
//Set random flat color
UIColor *randomColor = [[self class] randomFlatColor];
//If the selected color is blacklisted select a new color
while ([colors containsObject:randomColor]) {
randomColor = [[self class] randomFlatColor];
}
//Return
return randomColor;
}
+ (UIColor *)colorWithRandomFlatColorOfShadeStyle:(UIShadeStyle)shadeStyle {
//Return color with default alpha value of 1.0
return [[self class] colorWithRandomFlatColorOfShadeStyle:shadeStyle withAlpha:1.0];
}
+ (UIColor *)colorWithRandomFlatColorOfShadeStyle:(UIShadeStyle)shadeStyle withAlpha:(CGFloat)alpha {
//Number of colors to choose from
const NSInteger numberOfPossibleColors = 24;
//Chose one of those colors at random
NSInteger randomColorChosen = [[self class] generateRandomNumberWithMax:numberOfPossibleColors];
//Check if a previous random number exists
if (![[NSUserDefaults standardUserDefaults] integerForKey:@"previousRandomNumber"]) {
//If no previous number exists, save it as such and find the matching color
[[NSUserDefaults standardUserDefaults] setInteger:randomColorChosen forKey:@"previousRandomNumber"];
[[NSUserDefaults standardUserDefaults] synchronize];
} else {
//Keep generating a random number until it is different than the one generated last time
while (randomColorChosen == [[NSUserDefaults standardUserDefaults] integerForKey:@"previousRandomNumber"]) {
randomColorChosen = [[self class] generateRandomNumberWithMax:numberOfPossibleColors];
}
//Once a new number has been generated then store it as the previous number for next time and proceed
[[NSUserDefaults standardUserDefaults] setInteger:randomColorChosen forKey:@"previousRandomNumber"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
//Assign a random color based on randomColorChosen
UIColor *randomColor;
//Return a color depending on the specified shade
switch (shadeStyle) {
case UIShadeStyleDark: {
NSArray *darkColors = @[FlatBlackDark, FlatBlueDark, FlatBrownDark, FlatCoffeeDark, FlatForestGreenDark, FlatGrayDark, FlatGreenDark, FlatLimeDark, FlatMagentaDark, FlatMaroonDark, FlatMintDark, FlatNavyBlueDark, FlatOrangeDark, FlatPinkDark, FlatPlumDark, FlatPowderBlueDark, FlatPurpleDark, FlatRedDark, FlatSandDark, FlatSkyBlueDark, FlatTealDark, FlatWatermelonDark, FlatWhiteDark, FlatYellowDark];
randomColor = darkColors[randomColorChosen];
break;
}
case UIShadeStyleLight:
default: {
NSArray *lightColors = @[FlatBlack, FlatBlue, FlatBrown, FlatCoffee, FlatForestGreen, FlatGray, FlatGreen, FlatLime, FlatMagenta, FlatMaroon, FlatMint, FlatNavyBlue, FlatOrange, FlatPink, FlatPlum, FlatPowderBlue, FlatPurple, FlatRed, FlatSand, FlatSkyBlue, FlatTeal, FlatWatermelon, FlatWhite, FlatYellow];
randomColor = lightColors[randomColorChosen];
break;
}
}
//Return color with correct alpha value
randomColor = [randomColor colorWithAlphaComponent:alpha];
return randomColor;
}
#pragma mark - Chameleon Instance Methods
- (UIColor *)flatten {
return [UIColor colorWithFlatVersionFrom:self];
}
- (NSString *)hexValue {
UIColor *currentColor = self;
if (CGColorGetNumberOfComponents(self.CGColor) < 4) {
const CGFloat *components = CGColorGetComponents(self.CGColor);
currentColor = [UIColor colorWithRed:components[0] green:components[0] blue:components[0] alpha:components[1]];
}
if (CGColorSpaceGetModel(CGColorGetColorSpace(currentColor.CGColor)) != kCGColorSpaceModelRGB) {
return [NSString stringWithFormat:@"#FFFFFF"];
}
return [NSString stringWithFormat:@"#%02X%02X%02X", (int)((CGColorGetComponents(currentColor.CGColor))[0]*255.0), (int)((CGColorGetComponents(currentColor.CGColor))[1]*255.0), (int)((CGColorGetComponents(currentColor.CGColor))[2]*255.0)];
}
- (UIColor * _Nullable)darkenByPercentage:(CGFloat)percentage {
//Define HSBA values
CGFloat h, s, b, a;
//Check if HSBA values exist
if ([self getHue:&h saturation:&s brightness:&b alpha:&a]) {
//Make sure our percentage is greater than 0
if (percentage > 0) {
b = MIN(b - percentage, 1.0);
}
//Return darker color
return [UIColor colorWithHue:h saturation:s brightness:b alpha:a];
}
return nil;
}
- (UIColor * _Nullable)lightenByPercentage:(CGFloat)percentage {
//Define HSBA values
CGFloat h, s, b, a;
//Check if HSBA values exist
if ([self getHue:&h saturation:&s brightness:&b alpha:&a]) {
//Make sure our percentage is greater than 0
if (percentage > 0) {
b = MIN(b + percentage, 1.0);
}
//Return lighter color
return [UIColor colorWithHue:h saturation:s brightness:b alpha:a];
}
return nil;
}
- (UIColor * _Nullable)desaturateByPercentage:(CGFloat)percentage {
//Define HSBA values
CGFloat h, s, b, a;
//Check if HSBA values exist
if ([self getHue:&h saturation:&s brightness:&b alpha:&a]) {
//Make sure our percentage is greater than 0
if (percentage > 0) {
s = MIN(s - percentage, 1.0);
}
//Return darker color
return [UIColor colorWithHue:h saturation:s brightness:b alpha:a];
}
return nil;
}
- (UIColor * _Nullable)saturateByPercentage:(CGFloat)percentage {
//Define HSBA values
CGFloat h, s, b, a;
//Check if HSBA values exist
if ([self getHue:&h saturation:&s brightness:&b alpha:&a]) {
//Make sure our percentage is greater than 0
if (percentage > 0) {
s = MIN(s + percentage, 1.0);
}
//Return lighter color
return [UIColor colorWithHue:h saturation:s brightness:b alpha:a];
}
return nil;
}
- (BOOL)isEqualToColor:(UIColor *)color {
//Check if both colors are in the Monochrome / RGB color space
if ([self isMonochromeOrRGB] && [color isMonochromeOrRGB]) {
//Return comparison
return self.RGBAValue == color.RGBAValue;
}
//Return comparison
return [self isEqual:color];
}
#pragma mark - Chameleon Internal Methods
//Array of all our colors
+ (NSArray *)flatColors {
return @[FlatBlack, FlatBlackDark, FlatBlue, FlatBlueDark, FlatBrown, FlatBrownDark, FlatCoffee, FlatCoffeeDark, FlatForestGreen, FlatForestGreenDark, FlatGray, FlatGrayDark, FlatGreen, FlatGreenDark, FlatLime, FlatLimeDark, FlatMagenta, FlatMagentaDark, FlatMaroon, FlatMaroonDark, FlatMint, FlatMintDark, FlatNavyBlue, FlatNavyBlueDark, FlatOrange, FlatOrangeDark, FlatPink, FlatPinkDark, FlatPlum, FlatPlumDark, FlatPowderBlue, FlatPowderBlueDark, FlatPurple, FlatPurpleDark, FlatRed, FlatRedDark, FlatSand, FlatSandDark, FlatSkyBlue, FlatSkyBlueDark, FlatTeal, FlatTealDark, FlatWatermelon, FlatWatermelonDark, FlatWhite, FlatWhiteDark, FlatYellow, FlatYellowDark];
}
- (uint32_t)RGBAValue {
CGFloat rgba[4];
[self getRGBAComponents:rgba];
uint8_t red = rgba[0]*255;
uint8_t green = rgba[1]*255;
uint8_t blue = rgba[2]*255;
uint8_t alpha = rgba[3]*255;
return (red << 24) + (green << 16) + (blue << 8) + alpha;
}
- (void)getRGBAComponents:(CGFloat[4])rgba {
CGColorSpaceModel model = CGColorSpaceGetModel(CGColorGetColorSpace(self.CGColor));
const CGFloat *components = CGColorGetComponents(self.CGColor);
switch (model) {
case kCGColorSpaceModelMonochrome: {
rgba[0] = components[0];
rgba[1] = components[0];
rgba[2] = components[0];
rgba[3] = components[1];
break;
}
case kCGColorSpaceModelRGB: {
rgba[0] = components[0];
rgba[1] = components[1];
rgba[2] = components[2];
rgba[3] = components[3];
break;
}
case kCGColorSpaceModelCMYK:
case kCGColorSpaceModelDeviceN:
case kCGColorSpaceModelIndexed:
case kCGColorSpaceModelLab:
case kCGColorSpaceModelPattern:
case kCGColorSpaceModelUnknown: {
#ifdef DEBUG
NSLog(@"Unsupported color model: %i", model);
#endif
rgba[0] = 0.0f;
rgba[1] = 0.0f;
rgba[2] = 0.0f;
rgba[3] = 1.0f;
break;
}
}
}
//Check if color is in the monochrome or rgb color space
- (BOOL)isMonochromeOrRGB {
CGColorSpaceModel model = CGColorSpaceGetModel(CGColorGetColorSpace(self.CGColor));
return model == kCGColorSpaceModelMonochrome || model == kCGColorSpaceModelRGB;
}
//Calculate the total sum of differences - Euclidian distance
//Chameleon is now using the CIEDE2000 formula to calculate distances between 2 colors.
//More info: http://en.wikipedia.org/wiki/Color_difference
+ (float)totalSumOfDifferencesFroml1:(CGFloat)L1 l2:(CGFloat)L2 a1:(CGFloat)A1
a2:(CGFloat)A2 b1:(CGFloat)B1 b2:(CGFloat)B2 {
//Get C Values in LCH from LAB Values
CGFloat C1 = sqrt(pow(A1, 2) + pow(B1, 2));
CGFloat C2 = sqrt(pow(A2, 2) + pow(B2, 2));
//CIE Weights
CGFloat KL = 1;
CGFloat KC = 1;
CGFloat KH = 1;
//Variables specifically set for CIE:2000
CGFloat DeltaPrimeL = L2 - L1;
CGFloat MeanL = ((L1 + L2) / 2);
CGFloat MeanC = ((C1 + C2) / 2);
CGFloat A1Prime = A1 + A1 / 2 * (1 - sqrt(pow(MeanC, 7.0) / (pow(MeanC, 7.0) + pow(25.0, 7.0))));
CGFloat A2Prime = A2 + A2 / 2 * (1 - sqrt(pow(MeanC, 7.0) / (pow(MeanC, 7.0) + pow(25.0, 7.0))));
CGFloat C1Prime = sqrt(pow(A1Prime, 2) + pow(B1, 2));
CGFloat C2Prime = sqrt(pow(A2Prime, 2) + pow(B2, 2));
CGFloat DeltaPrimeC = C1Prime - C2Prime;
CGFloat DeltaC = C1 - C2;
CGFloat MeanCPrime = (C1Prime + C2Prime) / 2;
CGFloat H1Prime = fmodf(atan2(B1, A1Prime), (360.0 * M_PI/180));
CGFloat H2Prime = fmodf(atan2(B2, A2Prime), (360.0 * M_PI/180));
//Run everything through our △H' Function
CGFloat hDeltaPrime = 0;
if (fabs(H1Prime - H2Prime) <= (180.0 * M_PI/180)) {
hDeltaPrime = H2Prime - H1Prime;
} else if (H2Prime <= H1Prime) {
hDeltaPrime = (H2Prime - H1Prime) + ((360.0 * M_PI/180));