aest 0.1.0
Collection of C++ utility libraries
Loading...
Searching...
No Matches
Distance.cppm
Go to the documentation of this file.
1
8module;
9#include <cmath>
10#include <concepts>
11#include <cstdint>
12export module aest.colors:distance;
13
14import :cie_lab;
15import :color;
16import :converter;
17import :gamut;
18import :oklab;
19import :transfer;
20import :space;
21
22namespace aest::colors
23{
32template <ColorType Space>
33float channel_distance(const Space& color1, const Space& color2)
34{
35 const float diff1 = color1.ch1() - color2.ch1();
36 const float diff2 = color1.ch2() - color2.ch2();
37 const float diff3 = color1.ch3() - color2.ch3();
38
39 return std::sqrt(diff1 * diff1 + diff2 * diff2 + diff3 * diff3);
40}
41
51export template <ColorType Space>
52struct Euclidean;
53
60template <RGBGamut Gamut, TransferFunction Transfer>
61struct Euclidean<BasicRGB<Gamut, Transfer>>
62{
70 static float distance(const BasicRGB<Gamut, Transfer>& color1,
71 const BasicRGB<Gamut, Transfer>& color2)
72 {
73 return channel_distance(color1, color2);
74 }
75};
76
81template <>
83{
91 static float distance(const OKLab& color1, const OKLab& color2)
92 {
93 return channel_distance(color1, color2);
94 }
95};
96
101template <>
103{
111 static float distance(const CIELab& color1, const CIELab& color2)
112 {
113 return channel_distance(color1, color2);
114 }
115};
116
122export template <typename T>
123concept EuclideanSpace = ColorType<T> && requires(const T& color) {
124 { Euclidean<T>::distance(color, color) } -> std::same_as<float>;
125};
126
145export template <EuclideanSpace Space = OKLab, ColorType Color1, ColorType Color2>
147float euclidean(const Color1& color1, const Color2& color2, [[maybe_unused]] In<Space> space = {})
148{
149 return Euclidean<Space>::distance(convert<Space>(color1), convert<Space>(color2));
150}
151
167export template <ColorType Color1, ColorType Color2>
168 requires ConvertibleTo<CIELab, Color1> && ConvertibleTo<CIELab, Color2>
169float de_76(const Color1& color1, const Color2& color2)
170{
171 return euclidean<CIELab>(color1, color2);
172}
173
178export enum class CIE94Application : std::uint8_t
179{
182};
183
200export template <ColorType Color1, ColorType Color2>
201 requires ConvertibleTo<CIELab, Color1> && ConvertibleTo<CIELab, Color2>
202float de_94(const Color1& reference,
203 const Color2& sample,
205{
206 const bool textiles = application == CIE94Application::textiles;
207 const double kL = textiles ? 2.0 : 1.0;
208 const double k1 = textiles ? 0.048 : 0.045;
209 const double k2 = textiles ? 0.014 : 0.015;
210
211 const CIELab lab1 = convert<CIELab>(reference);
212 const CIELab lab2 = convert<CIELab>(sample);
213
214 const double chroma1 = std::hypot(double{lab1.a()}, double{lab1.b()});
215 const double chroma2 = std::hypot(double{lab2.a()}, double{lab2.b()});
216 const double lightness = double{lab1.lightness()} - lab2.lightness();
217 const double chroma = chroma1 - chroma2;
218 const double diffA = double{lab1.a()} - lab2.a();
219 const double diffB = double{lab1.b()} - lab2.b();
220
221 // ΔH² is what of Δa² + Δb² the chroma does not explain; rounding can push it below 0
222 const double hueSquared = std::fmax(diffA * diffA + diffB * diffB - chroma * chroma, 0.0);
223
224 const double lightnessTerm = lightness / kL;
225 const double chromaTerm = chroma / (1 + k1 * chroma1);
226 const double hueScale = 1 + k2 * chroma1;
227 return static_cast<float>(std::sqrt(lightnessTerm * lightnessTerm + chromaTerm * chromaTerm
228 + hueSquared / (hueScale * hueScale)));
229}
230
237double ciede2000_hue(const double a, const double b)
238{
239 if (a == 0 && b == 0)
240 return 0;
241 const double hue = std::atan2(b, a) * DEGREES_PER_RADIAN;
242 return hue < 0 ? hue + 360 : hue;
243}
244
250double ciede2000_chroma_weight(const double chroma)
251{
252 // 25^7: the weight is sqrt(1/2) at chroma 25
253 constexpr double pivot = 6103515625.0;
254
255 const double power = std::pow(chroma, 7);
256 return std::sqrt(power / (power + pivot));
257}
258
277export template <ColorType Color1, ColorType Color2>
278 requires ConvertibleTo<CIELab, Color1> && ConvertibleTo<CIELab, Color2>
279float de_2000(const Color1& color1, const Color2& color2)
280{
281 const CIELab lab1 = convert<CIELab>(color1);
282 const CIELab lab2 = convert<CIELab>(color2);
283 const double l1 = lab1.lightness(), a1 = lab1.a(), b1 = lab1.b();
284 const double l2 = lab2.lightness(), a2 = lab2.a(), b2 = lab2.b();
285
286 // Rescale a* so that the differences between near-grays are not understated
287 const double meanChroma = (std::hypot(a1, b1) + std::hypot(a2, b2)) / 2;
288 const double aScale = 1.5 - ciede2000_chroma_weight(meanChroma) / 2;
289 const double primeA1 = a1 * aScale;
290 const double primeA2 = a2 * aScale;
291 const double chroma1 = std::hypot(primeA1, b1);
292 const double chroma2 = std::hypot(primeA2, b2);
293 const double hue1 = ciede2000_hue(primeA1, b1);
294 const double hue2 = ciede2000_hue(primeA2, b2);
295
296 // The hue difference takes the shorter arc, and is 0 when either color is gray
297 const bool anyGray = chroma1 * chroma2 == 0;
298 double hueDiff = hue2 - hue1;
299 if (hueDiff > 180)
300 hueDiff -= 360;
301 else if (hueDiff < -180)
302 hueDiff += 360;
303 if (anyGray)
304 hueDiff = 0;
305
306 const double lightnessDiff = l2 - l1;
307 const double chromaDiff = chroma2 - chroma1;
308 const double hueDistance =
309 2 * std::sqrt(chroma1 * chroma2) * std::sin(hueDiff / 2 / DEGREES_PER_RADIAN);
310
311 // The mean hue is the middle of the shorter arc, or the sum when a color is gray
312 const double hueSum = hue1 + hue2;
313 double meanHue = hueSum;
314 if (!anyGray)
315 {
316 if (std::fabs(hue1 - hue2) <= 180)
317 meanHue = hueSum / 2;
318 else
319 meanHue = hueSum < 360 ? (hueSum + 360) / 2 : (hueSum - 360) / 2;
320 }
321
322 const auto cosine = [](const double degrees) {
323 return std::cos(degrees / DEGREES_PER_RADIAN);
324 };
325
326 const double meanLightness = (l1 + l2) / 2;
327 const double meanPrimeChroma = (chroma1 + chroma2) / 2;
328 const double hueWeight = 1 - 0.17 * cosine(meanHue - 30) + 0.24 * cosine(2 * meanHue)
329 + 0.32 * cosine(3 * meanHue + 6) - 0.20 * cosine(4 * meanHue - 63);
330
331 const double fromMidGray = (meanLightness - 50) * (meanLightness - 50);
332 const double lightnessScale = 1 + 0.015 * fromMidGray / std::sqrt(20 + fromMidGray);
333 const double chromaScale = 1 + 0.045 * meanPrimeChroma;
334 const double hueScale = 1 + 0.015 * meanPrimeChroma * hueWeight;
335 const double blueOffset = (meanHue - 275) / 25;
336 const double rotationDegrees = 30 * std::exp(-blueOffset * blueOffset);
337 const double rotation = -std::sin(2 * rotationDegrees / DEGREES_PER_RADIAN) * 2
338 * ciede2000_chroma_weight(meanPrimeChroma);
339
340 const double lightnessTerm = lightnessDiff / lightnessScale;
341 const double chromaTerm = chromaDiff / chromaScale;
342 const double hueTerm = hueDistance / hueScale;
343 return static_cast<float>(std::sqrt(lightnessTerm * lightnessTerm + chromaTerm * chromaTerm
344 + hueTerm * hueTerm + rotation * chromaTerm * hueTerm));
345}
346} // namespace aest::colors
constexpr double DEGREES_PER_RADIAN
Degrees in a radian.
Definition Color.cppm:110
constexpr ColorOut convert(const ColorIn &color)
Convert a color from one color space to another.
CIE94Application
The application whose weights de_94() uses, as CIE 116-1995 defines them.
@ textiles
kL = 2, K1 = 0.048, K2 = 0.014: lightness counts half as much.
@ graphic_arts
kL = 1, K1 = 0.045, K2 = 0.015: images, prints and screens.
float de_76(const Color1 &color1, const Color2 &color2)
The CIE 1976 color difference ΔE*ab: the Euclidean distance in CIELab.
float de_94(const Color1 &reference, const Color2 &sample, const CIE94Application application=CIE94Application::graphic_arts)
The CIE 1994 color difference ΔE*94.
float de_2000(const Color1 &color1, const Color2 &color2)
The CIEDE2000 color difference ΔE00, the current CIE formula.
float channel_distance(const Space &color1, const Space &color2)
Euclidean distance between the three channels of two colors, ignoring alpha.
Definition Distance.cppm:33
double ciede2000_chroma_weight(const double chroma)
The CIEDE2000 chroma weight sqrt(C^7 / (C^7 + 25^7)).
float euclidean(const Color1 &color1, const Color2 &color2, In< Space > space={})
Euclidean distance between two colors.
double ciede2000_hue(const double a, const double b)
Hue angle of a CIEDE2000 a', b' pair.
A color in an RGB color space: the primaries and white point of Gamut, with channels encoded by Trans...
Definition Gamut.cppm:296
A color in CIELab, the CIE 1976 L*a*b* space, relative to D50.
Definition CieLab.cppm:72
constexpr float lightness() const noexcept
Get the lightness, 100 being D50 white.
Definition CieLab.cppm:100
constexpr float b() const noexcept
Get the blue-yellow axis, negative toward blue.
Definition CieLab.cppm:112
constexpr float a() const noexcept
Get the green-red axis, negative toward green.
Definition CieLab.cppm:106
A color in OKLab, the perceptual space by Björn Ottosson.
Definition OkLab.cppm:56
Concept to check if a type is a color.
Definition Color.cppm:448
A color space that convert() can convert ColorIn to: directly through a Converter,...
A color space in which euclidean() can measure distances through Euclidean.
static float distance(const BasicRGB< Gamut, Transfer > &color1, const BasicRGB< Gamut, Transfer > &color2)
Distance between the channels of two colors, ignoring alpha.
Definition Distance.cppm:70
static float distance(const CIELab &color1, const CIELab &color2)
CIE 1976 difference between two colors, ignoring alpha.
static float distance(const OKLab &color1, const OKLab &color2)
Perceived difference between two colors, ignoring alpha.
Definition Distance.cppm:91
Measures the Euclidean distance between two colors of a color space.
Definition Distance.cppm:52
Tag naming the color space an operation works in.
Definition Space.cppm:30