forked from alejandrojapkin/CocoaLUT
/
LUTColor.m
234 lines (185 loc) · 10.2 KB
/
LUTColor.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
//
// LUTColor.m
// DropLUT
//
// Created by Wil Gieseler on 12/15/13.
// Copyright (c) 2013 Wil Gieseler. All rights reserved.
//
#import "LUTColor.h"
#import "LUTHelper.h"
#define LUT_COLOR_EQUALITY_ERROR_MARGIN .0005
@implementation LUTColor
+ (instancetype)colorWithRed:(LUTColorValue)r green:(LUTColorValue)g blue:(LUTColorValue)b {
LUTColor *color = [[LUTColor alloc] init];
color.red = isnan(r) ? 0 : r;
color.green = isnan(g) ? 0 : g;
color.blue = isnan(b) ? 0 : b;
return color;
}
+ (instancetype)colorFromIntegersWithBitDepth:(NSUInteger)bitdepth red:(NSUInteger)r green:(NSUInteger)g blue:(NSUInteger)b {
NSUInteger maxBits = pow(2, bitdepth) - 1;
return [LUTColor colorWithRed:nsremapint01(r, maxBits) green:nsremapint01(g, maxBits) blue:nsremapint01(b, maxBits)];
}
+ (instancetype)colorFromIntegersWithMaxOutputValue:(NSUInteger)maxOutputValue red:(NSUInteger)r green:(NSUInteger)g blue:(NSUInteger)b {
return [LUTColor colorWithRed:nsremapint01(r, maxOutputValue) green:nsremapint01(g, maxOutputValue) blue:nsremapint01(b, maxOutputValue)];
}
- (LUTColor *)clampedWithLowerBound:(double)lowerBound
upperBound:(double)upperBound{
return [LUTColor colorWithRed:clamp(self.red, lowerBound, upperBound)
green:clamp(self.green, lowerBound, upperBound)
blue:clamp(self.blue, lowerBound, upperBound)];
}
- (LUTColor *)clampedWithLowerBoundOnly:(double)lowerBound{
return [LUTColor colorWithRed:clampLowerBound(self.red, lowerBound)
green:clampLowerBound(self.green, lowerBound)
blue:clampLowerBound(self.blue, lowerBound)];
}
- (LUTColor *)clampedWithUpperBoundOnly:(double)upperBound{
return [LUTColor colorWithRed:clampUpperBound(self.red, upperBound)
green:clampUpperBound(self.green, upperBound)
blue:clampUpperBound(self.blue, upperBound)];
}
- (LUTColor *)contrastStretchWithCurrentMin:(double)currentMin
currentMax:(double)currentMax
finalMin:(double)finalMin
finalMax:(double)finalMax{
return [LUTColor colorWithRed:contrastStretch(self.red, currentMin, currentMax, finalMin, finalMax)
green:contrastStretch(self.green, currentMin, currentMax, finalMin, finalMax)
blue:contrastStretch(self.blue, currentMin, currentMax, finalMin, finalMax)];
}
- (LUTColor *)colorByMultiplyingByNumber:(double)number{
return [LUTColor colorWithRed:self.red*number green:self.green*number blue:self.blue*number];
}
- (LUTColor *)colorByMultiplyingColor:(LUTColor *)offsetColor{
return [LUTColor colorWithRed:self.red*offsetColor.red green:self.green*offsetColor.green blue:self.blue*offsetColor.blue];
}
- (LUTColor *)colorByAddingColor:(LUTColor *)offsetColor{
return [LUTColor colorWithRed:self.red+offsetColor.red green:self.green+offsetColor.green blue:self.blue+offsetColor.blue];
}
- (LUTColor *)colorBySubtractingColor:(LUTColor *)offsetColor{
return [LUTColor colorWithRed:self.red-offsetColor.red green:self.green-offsetColor.green blue:self.blue-offsetColor.blue];
}
- (LUTColor *)colorByInvertingColorWithMinimumValue:(double)minimumValue
maximumValue:(double)maximumValue{
double distance = abs(maximumValue-minimumValue);
return [LUTColor colorWithRed:distance - self.red green:distance - self.green blue:distance - self.blue];
}
//thanks http://git.dyne.org/frei0r/plain/src/filter/sopsat/sopsat.cpp
// The "saturation" parameter works like this:
// 0.0 creates a black-and-white image.
// 0.5 reduces the color saturation by half.
// 1.0 causes no change.
// 2.0 doubles the color saturation.
// Note: A "change" value greater than 1.0 may project your RGB values
// beyond their normal range, in which case you probably should truncate
// them to the desired range before trying to use them in an image.
// ex: REC709 luma: 0.212636821677 R + 0.715182981841 G + 0.0721801964814 B
/// AlexaWideGamut: 0.291948669899 R + 0.823830265984 G + -0.115778935883 B
- (LUTColor *)colorByChangingSaturation:(double)saturation
usingLumaR:(double)lumaR
lumaG:(double)lumaG
lumaB:(double)lumaB{
double luma = (self.red)*lumaR + (self.green)*lumaG + (self.blue)*lumaB;
return [LUTColor colorWithRed:luma + saturation * (self.red - luma)
green:luma + saturation * (self.green - luma)
blue:luma + saturation * (self.blue - luma)];
}
//thanks http://en.wikipedia.org/wiki/ASC_CDL
- (LUTColor *)colorByApplyingRedSlope:(double)redSlope
redOffset:(double)redOffset
redPower:(double)redPower
greenSlope:(double)greenSlope
greenOffset:(double)greenOffset
greenPower:(double)greenPower
blueSlope:(double)blueSlope
blueOffset:(double)blueOffset
bluePower:(double)bluePower {
redSlope = clampLowerBound(redSlope, 0);
redPower = clampLowerBound(redPower, 0);
greenSlope = clampLowerBound(greenSlope, 0);
greenPower = clampLowerBound(greenPower, 0);
blueSlope = clampLowerBound(blueSlope, 0);
bluePower = clampLowerBound(bluePower, 0);
double newRed = pow(self.red*redSlope + redOffset, redPower);
double newGreen = pow(self.green*greenSlope + greenOffset, greenPower);
double newBlue = pow(self.blue*blueSlope + blueOffset, bluePower);
return [LUTColor colorWithRed:newRed green:newGreen blue:newBlue];
}
- (LUTColor *)clamped01 {
return [LUTColor colorWithRed:clamp01(self.red) green:clamp01(self.green) blue:clamp01(self.blue)];
}
- (LUTColor *)lerpTo:(LUTColor *)otherColor amount:(double)amount {
return [LUTColor colorWithRed:lerp1d(self.red, otherColor.red, amount)
green:lerp1d(self.green, otherColor.green, amount)
blue:lerp1d(self.blue, otherColor.blue, amount)];
}
- (LUTColor *)remappedFromInputLow:(double)inputLow
inputHigh:(double)inputHigh
outputLow:(double)outputLow
outputHigh:(double)outputHigh
bounded:(BOOL)bounded{
if(!bounded){
return [LUTColor colorWithRed:remapNoError(self.red, inputLow, inputHigh, outputLow, outputHigh)
green:remapNoError(self.green, inputLow, inputHigh, outputLow, outputHigh)
blue:remapNoError(self.blue, inputLow, inputHigh, outputLow, outputHigh)];
}
else{
return [LUTColor colorWithRed:remap(self.red, inputLow, inputHigh, outputLow, outputHigh)
green:remap(self.green, inputLow, inputHigh, outputLow, outputHigh)
blue:remap(self.blue, inputLow, inputHigh, outputLow, outputHigh)];
}
}
- (NSString *)description{
return [NSString stringWithFormat:@"(%.6f, %.6f, %.6f)", self.red, self.green, self.blue];
}
- (NSAttributedString *)colorizedAttributedStringWithFormat:(NSString *)formatString{
NSString *redString = [NSString stringWithFormat:[NSString stringWithFormat:@"%@", formatString], self.red];
NSString *greenString = [NSString stringWithFormat:[NSString stringWithFormat:@"%@", formatString], self.green];
NSString *blueString = [NSString stringWithFormat:[NSString stringWithFormat:@"%@", formatString], self.blue];
NSAttributedString *redColoredString = [[NSAttributedString alloc] initWithString:redString attributes:@{NSForegroundColorAttributeName: [SystemColor redColor]}];
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
SystemColor *green = SystemColor.greenColor;
#elif TARGET_OS_MAC
SystemColor *green = [SystemColor colorWithDeviceRed:0 green:.8 blue:0 alpha:1];
#endif
NSAttributedString *greenColoredString = [[NSAttributedString alloc] initWithString:greenString attributes:@{NSForegroundColorAttributeName: green}];//custom green color because pure green is really hard to read.
NSAttributedString *blueColoredString = [[NSAttributedString alloc] initWithString:blueString attributes:@{NSForegroundColorAttributeName: [SystemColor blueColor]}];
NSMutableAttributedString *outString = [[NSMutableAttributedString alloc] initWithString:@""];
[outString appendAttributedString:redColoredString];
[outString appendAttributedString:[[NSAttributedString alloc] initWithString:@", "]];
[outString appendAttributedString:greenColoredString];
[outString appendAttributedString:[[NSAttributedString alloc] initWithString:@", "]];
[outString appendAttributedString:blueColoredString];
[outString appendAttributedString:[[NSAttributedString alloc] initWithString:@""]];
return outString;
}
- (BOOL)isEqualToLUTColor:(LUTColor *)otherColor{
if (!otherColor){
return NO;
}
return fabs(self.red - otherColor.red) <= LUT_COLOR_EQUALITY_ERROR_MARGIN && fabs(self.green - otherColor.green) <= LUT_COLOR_EQUALITY_ERROR_MARGIN && fabs(self.blue - otherColor.blue) <= LUT_COLOR_EQUALITY_ERROR_MARGIN;
}
- (BOOL)isEqual:(id)object {
if (self == object) {
return YES;
}
if (![object isKindOfClass:[LUTColor class]]) {
return NO;
}
return [self isEqualToLUTColor:(LUTColor *)object];
}
- (SystemColor *)systemColor {
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
return [UIColor colorWithRed:self.red green:self.green blue:self.blue alpha:1];
#elif TARGET_OS_MAC
return [NSColor colorWithDeviceRed:self.red green:self.green blue:self.blue alpha:1];
#endif
}
+ (instancetype)colorWithSystemColor:(SystemColor *)color {
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
return [LUTColor colorWithRed:[[color CIColor] red] green:[[color CIColor] green] blue:[[color CIColor] blue]];
#elif TARGET_OS_MAC
return [LUTColor colorWithRed:color.redComponent green:color.greenComponent blue:color.blueComponent];
#endif
}
@end