aest 0.1.0
Collection of C++ utility libraries
Loading...
Searching...
No Matches
Color.cppm
Go to the documentation of this file.
1
8module;
9#include <algorithm>
10#include <array>
11#include <concepts>
12#include <cstddef>
13#include <cstdint>
14#include <initializer_list>
15#include <numbers>
16#include <string_view>
17export module aest.colors:color;
18
19namespace aest::colors
20{
22inline constexpr std::uint8_t BYTE_MAX = 255;
23
29constexpr float clamp(const float value)
30{
31 return std::clamp(value, 0.f, 1.f);
32}
33
39constexpr float non_negative(const float value)
40{
41 return std::max(value, 0.f);
42}
43
49constexpr float to_float(const std::uint8_t value)
50{
51 return static_cast<float>(value) / BYTE_MAX;
52}
53
59constexpr std::uint8_t to_byte(const float value)
60{
61 return static_cast<std::uint8_t>(value * BYTE_MAX + 0.5f);
62}
63
70constexpr std::uint8_t byte_at(const std::uint32_t word, const std::uint32_t index)
71{
72 return static_cast<std::uint8_t>(word >> index * 8);
73}
74
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)
87{
88 return std::uint32_t{byte3} << 24 | std::uint32_t{byte2} << 16 | std::uint32_t{byte1} << 8
89 | byte0;
90}
91
99template <std::size_t Size>
100constexpr std::array<char, Size> join_names(const std::initializer_list<std::string_view> parts)
101{
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;
106 return chars;
107}
108
110constexpr double DEGREES_PER_RADIAN = 180.0 / std::numbers::pi;
111
118constexpr float wrap_hue(const float hue)
119{
120 // Degrees in a full turn of the hue circle
121 constexpr double fullTurn = 360.;
122
123 // std::fmod is constexpr only since C++23. The quotient is truncated in double,
124 // where it cannot round up to the next whole turn, and the subtraction is exact
125 const auto turns = static_cast<std::int64_t>(hue / fullTurn);
126 auto wrapped = static_cast<float>(hue - static_cast<double>(turns) * fullTurn);
127
128 // The remainder keeps the sign of hue, and a tiny negative one + 360 rounds to 360
129 if (wrapped < 0)
130 wrapped += static_cast<float>(fullTurn);
131
132 return wrapped == fullTurn ? 0.f : wrapped;
133}
134
139export template <typename Derived>
141{
143 friend Derived;
144
145private:
146 float _a;
147 float _ch1;
148 float _ch2;
149 float _ch3;
150
158 explicit constexpr ColorBase(const float alpha,
159 const float channel1,
160 const float channel2,
161 const float channel3)
162 : _a(clamp(alpha))
163 , _ch1(channel1)
164 , _ch2(channel2)
165 , _ch3(channel3)
166 {}
167
168public:
171
175 [[nodiscard]] constexpr float alpha() const noexcept { return _a; }
176
183 [[nodiscard]] constexpr float ch1() const noexcept { return _ch1; }
184
191 [[nodiscard]] constexpr float ch2() const noexcept { return _ch2; }
192
199 [[nodiscard]] constexpr float ch3() const noexcept { return _ch3; }
200
204
209 [[nodiscard]] constexpr Derived opacity(const float alpha) const
210 {
211 return Derived{_ch1, _ch2, _ch3, alpha};
212 }
213
217
223 friend constexpr bool operator==(const Derived color1, const Derived color2)
224 {
225 return color1._ch1 == color2._ch1 && color1._ch2 == color2._ch2
226 && color1._ch3 == color2._ch3 && color1._a == color2._a;
227 }
228
230};
231
249export template <typename Derived>
251{
254
260 friend constexpr Derived operator+(const Derived& lhs, const Derived& rhs) noexcept
261 {
262 return Derived{lhs.ch1() + rhs.ch1(), lhs.ch2() + rhs.ch2(), lhs.ch3() + rhs.ch3(),
263 lhs.alpha() + rhs.alpha()};
264 }
265
272 friend constexpr Derived operator-(const Derived& lhs, const Derived& rhs) noexcept
273 {
274 return Derived{lhs.ch1() - rhs.ch1(), lhs.ch2() - rhs.ch2(), lhs.ch3() - rhs.ch3(),
275 lhs.alpha() - rhs.alpha()};
276 }
277
284 friend constexpr Derived operator*(const Derived& lhs, const Derived& rhs) noexcept
285 {
286 return Derived{lhs.ch1() * rhs.ch1(), lhs.ch2() * rhs.ch2(), lhs.ch3() * rhs.ch3(),
287 lhs.alpha() * rhs.alpha()};
288 }
289
295 constexpr Derived& operator+=(const Derived& other) noexcept { return self() = self() + other; }
296
302 constexpr Derived& operator-=(const Derived& other) noexcept { return self() = self() - other; }
303
309 constexpr Derived& operator*=(const Derived& other) noexcept { return self() = self() * other; }
311
314
321 friend constexpr Derived operator*(const Derived& color, const float scalar) noexcept
322 {
323 return Derived{color.ch1() * scalar, color.ch2() * scalar, color.ch3() * scalar,
324 color.alpha()};
325 }
326
334 friend constexpr Derived operator*(const float scalar, const Derived& color) noexcept
335 {
336 return color * scalar;
337 }
338
346 friend constexpr Derived operator/(const Derived& color, const float scalar) noexcept
347 {
348 return Derived{color.ch1() / scalar, color.ch2() / scalar, color.ch3() / scalar,
349 color.alpha()};
350 }
351
358 constexpr Derived& operator*=(const float scalar) noexcept { return self() = self() * scalar; }
359
366 constexpr Derived& operator/=(const float scalar) noexcept { return self() = self() / scalar; }
368
369private:
374 constexpr Derived& self() noexcept { return *static_cast<Derived*>(this); }
375};
376
389export class XYZ final : public ColorBase<XYZ>, public ColorArithmetic<XYZ>
390{
391public:
393 static constexpr std::string_view TYPE = "XYZ";
394
396 constexpr XYZ()
397 : ColorBase(1.0f, 0.0f, 0.0f, 0.0f)
398 {}
399
408 constexpr XYZ(const float x, const float y, const float z, const float a = 1.0f)
409 : ColorBase(a, x, y, z)
410 {}
411
414
418 [[nodiscard]] constexpr float x() const noexcept { return _ch1; }
419
424 [[nodiscard]] constexpr float y() const noexcept { return _ch2; }
425
430 [[nodiscard]] constexpr float z() const noexcept { return _ch3; }
432};
433
447export template <typename T>
448concept ColorType = requires(const T& color) {
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>;
455} // namespace aest::colors
constexpr float clamp(const float value)
Clamp a floating-point value to the 0-1 range.
Definition Color.cppm:29
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.
Definition Color.cppm:83
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.
Definition Color.cppm:100
constexpr std::uint8_t to_byte(const float value)
Convert a float in the 0-1 range to a 8-bit color channel value.
Definition Color.cppm:59
constexpr std::uint8_t byte_at(const std::uint32_t word, const std::uint32_t index)
Extract one byte of a 32-bit word.
Definition Color.cppm:70
constexpr float wrap_hue(const float hue)
Wrap a hue angle to the [0, 360) range.
Definition Color.cppm:118
constexpr std::uint8_t BYTE_MAX
Maximum value for an 8-bit color channel.
Definition Color.cppm:22
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 float to_float(const std::uint8_t value)
Convert an 8-bit color channel value to a float in the 0-1 range.
Definition Color.cppm:49
constexpr float x() const noexcept
Get the X tristimulus value.
Definition Color.cppm:418
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.
Definition Color.cppm:408
constexpr float z() const noexcept
Get the Z tristimulus value.
Definition Color.cppm:430
static constexpr std::string_view TYPE
Color space name, used by the std::formatter specialization.
Definition Color.cppm:393
constexpr XYZ()
Default constructor initializes the color to opaque black (0, 0, 0, 1).
Definition Color.cppm:396
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
Mixin adding channel-wise arithmetic operators to a color type.
Definition Color.cppm:251
friend constexpr Derived operator/(const Derived &color, const float scalar) noexcept
Divides the color channels of a color, keeping its alpha.
Definition Color.cppm:346
friend constexpr Derived operator*(const Derived &lhs, const Derived &rhs) noexcept
Multiplies two colors, channel by channel.
Definition Color.cppm:284
constexpr Derived & self() noexcept
Access this object as the derived color type.
Definition Color.cppm:374
friend constexpr Derived operator*(const Derived &color, const float scalar) noexcept
Scales the color channels of a color, keeping its alpha.
Definition Color.cppm:321
constexpr Derived & operator+=(const Derived &other) noexcept
Adds another color to this color in place.
Definition Color.cppm:295
friend constexpr Derived operator-(const Derived &lhs, const Derived &rhs) noexcept
Subtracts a color from another, channel by channel.
Definition Color.cppm:272
friend constexpr Derived operator*(const float scalar, const Derived &color) noexcept
Scales the color channels of a color, keeping its alpha.
Definition Color.cppm:334
constexpr Derived & operator-=(const Derived &other) noexcept
Subtracts another color from this color in place.
Definition Color.cppm:302
constexpr Derived & operator*=(const float scalar) noexcept
Scales the color channels of this color in place, keeping its alpha.
Definition Color.cppm:358
constexpr Derived & operator/=(const float scalar) noexcept
Divides the color channels of this color in place, keeping its alpha.
Definition Color.cppm:366
constexpr Derived & operator*=(const Derived &other) noexcept
Multiplies this color by another color in place.
Definition Color.cppm:309
friend constexpr Derived operator+(const Derived &lhs, const Derived &rhs) noexcept
Adds two colors together, channel by channel.
Definition Color.cppm:260
float _ch1
First channel value.
Definition Color.cppm:147
constexpr float ch2() const noexcept
Get the second channel value.
Definition Color.cppm:191
constexpr float ch1() const noexcept
Get the first channel value.
Definition Color.cppm:183
float _ch3
Third channel value.
Definition Color.cppm:149
friend constexpr bool operator==(const Derived color1, const Derived color2)
Compare two colors of same type by checking each channel for equality.
Definition Color.cppm:223
float _ch2
Second channel value.
Definition Color.cppm:148
constexpr float ch3() const noexcept
Get the third channel value.
Definition Color.cppm:199
constexpr ColorBase(const float alpha, const float channel1, const float channel2, const float channel3)
Construct a color from raw channel values.
Definition Color.cppm:158
constexpr Derived opacity(const float alpha) const
Returns a new color with the same channels and the specified alpha value.
Definition Color.cppm:209
constexpr float alpha() const noexcept
Get the alpha channel value in the 0-1 range.
Definition Color.cppm:175
float _a
Alpha channel value in the 0-1 range.
Definition Color.cppm:146
friend Derived
Lets the derived color reach the private constructor and channels.
Definition Color.cppm:143