ColorUnits.cpp
7.53 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
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
/**
\file ColorUnits.cpp
Copyright Notice\n
Copyright (C) 2020 Jan Rogall - developer\n
This file is part of hueplusplus.
hueplusplus is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
hueplusplus is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with hueplusplus. If not, see <http://www.gnu.org/licenses/>.
**/
#include <algorithm>
#include <cmath>
#include <hueplusplus/ColorUnits.h>
namespace hueplusplus
{
namespace
{
float sign(const XY& p0, const XY& p1, const XY& p2)
{
return (p0.x - p2.x) * (p1.y - p2.y) - (p1.x - p2.x) * (p0.y - p2.y);
}
bool isRightOf(const XY& xy, const XY& p1, const XY& p2)
{
return sign(xy, p1, p2) < 0;
}
XY projectOntoLine(const XY& xy, const XY& p1, const XY& p2)
{
// Using dot product to project onto line
// Vector AB = B - A
// Vector AX = X - A
// Projected length l = (AX dot AB) / len(AB)
// Result: E = A + l*AB/len(AB) = A + AB * (AX dot AB) / (len(AB))^2
const float abX = p2.x - p1.x;
const float abY = p2.y - p1.y;
const float lenABSquared = abX * abX + abY * abY;
const float dot = (xy.x - p1.x) * abX + (xy.y - p1.y) * abY;
const float eX = p1.x + abX * dot / lenABSquared;
const float eY = p1.y + abY * dot / lenABSquared;
return XY {eX, eY};
}
} // namespace
bool ColorGamut::contains(const XY& xy) const
{
return !isRightOf(xy, redCorner, greenCorner) && !isRightOf(xy, greenCorner, blueCorner)
&& !isRightOf(xy, blueCorner, redCorner);
}
XY ColorGamut::corrected(const XY& xy) const
{
// red, green and blue are in counterclockwise orientation
if (isRightOf(xy, redCorner, greenCorner))
{
// Outside of triangle, check whether to use nearest corner or point on line
if (isRightOf(xy, greenCorner, blueCorner))
{
// Point is outside of red-green line, closest to green corner
return greenCorner;
}
else if (isRightOf(xy, blueCorner, redCorner))
{
// Point is outside of red-green line, closest to red corner
return redCorner;
}
else
{
// Point is closest to line, project onto it
return projectOntoLine(xy, redCorner, greenCorner);
}
}
else if (isRightOf(xy, greenCorner, blueCorner))
{
// Green corner already checked above
if (isRightOf(xy, blueCorner, redCorner))
{
// Point is outside of green-blue line, closest to blue corner
return blueCorner;
}
else
{
return projectOntoLine(xy, greenCorner, blueCorner);
}
}
else if (isRightOf(xy, blueCorner, redCorner))
{
// All corners already checked
return projectOntoLine(xy, blueCorner, redCorner);
}
return xy;
}
XYBrightness RGB::toXY() const
{
if (r == 0 && g == 0 && b == 0)
{
// Return white with minimum brightness
return XYBrightness {XY {0.32272673f, 0.32902291f}, 0.f};
}
const float red = r / 255.f;
const float green = g / 255.f;
const float blue = b / 255.f;
const float redCorrected = (red > 0.04045f) ? pow((red + 0.055f) / (1.0f + 0.055f), 2.4f) : (red / 12.92f);
const float greenCorrected = (green > 0.04045f) ? pow((green + 0.055f) / (1.0f + 0.055f), 2.4f) : (green / 12.92f);
const float blueCorrected = (blue > 0.04045f) ? pow((blue + 0.055f) / (1.0f + 0.055f), 2.4f) : (blue / 12.92f);
const float X = redCorrected * 0.664511f + greenCorrected * 0.154324f + blueCorrected * 0.162028f;
const float Y = redCorrected * 0.283881f + greenCorrected * 0.668433f + blueCorrected * 0.047685f;
const float Z = redCorrected * 0.000088f + greenCorrected * 0.072310f + blueCorrected * 0.986039f;
const float x = X / (X + Y + Z);
const float y = Y / (X + Y + Z);
// Set brightness to the brightest channel value (rather than average of them),
// so full red/green/blue can be displayed
return XYBrightness {XY {x, y}, std::max({red, green, blue})};
}
XYBrightness RGB::toXY(const ColorGamut& gamut) const
{
XYBrightness xy = toXY();
if (!gamut.contains(xy.xy))
{
xy.xy = gamut.corrected(xy.xy);
}
return xy;
}
HueSaturation RGB::toHueSaturation() const
{
const uint8_t cmax = std::max(r, std::max(g, b));
const uint8_t cmin = std::min(r, std::min(g, b));
const float diff = cmax - cmin;
int h = -1;
int s = -1;
if (cmax == cmin)
{
h = 0;
}
else if (cmax == r)
{
h = (int)(9307 * ((g - b) / diff) + 65535) % 65535;
}
else if (cmax == g)
{
h = (int)(12750 * ((b - r) / diff) + 25500) % 65535;
}
else if (cmax == b)
{
h = (int)(10710 * ((r - g) / diff) + 46920) % 65535;
}
if (cmax == 0)
{
s = 0;
}
else
{
s = std::round((diff / cmax) * 254);
}
return {h, s};
}
RGB RGB::fromXY(const XYBrightness& xy)
{
if (xy.brightness < 1e-4)
{
return RGB {0, 0, 0};
}
const float z = 1.f - xy.xy.x - xy.xy.y;
// use a fixed luminosity and rescale the resulting rgb values using brightness
// randomly sampled conversions shown a minimum difference between original values
// and values after rgb -> xy -> rgb conversion for Y = 0.3
// (r-r')^2, (g-g')^2, (b-b')^2:
// 4.48214, 4.72039, 3.12141
// Max. Difference:
// 9, 9, 8
const float Y = 0.3f;
const float X = (Y / xy.xy.y) * xy.xy.x;
const float Z = (Y / xy.xy.y) * z;
const float r = X * 1.656492f - Y * 0.354851f - Z * 0.255038f;
const float g = -X * 0.707196f + Y * 1.655397f + Z * 0.036152f;
const float b = X * 0.051713f - Y * 0.121364f + Z * 1.011530f;
// Reverse gamma correction
const float gammaR = r <= 0.0031308f ? 12.92f * r : (1.0f + 0.055f) * pow(r, (1.0f / 2.4f)) - 0.055f;
const float gammaG = g <= 0.0031308f ? 12.92f * g : (1.0f + 0.055f) * pow(g, (1.0f / 2.4f)) - 0.055f;
const float gammaB = b <= 0.0031308f ? 12.92f * b : (1.0f + 0.055f) * pow(b, (1.0f / 2.4f)) - 0.055f;
// Scale color values so that the brightness matches
const float maxColor = std::max({gammaR, gammaG, gammaB});
if (maxColor < 1e-4)
{
// Low color values, out of gamut?
return RGB {0, 0, 0};
}
const float rScaled = gammaR / maxColor * xy.brightness * 255.f;
const float gScaled = gammaG / maxColor * xy.brightness * 255.f;
const float bScaled = gammaB / maxColor * xy.brightness * 255.f;
return RGB {static_cast<uint8_t>(std::round(std::max(0.f, rScaled))),
static_cast<uint8_t>(std::round(std::max(0.f, gScaled))),
static_cast<uint8_t>(std::round(std::max(0.f, bScaled)))};
}
RGB RGB::fromXY(const XYBrightness& xy, const ColorGamut& gamut)
{
if (gamut.contains(xy.xy))
{
return fromXY(xy);
}
else
{
return fromXY(XYBrightness {gamut.corrected(xy.xy), xy.brightness});
}
}
unsigned int kelvinToMired(unsigned int kelvin)
{
return int(std::round(1000000.f / kelvin));
}
unsigned int miredToKelvin(unsigned int mired)
{
return int(std::round(1000000.f / mired));
}
} // namespace hueplusplus