aest 0.1.0
Collection of C++ utility libraries
Loading...
Searching...
No Matches
CieLab.cppm
Go to the documentation of this file.
1
8module;
9#include <cmath>
10#include <string_view>
11export module aest.colors:cie_lab;
12
13import aest.blas;
14import :color;
15import :converter;
16import :gamut;
17
18namespace aest::colors
19{
22
25
28
30constexpr double LAB_EPSILON = 216.0 / 24389.0;
31
33constexpr double LAB_KAPPA = 24389.0 / 27.0;
34
42double lab_compress(const double ratio)
43{
44 return ratio > LAB_EPSILON ? std::cbrt(ratio) : (LAB_KAPPA * ratio + 16) / 116;
45}
46
53constexpr double lab_expand(const double value)
54{
55 const double cube = value * value * value;
56 return cube > LAB_EPSILON ? cube : (116 * value - 16) / LAB_KAPPA;
57}
58
71export class CIELab final : public ColorBase<CIELab>
72{
73public:
75 static constexpr std::string_view TYPE = "CIELab";
76
78 constexpr CIELab()
79 : ColorBase(1.0f, 0.0f, 0.0f, 0.0f)
80 {}
81
90 constexpr CIELab(const float l, const float a, const float b, const float alpha = 1.0f)
91 : ColorBase(alpha, l, a, b)
92 {}
93
96
100 [[nodiscard]] constexpr float lightness() const noexcept { return _ch1; }
101
106 [[nodiscard]] constexpr float a() const noexcept { return _ch2; }
107
112 [[nodiscard]] constexpr float b() const noexcept { return _ch3; }
114};
115
124export class CIELCH final : public ColorBase<CIELCH>
125{
126public:
128 static constexpr std::string_view TYPE = "CIELCH";
129
131 constexpr CIELCH()
132 : ColorBase(1.0f, 0.0f, 0.0f, 0.0f)
133 {}
134
143 constexpr CIELCH(const float l, const float c, const float h, const float alpha = 1.0f)
145 {}
146
149
153 [[nodiscard]] constexpr float lightness() const noexcept { return _ch1; }
154
159 [[nodiscard]] constexpr float chroma() const noexcept { return _ch2; }
160
165 [[nodiscard]] constexpr float hue() const noexcept { return _ch3; }
167};
168
170template <>
172{
174 using type = XYZ;
175};
176
178template <>
180{
182 using type = CIELab;
183};
184
186template <>
188{
196 static CIELab convert(const XYZ& color)
197 {
198 const Vector3 xyz = blas::multiply(D65_TO_D50, {color.x(), color.y(), color.z()});
199 const double fx = lab_compress(xyz[0] / D50_WHITE[0]);
200 const double fy = lab_compress(xyz[1] / D50_WHITE[1]);
201 const double fz = lab_compress(xyz[2] / D50_WHITE[2]);
202 return CIELab{static_cast<float>(116 * fy - 16), static_cast<float>(500 * (fx - fy)),
203 static_cast<float>(200 * (fy - fz)), color.alpha()};
204 }
205};
206
208template <>
210{
217 static constexpr XYZ convert(const CIELab& color)
218 {
219 const double lightness = color.lightness();
220 const double fy = (lightness + 16) / 116;
221 const double fx = fy + color.a() / 500.0;
222 const double fz = fy - color.b() / 200.0;
223
224 // Y is expanded from the lightness, so the line and the cube meet at κε
225 const double y = lightness > LAB_KAPPA * LAB_EPSILON ? fy * fy * fy : lightness / LAB_KAPPA;
226 const Vector3 xyz =
228 lab_expand(fz) * D50_WHITE[2]});
229 return XYZ{static_cast<float>(xyz[0]), static_cast<float>(xyz[1]),
230 static_cast<float>(xyz[2]), color.alpha()};
231 }
232};
233
235template <>
237{
244 static CIELCH convert(const CIELab& color)
245 {
246 const double a = color.a();
247 const double b = color.b();
248 return CIELCH{color.lightness(), static_cast<float>(std::hypot(a, b)),
249 static_cast<float>(std::atan2(b, a) * DEGREES_PER_RADIAN), color.alpha()};
250 }
251};
252
254template <>
256{
263 static CIELab convert(const CIELCH& color)
264 {
265 const double chroma = color.chroma();
266 const double angle = color.hue() / DEGREES_PER_RADIAN;
267 return CIELab{color.lightness(), static_cast<float>(chroma * std::cos(angle)),
268 static_cast<float>(chroma * std::sin(angle)), color.alpha()};
269 }
270};
271} // namespace aest::colors
constexpr double LAB_KAPPA
CIE κ, 24389 / 27: the slope of the line near black.
Definition CieLab.cppm:33
double lab_compress(const double ratio)
The CIELab compression of a ratio to the white: a cube root, with a line near black.
Definition CieLab.cppm:42
constexpr Matrix3 D50_TO_D65
Matrix adapting XYZ, D50, to XYZ, D65, with the Bradford transform.
Definition CieLab.cppm:21
constexpr Matrix3 D65_TO_D50
Matrix adapting XYZ, D65, to XYZ, D50, with the Bradford transform.
Definition CieLab.cppm:24
constexpr Vector3 D50_WHITE
XYZ of the D50 white at luminance Y = 1, the reference white of CIELab.
Definition CieLab.cppm:27
constexpr double LAB_EPSILON
CIE ε, 216 / 24389: below it, CIELab follows a line instead of the cube root.
Definition CieLab.cppm:30
constexpr double lab_expand(const double value)
Inverse of lab_compress() for the X and Z ratios.
Definition CieLab.cppm:53
constexpr float wrap_hue(const float hue)
Wrap a hue angle to the [0, 360) range.
Definition Color.cppm:118
constexpr float non_negative(const float value)
Clamp a floating-point value to be non-negative, leaving it unbounded above.
Definition Color.cppm:39
constexpr double DEGREES_PER_RADIAN
Degrees in a radian.
Definition Color.cppm:110
constexpr Chromaticity D50
Chromaticity of the D50 white point, as CSS Color 4 and the ICC profile connection space define it: t...
Definition Gamut.cppm:33
constexpr Matrix3 bradford_to_d65(const Chromaticity &white)
Derives the Bradford matrix that adapts XYZ colors from a white point to D65.
Definition Gamut.cppm:166
constexpr Vector3 xyz_of(const Chromaticity &chromaticity)
The XYZ of a chromaticity at luminance Y = 1.
Definition Gamut.cppm:130
blas::Vector< double, 3 > Vector3
A column of three values, such as XYZ or linear RGB channels.
Definition Gamut.cppm:113
blas::Matrix< double, 3, 3 > Matrix3
A 3x3 matrix, stored as three rows.
Definition Gamut.cppm:116
constexpr Vector< T, Rows > multiply(const Matrix< T, Rows, Columns > &matrix, const Vector< T, Columns > &vector)
Multiplies a matrix by a column vector.
Definition Matrix.cppm:69
constexpr Matrix< T, 3, 3 > inverse(const Matrix< T, 3, 3 > &matrix)
Inverts a 3x3 matrix through its adjugate.
Definition Matrix.cppm:158
A color in CIELCH: CIELab in polar form, as lightness, chroma and hue.
Definition CieLab.cppm:125
static constexpr std::string_view TYPE
Color space name, used by the std::formatter specialization.
Definition CieLab.cppm:128
constexpr float lightness() const noexcept
Get the lightness, 100 being D50 white.
Definition CieLab.cppm:153
constexpr float chroma() const noexcept
Get the chroma, the distance from gray.
Definition CieLab.cppm:159
constexpr float hue() const noexcept
Get the hue angle in the 0-360 range.
Definition CieLab.cppm:165
constexpr CIELCH(const float l, const float c, const float h, const float alpha=1.0f)
Construct a color from CIELCH values and alpha.
Definition CieLab.cppm:143
constexpr CIELCH()
Default constructor initializes the color to opaque black (0, 0, 0, 1).
Definition CieLab.cppm:131
A color in CIELab, the CIE 1976 L*a*b* space, relative to D50.
Definition CieLab.cppm:72
static constexpr std::string_view TYPE
Color space name, used by the std::formatter specialization.
Definition CieLab.cppm:75
constexpr CIELab()
Default constructor initializes the color to opaque black (0, 0, 0, 1).
Definition CieLab.cppm:78
constexpr CIELab(const float l, const float a, const float b, const float alpha=1.0f)
Construct a color from CIELab values and alpha.
Definition CieLab.cppm:90
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 the CIE 1931 XYZ color space, relative to the D65 white point.
Definition Color.cppm:390
constexpr float x() const noexcept
Get the X tristimulus value.
Definition Color.cppm:418
constexpr float z() const noexcept
Get the Z tristimulus value.
Definition Color.cppm:430
constexpr float y() const noexcept
Get the Y tristimulus value, the relative luminance.
Definition Color.cppm:424
constexpr ColorBase(const float alpha, const float channel1, const float channel2, const float channel3)
Definition Color.cppm:158
constexpr float alpha() const noexcept
Definition Color.cppm:175
static CIELCH convert(const CIELab &color)
Converts color to CIELCH.
Definition CieLab.cppm:244
static CIELab convert(const CIELCH &color)
Converts color to CIELab.
Definition CieLab.cppm:263
static CIELab convert(const XYZ &color)
Converts color to CIELab: Bradford to D50, then the CIELab compression of each ratio to the white.
Definition CieLab.cppm:196
static constexpr XYZ convert(const CIELab &color)
Converts color to XYZ: the inverse compression, then Bradford back to D65.
Definition CieLab.cppm:217
Converts colors from one color space to another.
CIELab type
The parent space.
Definition CieLab.cppm:182
Names the color space a space converts through, joining every space in one tree.