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