aest 0.1.0
Collection of C++ utility libraries
Loading...
Searching...
No Matches
OkLab.cppm
Go to the documentation of this file.
1
8module;
9#include <array>
10#include <cmath>
11#include <string_view>
12export module aest.colors:oklab;
13
14import aest.blas;
15import :color;
16import :converter;
17import :gamut;
18
19namespace aest::colors
20{
25constexpr Matrix3 XYZ_TO_LMS = {{
26 {0.8190224379967030, 0.3619062600528904, -0.1288737815209879},
27 {0.0329836539323885, 0.9292868615863434, 0.0361446663506424},
28 {0.0481771893596242, 0.2642395317527308, 0.6335478284694309},
29}};
30
32constexpr Matrix3 LMS_TO_OKLAB = {{
33 {0.2104542683093140, 0.7936177747023054, -0.0040720430116193},
34 {1.9779985324311684, -2.4285922420485799, 0.4505937096174110},
35 {0.0259040424655478, 0.7827717124575296, -0.8086757549230774},
36}};
37
40
43
55export class OKLab final : public ColorBase<OKLab>
56{
57public:
59 static constexpr std::string_view TYPE = "OKLab";
60
62 constexpr OKLab()
63 : ColorBase(1.0f, 0.0f, 0.0f, 0.0f)
64 {}
65
74 constexpr OKLab(const float l, const float a, const float b, const float alpha = 1.0f)
75 : ColorBase(alpha, l, a, b)
76 {}
77
80
84 [[nodiscard]] constexpr float lightness() const noexcept { return _ch1; }
85
90 [[nodiscard]] constexpr float a() const noexcept { return _ch2; }
91
96 [[nodiscard]] constexpr float b() const noexcept { return _ch3; }
98};
99
110export class OKLCH final : public ColorBase<OKLCH>
111{
112public:
114 static constexpr std::string_view TYPE = "OKLCH";
115
117 constexpr OKLCH()
118 : ColorBase(1.0f, 0.0f, 0.0f, 0.0f)
119 {}
120
129 constexpr OKLCH(const float l, const float c, const float h, const float alpha = 1.0f)
131 {}
132
135
139 [[nodiscard]] constexpr float lightness() const noexcept { return _ch1; }
140
145 [[nodiscard]] constexpr float chroma() const noexcept { return _ch2; }
146
151 [[nodiscard]] constexpr float hue() const noexcept { return _ch3; }
153};
154
156template <>
158{
160 using type = XYZ;
161};
162
164template <>
166{
168 using type = OKLab;
169};
170
172template <>
174{
181 static OKLab convert(const XYZ& color)
182 {
183 const Vector3 lms = blas::multiply(XYZ_TO_LMS, {color.x(), color.y(), color.z()});
184 const Vector3 lab =
185 blas::multiply(LMS_TO_OKLAB, {std::cbrt(lms[0]), std::cbrt(lms[1]), std::cbrt(lms[2])});
186 return OKLab{static_cast<float>(lab[0]), static_cast<float>(lab[1]),
187 static_cast<float>(lab[2]), color.alpha()};
188 }
189};
190
192template <>
194{
200 static constexpr XYZ convert(const OKLab& color)
201 {
202 const Vector3 roots =
203 blas::multiply(OKLAB_TO_LMS, {color.lightness(), color.a(), color.b()});
204 const Vector3 xyz = blas::multiply(LMS_TO_XYZ, {roots[0] * roots[0] * roots[0],
205 roots[1] * roots[1] * roots[1],
206 roots[2] * roots[2] * roots[2]});
207 return XYZ{static_cast<float>(xyz[0]), static_cast<float>(xyz[1]),
208 static_cast<float>(xyz[2]), color.alpha()};
209 }
210};
211
213template <>
215{
222 static OKLCH convert(const OKLab& color)
223 {
224 const double a = color.a();
225 const double b = color.b();
226 return OKLCH{color.lightness(), static_cast<float>(std::hypot(a, b)),
227 static_cast<float>(std::atan2(b, a) * DEGREES_PER_RADIAN), color.alpha()};
228 }
229};
230
232template <>
234{
241 static OKLab convert(const OKLCH& color)
242 {
243 const double chroma = color.chroma();
244 const double angle = color.hue() / DEGREES_PER_RADIAN;
245 return OKLab{color.lightness(), static_cast<float>(chroma * std::cos(angle)),
246 static_cast<float>(chroma * std::sin(angle)), color.alpha()};
247 }
248};
249} // namespace aest::colors
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
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
constexpr Matrix3 OKLAB_TO_LMS
Matrix from OKLab back to the cube roots of LMS.
Definition OkLab.cppm:42
constexpr Matrix3 XYZ_TO_LMS
Matrix from XYZ, D65, to the LMS cone responses of OKLab, as recomputed by CSS Color 4 so that D65 wh...
Definition OkLab.cppm:25
constexpr Matrix3 LMS_TO_XYZ
Matrix from LMS back to XYZ, D65.
Definition OkLab.cppm:39
constexpr Matrix3 LMS_TO_OKLAB
Matrix from the cube roots of LMS to OKLab, from CSS Color 4.
Definition OkLab.cppm:32
A color in OKLCH: OKLab in polar form, as lightness, chroma and hue.
Definition OkLab.cppm:111
constexpr OKLCH()
Default constructor initializes the color to opaque black (0, 0, 0, 1).
Definition OkLab.cppm:117
constexpr float lightness() const noexcept
Get the perceived lightness, 1 being D65 white.
Definition OkLab.cppm:139
static constexpr std::string_view TYPE
Color space name, used by the std::formatter specialization.
Definition OkLab.cppm:114
constexpr OKLCH(const float l, const float c, const float h, const float alpha=1.0f)
Construct a color from OKLCH values and alpha.
Definition OkLab.cppm:129
constexpr float chroma() const noexcept
Get the chroma, the distance from gray.
Definition OkLab.cppm:145
constexpr float hue() const noexcept
Get the hue angle in the 0-360 range.
Definition OkLab.cppm:151
A color in OKLab, the perceptual space by Björn Ottosson.
Definition OkLab.cppm:56
static constexpr std::string_view TYPE
Color space name, used by the std::formatter specialization.
Definition OkLab.cppm:59
constexpr float a() const noexcept
Get the green-red axis, negative toward green.
Definition OkLab.cppm:90
constexpr OKLab()
Default constructor initializes the color to opaque black (0, 0, 0, 1).
Definition OkLab.cppm:62
constexpr OKLab(const float l, const float a, const float b, const float alpha=1.0f)
Construct a color from OKLab values and alpha.
Definition OkLab.cppm:74
constexpr float lightness() const noexcept
Get the perceived lightness, 1 being D65 white.
Definition OkLab.cppm:84
constexpr float b() const noexcept
Get the blue-yellow axis, negative toward blue.
Definition OkLab.cppm:96
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 OKLCH convert(const OKLab &color)
Converts color to OKLCH.
Definition OkLab.cppm:222
static OKLab convert(const OKLCH &color)
Converts color to OKLab.
Definition OkLab.cppm:241
static OKLab convert(const XYZ &color)
Converts color to OKLab: LMS, cube root, then the OKLab matrix.
Definition OkLab.cppm:181
static constexpr XYZ convert(const OKLab &color)
Converts color to XYZ: the inverse matrix, cube, then back from LMS.
Definition OkLab.cppm:200
Converts colors from one color space to another.
OKLab type
The parent space.
Definition OkLab.cppm:168
XYZ type
The parent space.
Definition OkLab.cppm:160
Names the color space a space converts through, joining every space in one tree.