32template <ColorType Space>
35 const float diff1 = color1.ch1() - color2.ch1();
36 const float diff2 = color1.ch2() - color2.ch2();
37 const float diff3 = color1.ch3() - color2.ch3();
39 return std::sqrt(diff1 * diff1 + diff2 * diff2 + diff3 * diff3);
51export template <ColorType Space>
60template <RGBGamut Gamut, TransferFunction Transfer>
122export template <
typename T>
145export template <Eucl
ideanSpace Space = OKLab, ColorType Color1, ColorType Color2>
149 return Euclidean<Space>::distance(convert<Space>(color1), convert<Space>(color2));
167export template <ColorType Color1, ColorType Color2>
168 requires ConvertibleTo<CIELab, Color1> && ConvertibleTo<CIELab, Color2>
169float de_76(
const Color1& color1,
const Color2& color2)
200export template <ColorType Color1, ColorType Color2>
201 requires ConvertibleTo<CIELab, Color1> && ConvertibleTo<CIELab, Color2>
203 const Color2& sample,
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;
214 const double chroma1 = std::hypot(
double{lab1.
a()},
double{lab1.
b()});
215 const double chroma2 = std::hypot(
double{lab2.
a()},
double{lab2.
b()});
217 const double chroma = chroma1 - chroma2;
218 const double diffA =
double{lab1.
a()} - lab2.
a();
219 const double diffB =
double{lab1.
b()} - lab2.
b();
222 const double hueSquared = std::fmax(diffA * diffA + diffB * diffB - chroma * chroma, 0.0);
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)));
239 if (a == 0 && b == 0)
242 return hue < 0 ? hue + 360 : hue;
253 constexpr double pivot = 6103515625.0;
255 const double power = std::pow(chroma, 7);
256 return std::sqrt(power / (power + pivot));
277export template <ColorType Color1, ColorType Color2>
278 requires ConvertibleTo<CIELab, Color1> && ConvertibleTo<CIELab, Color2>
279float de_2000(
const Color1& color1,
const Color2& 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();
287 const double meanChroma = (std::hypot(a1, b1) + std::hypot(a2, b2)) / 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);
297 const bool anyGray = chroma1 * chroma2 == 0;
298 double hueDiff = hue2 - hue1;
301 else if (hueDiff < -180)
306 const double lightnessDiff = l2 - l1;
307 const double chromaDiff = chroma2 - chroma1;
308 const double hueDistance =
312 const double hueSum = hue1 + hue2;
313 double meanHue = hueSum;
316 if (std::fabs(hue1 - hue2) <= 180)
317 meanHue = hueSum / 2;
319 meanHue = hueSum < 360 ? (hueSum + 360) / 2 : (hueSum - 360) / 2;
322 const auto cosine = [](
const double degrees) {
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);
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);
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));
constexpr double DEGREES_PER_RADIAN
Degrees in a radian.
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.
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...
A color in CIELab, the CIE 1976 L*a*b* space, relative to D50.
constexpr float lightness() const noexcept
Get the lightness, 100 being D50 white.
constexpr float b() const noexcept
Get the blue-yellow axis, negative toward blue.
constexpr float a() const noexcept
Get the green-red axis, negative toward green.
A color in OKLab, the perceptual space by Björn Ottosson.
Concept to check if a type is a color.
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.
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.
Measures the Euclidean distance between two colors of a color space.
Tag naming the color space an operation works in.