35 const float r,
const float g,
const float b,
const float max,
const float delta)
40 return 60.0f * ((g - b) / delta);
42 return 60.0f * ((b - r) / delta + 2);
43 return 60.0f * ((r - g) / delta + 4);
56template <BoundedRGBSpace RGB>
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));
68 float r = 0, g = 0, b = 0;
100 return RGB{r + offset, g + offset, b + offset, alpha};
118export template <ColorType ColorOut, ColorType ColorIn>
131export template <ColorType Space>
146template <
typename ColorOut,
typename ColorIn>
157template <
typename Ancestor,
typename Space>
159 if constexpr (std::is_same_v<Ancestor, Space>)
173template <
typename From,
typename To>
182template <
typename From,
typename To>
195template <
typename From,
typename To>
207template <ColorType Ancestor, ColorType Space>
208constexpr Ancestor
ascend(
const Space& color)
210 if constexpr (std::is_same_v<Ancestor, Space>)
226template <ColorType Descendant, ColorType Space>
227constexpr Descendant
descend(
const Space& color)
229 if constexpr (std::is_same_v<Descendant, Space>)
244export template <
typename ColorOut,
typename ColorIn>
263export template <ColorType ColorOut, ColorType ColorIn>
265constexpr ColorOut
convert(
const ColorIn& color)
281template <ColorType ColorOut, ColorType ColorIn>
282 requires(std::is_same_v<ColorOut, ColorIn>)
290 static constexpr auto convert(
const ColorIn& color) -> ColorOut {
return color; }
298template <RGBGamut Gamut>
310template <RGBGamut Gamut, TransferFunction Transfer>
311 requires(!std::same_as<Transfer, tf::Linear>)
322template <BoundedRGBSpace RGB>
333template <BoundedRGBSpace RGB>
344template <BoundedRGBSpace RGB>
355 const float c = 2 * std::min(l, 1 - l) * color.
saturation();
364template <BoundedRGBSpace RGB>
374 const float r = color.red();
375 const float g = color.green();
376 const float b = color.blue();
378 const float max = std::max({r, g, b});
379 const float min = std::min({r, g, b});
380 const float delta = max - min;
382 const float l = std::midpoint(max, min);
383 const float s = delta == 0 ? 0 : delta / (2 * std::min(l, 1 - l));
393template <BoundedRGBSpace RGB>
403 const float r = color.red();
404 const float g = color.green();
405 const float b = color.blue();
407 const float max = std::max({r, g, b});
408 const float min = std::min({r, g, b});
409 const float delta = max - min;
411 const float s = max == 0.0f ? 0.0f : delta / max;
421template <BoundedRGBSpace RGB>
442template <BoundedRGBSpace RGB>
453 const float v = l + color.
saturation() * std::min(l, 1 - l);
454 const float s = v == 0 ? 0 : 2 * (1 - l / v);
465template <BoundedRGBSpace RGB>
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);
488template <RGBGamut Gamut, TransferFunction Transfer>
489 requires(!std::same_as<Transfer, tf::Linear>)
499 return {Transfer::decode(color.
red()), Transfer::decode(color.
green()),
500 Transfer::decode(color.
blue()), color.
alpha()};
509template <RGBGamut Gamut, TransferFunction Transfer>
510 requires(!std::same_as<Transfer, tf::Linear>)
520 return {Transfer::encode(color.
red()), Transfer::encode(color.
green()),
521 Transfer::encode(color.
blue()), color.
alpha()};
529template <RGBGamut Gamut>
541 return XYZ{
static_cast<float>(xyz[0]),
static_cast<float>(xyz[1]),
542 static_cast<float>(xyz[2]), color.
alpha()};
550template <RGBGamut Gamut>
562 return {
static_cast<float>(rgb[0]),
static_cast<float>(rgb[1]),
static_cast<float>(rgb[2]),
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.
constexpr Matrix3 XYZ_TO_RGB
Matrix from XYZ, D65, to linear RGB of Gamut.
constexpr Matrix3 RGB_TO_XYZ
Matrix from linear RGB of Gamut to XYZ, D65.
constexpr Vector< T, Rows > multiply(const Matrix< T, Rows, Columns > &matrix, const Vector< T, Columns > &vector)
Multiplies a matrix by a column vector.
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...
constexpr float green() const noexcept
Get the green channel value.
constexpr float red() const noexcept
Get the red channel value.
constexpr float blue() const noexcept
Get the blue channel value.
A color in the CIE 1931 XYZ color space, relative to the D65 white point.
constexpr float x() const noexcept
Get the X tristimulus value.
constexpr float z() const noexcept
Get the Z tristimulus value.
constexpr float y() const noexcept
Get the Y tristimulus value, the relative luminance.
Concept to check if a type is a color.
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.
From type
The common ancestor.
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.
RGB type
The parent space.
RGB type
The parent space.
BasicRGB< Gamut, tf::Linear > type
The parent space.
XYZ type
The parent space.
Names the color space a space converts through, joining every space in one tree.