14#include <initializer_list>
29constexpr float clamp(
const float value)
31 return std::clamp(value, 0.f, 1.f);
41 return std::max(value, 0.f);
49constexpr float to_float(
const std::uint8_t value)
51 return static_cast<float>(value) /
BYTE_MAX;
59constexpr std::uint8_t
to_byte(
const float value)
61 return static_cast<std::uint8_t
>(value *
BYTE_MAX + 0.5f);
70constexpr std::uint8_t
byte_at(
const std::uint32_t word,
const std::uint32_t index)
72 return static_cast<std::uint8_t
>(word >> index * 8);
83constexpr std::uint32_t
pack(
const std::uint8_t byte3,
84 const std::uint8_t byte2,
85 const std::uint8_t byte1,
86 const std::uint8_t byte0)
88 return std::uint32_t{byte3} << 24 | std::uint32_t{byte2} << 16 | std::uint32_t{byte1} << 8
99template <std::
size_t Size>
100constexpr std::array<char, Size>
join_names(
const std::initializer_list<std::string_view> parts)
102 std::array<char, Size> chars{};
103 auto out = chars.begin();
104 for (
const std::string_view part : parts)
105 out = std::ranges::copy(part, out).out;
121 constexpr double fullTurn = 360.;
125 const auto turns =
static_cast<std::int64_t
>(hue / fullTurn);
126 auto wrapped =
static_cast<float>(hue -
static_cast<double>(turns) * fullTurn);
130 wrapped +=
static_cast<float>(fullTurn);
132 return wrapped == fullTurn ? 0.f : wrapped;
139export template <
typename Derived>
159 const float channel1,
160 const float channel2,
161 const float channel3)
175 [[nodiscard]]
constexpr float alpha() const noexcept {
return _a; }
183 [[nodiscard]]
constexpr float ch1() const noexcept {
return _ch1; }
191 [[nodiscard]]
constexpr float ch2() const noexcept {
return _ch2; }
199 [[nodiscard]]
constexpr float ch3() const noexcept {
return _ch3; }
225 return color1._ch1 == color2._ch1 && color1._ch2 == color2._ch2
226 && color1._ch3 == color2._ch3 && color1._a == color2._a;
249export template <
typename Derived>
260 friend constexpr Derived
operator+(
const Derived& lhs,
const Derived& rhs)
noexcept
262 return Derived{lhs.ch1() + rhs.ch1(), lhs.ch2() + rhs.ch2(), lhs.ch3() + rhs.ch3(),
263 lhs.alpha() + rhs.alpha()};
272 friend constexpr Derived
operator-(
const Derived& lhs,
const Derived& rhs)
noexcept
274 return Derived{lhs.ch1() - rhs.ch1(), lhs.ch2() - rhs.ch2(), lhs.ch3() - rhs.ch3(),
275 lhs.alpha() - rhs.alpha()};
284 friend constexpr Derived
operator*(
const Derived& lhs,
const Derived& rhs)
noexcept
286 return Derived{lhs.ch1() * rhs.ch1(), lhs.ch2() * rhs.ch2(), lhs.ch3() * rhs.ch3(),
287 lhs.alpha() * rhs.alpha()};
321 friend constexpr Derived
operator*(
const Derived& color,
const float scalar)
noexcept
323 return Derived{color.ch1() * scalar, color.ch2() * scalar, color.ch3() * scalar,
334 friend constexpr Derived
operator*(
const float scalar,
const Derived& color)
noexcept
336 return color * scalar;
346 friend constexpr Derived
operator/(
const Derived& color,
const float scalar)
noexcept
348 return Derived{color.ch1() / scalar, color.ch2() / scalar, color.ch3() / scalar,
374 constexpr Derived&
self() noexcept {
return *
static_cast<Derived*
>(
this); }
393 static constexpr std::string_view
TYPE =
"XYZ";
408 constexpr XYZ(
const float x,
const float y,
const float z,
const float a = 1.0f)
418 [[nodiscard]]
constexpr float x() const noexcept {
return _ch1; }
424 [[nodiscard]]
constexpr float y() const noexcept {
return _ch2; }
430 [[nodiscard]]
constexpr float z() const noexcept {
return _ch3; }
447export template <
typename T>
449 { color.alpha() } -> std::same_as<float>;
450 { color.ch1() } -> std::same_as<float>;
451 { color.ch2() } -> std::same_as<float>;
452 { color.ch3() } -> std::same_as<float>;
453 { T::TYPE } -> std::convertible_to<std::string_view>;
454} && std::constructible_from<T, float, float, float, float>;
constexpr float clamp(const float value)
Clamp a floating-point value to the 0-1 range.
constexpr std::uint32_t pack(const std::uint8_t byte3, const std::uint8_t byte2, const std::uint8_t byte1, const std::uint8_t byte0)
Pack four bytes into a 32-bit word, most significant first.
constexpr std::array< char, Size > join_names(const std::initializer_list< std::string_view > parts)
Concatenate names into a character array, for a color space name built at compile time.
constexpr std::uint8_t to_byte(const float value)
Convert a float in the 0-1 range to a 8-bit color channel value.
constexpr std::uint8_t byte_at(const std::uint32_t word, const std::uint32_t index)
Extract one byte of a 32-bit word.
constexpr float wrap_hue(const float hue)
Wrap a hue angle to the [0, 360) range.
constexpr std::uint8_t BYTE_MAX
Maximum value for an 8-bit color channel.
constexpr float non_negative(const float value)
Clamp a floating-point value to be non-negative, leaving it unbounded above.
constexpr double DEGREES_PER_RADIAN
Degrees in a radian.
constexpr float to_float(const std::uint8_t value)
Convert an 8-bit color channel value to a float in the 0-1 range.
constexpr float x() const noexcept
Get the X tristimulus value.
constexpr XYZ(const float x, const float y, const float z, const float a=1.0f)
Construct a color from XYZ tristimulus values and alpha.
constexpr float z() const noexcept
Get the Z tristimulus value.
static constexpr std::string_view TYPE
Color space name, used by the std::formatter specialization.
constexpr XYZ()
Default constructor initializes the color to opaque black (0, 0, 0, 1).
constexpr float y() const noexcept
Get the Y tristimulus value, the relative luminance.
Concept to check if a type is a color.
Mixin adding channel-wise arithmetic operators to a color type.
friend constexpr Derived operator/(const Derived &color, const float scalar) noexcept
Divides the color channels of a color, keeping its alpha.
friend constexpr Derived operator*(const Derived &lhs, const Derived &rhs) noexcept
Multiplies two colors, channel by channel.
constexpr Derived & self() noexcept
Access this object as the derived color type.
friend constexpr Derived operator*(const Derived &color, const float scalar) noexcept
Scales the color channels of a color, keeping its alpha.
constexpr Derived & operator+=(const Derived &other) noexcept
Adds another color to this color in place.
friend constexpr Derived operator-(const Derived &lhs, const Derived &rhs) noexcept
Subtracts a color from another, channel by channel.
friend constexpr Derived operator*(const float scalar, const Derived &color) noexcept
Scales the color channels of a color, keeping its alpha.
constexpr Derived & operator-=(const Derived &other) noexcept
Subtracts another color from this color in place.
constexpr Derived & operator*=(const float scalar) noexcept
Scales the color channels of this color in place, keeping its alpha.
constexpr Derived & operator/=(const float scalar) noexcept
Divides the color channels of this color in place, keeping its alpha.
constexpr Derived & operator*=(const Derived &other) noexcept
Multiplies this color by another color in place.
friend constexpr Derived operator+(const Derived &lhs, const Derived &rhs) noexcept
Adds two colors together, channel by channel.
float _ch1
First channel value.
constexpr float ch2() const noexcept
Get the second channel value.
constexpr float ch1() const noexcept
Get the first channel value.
float _ch3
Third channel value.
friend constexpr bool operator==(const Derived color1, const Derived color2)
Compare two colors of same type by checking each channel for equality.
float _ch2
Second channel value.
constexpr float ch3() const noexcept
Get the third channel value.
constexpr ColorBase(const float alpha, const float channel1, const float channel2, const float channel3)
Construct a color from raw channel values.
constexpr Derived opacity(const float alpha) const
Returns a new color with the same channels and the specified alpha value.
constexpr float alpha() const noexcept
Get the alpha channel value in the 0-1 range.
float _a
Alpha channel value in the 0-1 range.
friend Derived
Lets the derived color reach the private constructor and channels.