aest 0.1.0
Collection of C++ utility libraries
Loading...
Searching...
No Matches
Converter.cppm
Go to the documentation of this file.
1
7module;
8#include <algorithm>
9#include <array>
10#include <concepts>
11#include <cstdint>
12#include <numeric>
13#include <type_traits>
14export module aest.colors:converter;
15
16import aest.blas;
17import :color;
18import :cylindrical;
19import :gamut;
20import :transfer;
21
22namespace aest::colors
23{
34constexpr float hue_of(
35 const float r, const float g, const float b, const float max, const float delta)
36{
37 if (delta == 0)
38 return 0;
39 if (max == r)
40 return 60.0f * ((g - b) / delta);
41 if (max == g)
42 return 60.0f * ((b - r) / delta + 2);
43 return 60.0f * ((r - g) / delta + 4);
44}
45
56template <BoundedRGBSpace RGB>
57constexpr RGB rgb_from_hue(const float hue,
58 const float chroma,
59 const float offset,
60 const float alpha)
61{
62 // Distance from the middle of the current pair of 60 degree sectors;
63 // std::fmod and std::fabs are constexpr only since C++23
64 const float sector = hue / 60.0f;
65 const float distance = sector - 2 * static_cast<std::int32_t>(sector / 2) - 1;
66 const float second = chroma * (1 - (distance < 0 ? -distance : distance));
67
68 float r = 0, g = 0, b = 0;
69 if (hue < 60)
70 {
71 r = chroma;
72 g = second;
73 }
74 else if (hue < 120)
75 {
76 r = second;
77 g = chroma;
78 }
79 else if (hue < 180)
80 {
81 g = chroma;
82 b = second;
83 }
84 else if (hue < 240)
85 {
86 g = second;
87 b = chroma;
88 }
89 else if (hue < 300)
90 {
91 r = second;
92 b = chroma;
93 }
94 else
95 {
96 r = chroma;
97 b = second;
98 }
99
100 return RGB{r + offset, g + offset, b + offset, alpha};
101}
102
118export template <ColorType ColorOut, ColorType ColorIn>
120
131export template <ColorType Space>
133
138template <typename T>
139concept HasParentSpace = ColorType<T> && requires { typename ParentSpace<T>::type; };
140
146template <typename ColorOut, typename ColorIn>
147concept DirectConverter = requires(const ColorIn& color) {
148 { Converter<ColorOut, ColorIn>::convert(color) } -> std::same_as<ColorOut>;
149};
150
157template <typename Ancestor, typename Space>
158constexpr bool IsAncestorOrSelf = [] {
159 if constexpr (std::is_same_v<Ancestor, Space>)
160 return true;
161 else if constexpr (HasParentSpace<Space>)
162 return IsAncestorOrSelf<Ancestor, typename ParentSpace<Space>::type>;
163 else
164 return false;
165}();
166
173template <typename From, typename To>
175{};
176
182template <typename From, typename To>
184struct CommonAncestor<From, To>
185{
187 using type = From;
188};
189
195template <typename From, typename To>
197struct CommonAncestor<From, To> : CommonAncestor<typename ParentSpace<From>::type, To>
198{};
199
207template <ColorType Ancestor, ColorType Space>
208constexpr Ancestor ascend(const Space& color)
209{
210 if constexpr (std::is_same_v<Ancestor, Space>)
211 return color;
212 else
213 {
214 using Parent = typename ParentSpace<Space>::type;
216 }
217}
218
226template <ColorType Descendant, ColorType Space>
227constexpr Descendant descend(const Space& color)
228{
229 if constexpr (std::is_same_v<Descendant, Space>)
230 return color;
231 else
232 {
233 using Parent = typename ParentSpace<Descendant>::type;
235 }
236}
237
244export template <typename ColorOut, typename ColorIn>
248 });
249
263export template <ColorType ColorOut, ColorType ColorIn>
265constexpr ColorOut convert(const ColorIn& color)
266{
269 else
270 {
271 using Ancestor = typename CommonAncestor<ColorIn, ColorOut>::type;
272 return descend<ColorOut>(ascend<Ancestor>(color));
273 }
274}
275
281template <ColorType ColorOut, ColorType ColorIn>
282 requires(std::is_same_v<ColorOut, ColorIn>)
284{
290 static constexpr auto convert(const ColorIn& color) -> ColorOut { return color; }
291};
292
298template <RGBGamut Gamut>
299struct ParentSpace<BasicRGB<Gamut, tf::Linear>>
300{
302 using type = XYZ;
303};
304
310template <RGBGamut Gamut, TransferFunction Transfer>
311 requires(!std::same_as<Transfer, tf::Linear>)
317
322template <BoundedRGBSpace RGB>
324{
326 using type = RGB;
327};
328
333template <BoundedRGBSpace RGB>
335{
337 using type = RGB;
338};
339
344template <BoundedRGBSpace RGB>
345struct Converter<RGB, BasicHSL<RGB>>
346{
352 static constexpr RGB convert(const BasicHSL<RGB>& color)
353 {
354 const float l = color.lightness();
355 const float c = 2 * std::min(l, 1 - l) * color.saturation();
356 return rgb_from_hue<RGB>(color.hue(), c, l - c / 2, color.alpha());
357 }
358};
359
364template <BoundedRGBSpace RGB>
365struct Converter<BasicHSL<RGB>, RGB>
366{
372 static constexpr BasicHSL<RGB> convert(const RGB& color)
373 {
374 const float r = color.red();
375 const float g = color.green();
376 const float b = color.blue();
377
378 const float max = std::max({r, g, b});
379 const float min = std::min({r, g, b});
380 const float delta = max - min;
381
382 const float l = std::midpoint(max, min);
383 const float s = delta == 0 ? 0 : delta / (2 * std::min(l, 1 - l));
384
385 return BasicHSL<RGB>{hue_of(r, g, b, max, delta), s, l, color.alpha()};
386 }
387};
388
393template <BoundedRGBSpace RGB>
394struct Converter<BasicHSV<RGB>, RGB>
395{
401 static constexpr BasicHSV<RGB> convert(const RGB& color)
402 {
403 const float r = color.red();
404 const float g = color.green();
405 const float b = color.blue();
406
407 const float max = std::max({r, g, b});
408 const float min = std::min({r, g, b});
409 const float delta = max - min;
410
411 const float s = max == 0.0f ? 0.0f : delta / max;
412
413 return BasicHSV<RGB>{hue_of(r, g, b, max, delta), s, max, color.alpha()};
414 }
415};
416
421template <BoundedRGBSpace RGB>
422struct Converter<RGB, BasicHSV<RGB>>
423{
429 static constexpr RGB convert(const BasicHSV<RGB>& color)
430 {
431 const float v = color.brightness();
432 const float c = v * color.saturation();
433 return rgb_from_hue<RGB>(color.hue(), c, v - c, color.alpha());
434 }
435};
436
442template <BoundedRGBSpace RGB>
443struct Converter<BasicHSV<RGB>, BasicHSL<RGB>>
444{
450 static constexpr BasicHSV<RGB> convert(const BasicHSL<RGB>& color)
451 {
452 const float l = color.lightness();
453 const float v = l + color.saturation() * std::min(l, 1 - l);
454 const float s = v == 0 ? 0 : 2 * (1 - l / v);
455
456 return BasicHSV<RGB>{color.hue(), s, v, color.alpha()};
457 }
458};
459
465template <BoundedRGBSpace RGB>
466struct Converter<BasicHSL<RGB>, BasicHSV<RGB>>
467{
473 static constexpr BasicHSL<RGB> convert(const BasicHSV<RGB>& color)
474 {
475 const float v = color.brightness();
476 const float l = v * (1 - color.saturation() / 2);
477 const float s = l == 0 || l == 1 ? 0 : (v - l) / std::min(l, 1 - l);
478
479 return BasicHSL<RGB>{color.hue(), s, l, color.alpha()};
480 }
481};
482
488template <RGBGamut Gamut, TransferFunction Transfer>
489 requires(!std::same_as<Transfer, tf::Linear>)
491{
498 {
499 return {Transfer::decode(color.red()), Transfer::decode(color.green()),
500 Transfer::decode(color.blue()), color.alpha()};
501 }
502};
503
509template <RGBGamut Gamut, TransferFunction Transfer>
510 requires(!std::same_as<Transfer, tf::Linear>)
512{
519 {
520 return {Transfer::encode(color.red()), Transfer::encode(color.green()),
521 Transfer::encode(color.blue()), color.alpha()};
522 }
523};
524
529template <RGBGamut Gamut>
530struct Converter<XYZ, BasicRGB<Gamut, tf::Linear>>
531{
537 static constexpr XYZ convert(const BasicRGB<Gamut, tf::Linear>& color)
538 {
539 const Vector3 xyz =
540 blas::multiply(RGB_TO_XYZ<Gamut>, {color.red(), color.green(), color.blue()});
541 return XYZ{static_cast<float>(xyz[0]), static_cast<float>(xyz[1]),
542 static_cast<float>(xyz[2]), color.alpha()};
543 }
544};
545
550template <RGBGamut Gamut>
551struct Converter<BasicRGB<Gamut, tf::Linear>, XYZ>
552{
559 static constexpr BasicRGB<Gamut, tf::Linear> convert(const XYZ& color)
560 {
561 const Vector3 rgb = blas::multiply(XYZ_TO_RGB<Gamut>, {color.x(), color.y(), color.z()});
562 return {static_cast<float>(rgb[0]), static_cast<float>(rgb[1]), static_cast<float>(rgb[2]),
563 color.alpha()};
564 }
565};
566} // namespace aest::colors
constexpr Descendant descend(const Space &color)
Converts a color down the tree, child by child, to one of its descendants.
constexpr ColorOut convert(const ColorIn &color)
Convert a color from one color space to another.
constexpr RGB rgb_from_hue(const float hue, const float chroma, const float offset, const float alpha)
Builds an RGB color from a hue, a chroma and an offset, shared by the HSL and HSV conversions.
constexpr Ancestor ascend(const Space &color)
Converts a color up the tree, parent by parent, to one of its ancestors.
constexpr bool IsAncestorOrSelf
Whether Ancestor is Space or one of its ancestors in the conversion tree.
constexpr float hue_of(const float r, const float g, const float b, const float max, const float delta)
Hue of an sRGB color, shared by the HSL and HSV conversions.
blas::Vector< double, 3 > Vector3
A column of three values, such as XYZ or linear RGB channels.
Definition Gamut.cppm:113
constexpr Matrix3 XYZ_TO_RGB
Matrix from XYZ, D65, to linear RGB of Gamut.
Definition Gamut.cppm:201
constexpr Matrix3 RGB_TO_XYZ
Matrix from linear RGB of Gamut to XYZ, D65.
Definition Gamut.cppm:194
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
A color as hue, saturation and lightness, over the encoded RGB space RGB.
constexpr float lightness() const noexcept
Get the lightness value in the 0-1 range.
constexpr float saturation() const noexcept
Get the saturation value in the 0-1 range.
constexpr float hue() const noexcept
Get the hue value in the 0-360 range.
A color as hue, saturation and value (brightness), over the encoded RGB space RGB.
constexpr float brightness() const noexcept
Get the brightness value in the 0-1 range.
constexpr float saturation() const noexcept
Get the saturation value in the 0-1 range.
constexpr float hue() const noexcept
Get the hue value in the 0-360 range.
A color in an RGB color space: the primaries and white point of Gamut, with channels encoded by Trans...
Definition Gamut.cppm:296
constexpr float green() const noexcept
Get the green channel value.
Definition Gamut.cppm:330
constexpr float red() const noexcept
Get the red channel value.
Definition Gamut.cppm:324
constexpr float blue() const noexcept
Get the blue channel value.
Definition Gamut.cppm:336
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
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 pair of color spaces with a Converter specialization between them.
A color space with a parent in the conversion tree.
Transfer function tags, the second parameter of BasicRGB.
constexpr float alpha() const noexcept
Get the alpha channel value in the 0-1 range.
Definition Color.cppm:175
The closest common ancestor of two color spaces, as type; no type when they are in different trees.
static constexpr BasicHSL< RGB > convert(const BasicHSV< RGB > &color)
Converts color to HSL, keeping the hue.
static constexpr BasicHSL< RGB > convert(const RGB &color)
Converts color to HSL.
static constexpr BasicHSV< RGB > convert(const BasicHSL< RGB > &color)
Converts color to HSV, keeping the hue.
static constexpr BasicHSV< RGB > convert(const RGB &color)
Converts color to HSV.
static BasicRGB< Gamut, Transfer > convert(const BasicRGB< Gamut, tf::Linear > &color)
Applies the transfer function to each color channel.
static BasicRGB< Gamut, tf::Linear > convert(const BasicRGB< Gamut, Transfer > &color)
Applies the inverse transfer function to each color channel.
static constexpr BasicRGB< Gamut, tf::Linear > convert(const XYZ &color)
Converts color to the linear RGB of Gamut.
static constexpr auto convert(const ColorIn &color) -> ColorOut
Returns the color unchanged.
static constexpr RGB convert(const BasicHSL< RGB > &color)
Converts color to RGB.
static constexpr RGB convert(const BasicHSV< RGB > &color)
Converts color to RGB.
static constexpr XYZ convert(const BasicRGB< Gamut, tf::Linear > &color)
Converts color to XYZ.
Converts colors from one color space to another.
BasicRGB< Gamut, tf::Linear > type
The parent space.
Names the color space a space converts through, joining every space in one tree.