|
@@ -1,157 +0,0 @@
|
1
|
|
-//
|
2
|
|
-// Color+Interpolation.m
|
3
|
|
-//
|
4
|
|
-// Created by Leo Natan on 01/10/2016.
|
5
|
|
-// Copyright © 2016 Leo Natan. All rights reserved.
|
6
|
|
-//
|
7
|
|
-
|
8
|
|
-#if __has_include(<UIKit/UIKit.h>) || __has_include(<AppKit/AppKit.h>)
|
9
|
|
-
|
10
|
|
-#import "Color+Interpolation.h"
|
11
|
|
-
|
12
|
|
-#if __has_include(<UIKit/UIKit.h>)
|
13
|
|
-#define Color UIColor
|
14
|
|
-#else
|
15
|
|
-#define Color NSColor
|
16
|
|
-#endif
|
17
|
|
-
|
18
|
|
-#define SWAP(x, y) do { __typeof(x) __ZZZZ__SWAP = x; x = y; y = __ZZZZ__SWAP; } while(0)
|
19
|
|
-
|
20
|
|
-//Same value as LNInterpolationBehaviorUseDefault
|
21
|
|
-LNInterpolationBehavior const LNInterpolationBehaviorUseLABColorSpace = @"LNInterpolationBehaviorUseDefault";
|
22
|
|
-LNInterpolationBehavior const LNInterpolationBehaviorUseRGBColorSpace = @"LNInterpolationBehaviorUseRGB";
|
23
|
|
-
|
24
|
|
-extern double LNLinearInterpolate(double from, double to, double p);
|
25
|
|
-
|
26
|
|
-static NSArray<NSNumber*>* LNRGBComponentsFromColor(Color* color)
|
27
|
|
-{
|
28
|
|
- size_t numberOfComponents = CGColorGetNumberOfComponents(color.CGColor);
|
29
|
|
- const CGFloat* components = CGColorGetComponents(color.CGColor);
|
30
|
|
-
|
31
|
|
- return numberOfComponents == 2 ? @[@(components[0]), @(components[0]), @(components[0]), @(components[1])] : @[@(components[0]), @(components[1]), @(components[2]), @(components[3])];
|
32
|
|
-}
|
33
|
|
-
|
34
|
|
-static Color* LNColorFromRGBComponents(NSArray<NSNumber*>* components)
|
35
|
|
-{
|
36
|
|
- return [Color colorWithRed:components[0].doubleValue green:components[1].doubleValue blue:components[2].doubleValue alpha:components[3].doubleValue];
|
37
|
|
-}
|
38
|
|
-
|
39
|
|
-static NSArray<NSNumber*>* LNLabComponentsFromColor(Color* color)
|
40
|
|
-{
|
41
|
|
- NSArray<NSNumber*>* rgbComponents = LNRGBComponentsFromColor(color);
|
42
|
|
- CGFloat r = rgbComponents[0].doubleValue;
|
43
|
|
- CGFloat g = rgbComponents[1].doubleValue;
|
44
|
|
- CGFloat b = rgbComponents[2].doubleValue;
|
45
|
|
-
|
46
|
|
- //RGB -> XYZ
|
47
|
|
- //http://www.brucelindbloom.com/index.html?Eqn_RGB_to_XYZ.html
|
48
|
|
-
|
49
|
|
- r = (r > 0.04045) ? pow((r + 0.055) / 1.055, 2.4) : (r / 12.92);
|
50
|
|
- g = (g > 0.04045) ? pow((g + 0.055) / 1.055, 2.4) : (g / 12.92);
|
51
|
|
- b = (b > 0.04045) ? pow((b + 0.055) / 1.055, 2.4) : (b / 12.92);
|
52
|
|
-
|
53
|
|
- //http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html (sRGB -> XYZ)
|
54
|
|
- CGFloat x = r * 41.24564 + g * 35.75761 + b * 18.04375;
|
55
|
|
- CGFloat y = r * 21.26729 + g * 71.51522 + b * 07.21750;
|
56
|
|
- CGFloat z = r * 01.93339 + g * 11.91920 + b * 95.03041;
|
57
|
|
-
|
58
|
|
- //XYZ -> Lab
|
59
|
|
- //http://www.brucelindbloom.com/index.html?Eqn_XYZ_to_Lab.html
|
60
|
|
-
|
61
|
|
- static const CGFloat eps = 216.0 / 24389.0;
|
62
|
|
- static const CGFloat k = 24389.0 / 27.0;
|
63
|
|
-
|
64
|
|
- x = x / 100.0;//95.047;
|
65
|
|
- y = y / 100.0;//100.000;
|
66
|
|
- z = z / 100.0;//108.883;
|
67
|
|
-
|
68
|
|
- x = x > eps ? pow(x, 1.0 / 3.0) : (k * x + 16.0) / 116.0;
|
69
|
|
- y = y > eps ? pow(y, 1.0 / 3.0) : (k * y + 16.0) / 116.0;
|
70
|
|
- z = z > eps ? pow(z, 1.0 / 3.0) : (k * z + 16.0) / 116.0;
|
71
|
|
-
|
72
|
|
- CGFloat l = 116 * y - 16;
|
73
|
|
- CGFloat a = 500 * (x - y);
|
74
|
|
- b = 200 * (y - z);
|
75
|
|
-
|
76
|
|
- return @[@(l), @(a), @(b), rgbComponents[3]];
|
77
|
|
-}
|
78
|
|
-
|
79
|
|
-static Color* LNColorFromLabComponents(NSArray<NSNumber*>* components)
|
80
|
|
-{
|
81
|
|
- CGFloat l = components[0].doubleValue;
|
82
|
|
- CGFloat a = components[1].doubleValue;
|
83
|
|
- CGFloat b = components[2].doubleValue;
|
84
|
|
-
|
85
|
|
- //Lab -> XYZ
|
86
|
|
- //http://www.brucelindbloom.com/index.html?Eqn_Lab_to_XYZ.html
|
87
|
|
-
|
88
|
|
- static const CGFloat eps = 216.0 / 24389.0;
|
89
|
|
- static const CGFloat k = 24389.0 / 27.0;
|
90
|
|
-
|
91
|
|
- CGFloat y = (l + 16.0) / 116.0;
|
92
|
|
- CGFloat x = a / 500 + y;
|
93
|
|
- CGFloat z = y - b / 200;
|
94
|
|
-
|
95
|
|
- x = pow(x, 3.0) > eps ? pow(x, 3.0) : (116 * x - 16) / k;
|
96
|
|
- y = l > k * eps ? pow((l + 16) / 116, 3.0) : l / k;
|
97
|
|
- z = pow(z, 3.0) > eps ? pow(z, 3.0) : (116 * z - 16) / k;
|
98
|
|
-
|
99
|
|
- x = x * 1.0;//.95047;
|
100
|
|
- y = y * 1.0;//1.00000;
|
101
|
|
- z = z * 1.0;//1.08883;
|
102
|
|
-
|
103
|
|
- //XYZ -> RGB
|
104
|
|
-
|
105
|
|
- //http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html (XYZ -> sRGB)
|
106
|
|
- CGFloat r = x * 3.2404542 + y * -1.5371385 + z * -0.4985314;
|
107
|
|
- CGFloat g = x * -0.9692660 + y * 1.8760108 + z * 0.0415560;
|
108
|
|
- b = x * 0.0556434 + y * -0.2040259 + z * 1.0572252;
|
109
|
|
-
|
110
|
|
- r = r <= 0.0031308 ? 12.92 * r : 1.055 * pow(r, 1.0 / 2.4) - 0.055;
|
111
|
|
- g = g <= 0.0031308 ? 12.92 * g : 1.055 * pow(g, 1.0 / 2.4) - 0.055;
|
112
|
|
- b = b <= 0.0031308 ? 12.92 * b : 1.055 * pow(b, 1.0 / 2.4) - 0.055;
|
113
|
|
-
|
114
|
|
- // return Color
|
115
|
|
- return LNColorFromRGBComponents(@[@(r), @(g), @(b), components[3]]);
|
116
|
|
-}
|
117
|
|
-
|
118
|
|
-static inline __attribute__((__always_inline__)) Color* LNInterpolateColor(Color* fromValue, Color* toValue, CGFloat p, NSArray* (*compConverter)(Color*), Color* (*colorConverter)(NSArray*))
|
119
|
|
-{
|
120
|
|
- NSArray<NSNumber*>* arrayC1 = compConverter(fromValue);
|
121
|
|
- NSArray<NSNumber*>* arrayC2 = compConverter(toValue);
|
122
|
|
-
|
123
|
|
- NSMutableArray<NSNumber*>* arrayOutput = [NSMutableArray new];
|
124
|
|
- [arrayC1 enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
|
125
|
|
- arrayOutput[idx] = @(LNLinearInterpolate(obj.doubleValue, arrayC2[idx].doubleValue, p));
|
126
|
|
- }];
|
127
|
|
-
|
128
|
|
- return colorConverter(arrayOutput);
|
129
|
|
-}
|
130
|
|
-
|
131
|
|
-@implementation Color (Interpolation)
|
132
|
|
-
|
133
|
|
-- (instancetype)interpolateToValue:(Color*)toValue progress:(double)p
|
134
|
|
-{
|
135
|
|
- return [self interpolateToValue:toValue progress:p behavior:LNInterpolationBehaviorUseDefault];
|
136
|
|
-}
|
137
|
|
-
|
138
|
|
-- (instancetype)interpolateToValue:(id)toValue progress:(double)p behavior:(LNInterpolationBehavior)behavior
|
139
|
|
-{
|
140
|
|
- NSParameterAssert([toValue isKindOfClass:[Color class]]);
|
141
|
|
-
|
142
|
|
- if(p <= 0)
|
143
|
|
- {
|
144
|
|
- return self;
|
145
|
|
- }
|
146
|
|
-
|
147
|
|
- if(p >= 1)
|
148
|
|
- {
|
149
|
|
- return toValue;
|
150
|
|
- }
|
151
|
|
-
|
152
|
|
- return LNInterpolateColor(self, toValue, p, behavior == LNInterpolationBehaviorUseRGBColorSpace ? LNRGBComponentsFromColor : LNLabComponentsFromColor, behavior == LNInterpolationBehaviorUseRGBColorSpace ? LNColorFromRGBComponents : LNColorFromLabComponents);
|
153
|
|
-}
|
154
|
|
-
|
155
|
|
-@end
|
156
|
|
-
|
157
|
|
-#endif
|