sandbox
Loading...
Searching...
No Matches
color.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
26#ifndef LIBSBX_MATH_COLOR_HPP_
27#define LIBSBX_MATH_COLOR_HPP_
28
29#include <cmath>
30
31#include <yaml-cpp/yaml.h>
32
34
35namespace sbx::math {
36
48class color {
49
50public:
51
55 color() noexcept;
56
62 color(std::uint32_t rgba) noexcept;
63
64 color(std::uint8_t red, std::uint8_t green, std::uint8_t blue, std::uint8_t alpha = 255u) noexcept;
65
74 template<std::floating_point Type>
75 color(Type red, Type green, Type blue, Type alpha = Type{1}) noexcept;
76
82 static auto black() noexcept -> color;
83
89 static auto white() noexcept -> color;
90
96 static auto red() noexcept -> color;
97
103 static auto green() noexcept -> color;
104
110 static auto blue() noexcept -> color;
111
117 static auto magenta() noexcept -> color;
118
124 static auto yellow() noexcept -> color;
125
131 static auto cyan() noexcept -> color;
132
138 static auto orange() noexcept -> color;
139
145 auto r() const noexcept -> const std::float_t&;
146
152 auto r() noexcept -> std::float_t&;
153
159 auto g() const noexcept -> const std::float_t&;
160
166 auto g() noexcept -> std::float_t&;
167
173 auto b() const noexcept -> const std::float_t&;
174
180 auto b() noexcept -> std::float_t&;
181
187 auto a() const noexcept -> const std::float_t&;
188
194 auto a() noexcept -> std::float_t&;
195
196private:
197
198 std::float_t _red;
199 std::float_t _green;
200 std::float_t _blue;
201 std::float_t _alpha;
202
203}; // class color
204
213auto operator==(const color& lhs, const color& rhs) noexcept -> bool;
214
223auto operator*(color lhs, const std::float_t value) -> color;
224
225template<std::floating_point Type>
226color::color(Type red, Type green, Type blue, Type alpha) noexcept
227: _red{static_cast<std::float_t>(red)},
228 _green{static_cast<std::float_t>(green)},
229 _blue{static_cast<std::float_t>(blue)},
230 _alpha{static_cast<std::float_t>(alpha)} { }
231
232} // namespace sbx::math
233
237template<>
238struct YAML::convert<sbx::math::color> {
239
240 static auto encode(const sbx::math::color& color) -> Node;
241 static auto decode(const Node& node, sbx::math::color& color) -> bool;
242
243}; // struct YAML::convert
244
248auto operator<<(YAML::Emitter& out, const sbx::math::color& color) -> YAML::Emitter&;
249
253template<>
254struct std::hash<sbx::math::color> {
255
256 auto operator()(const sbx::math::color& color) const noexcept -> std::size_t;
257
258}; // struct std::hash
259
260#endif // LIBSBX_MATH_COLOR_HPP_
constexpr auto operator*(basic_degree< Type > lhs, const Other rhs) noexcept -> basic_degree< Type >
Multiplies a degree value by a scalar factor.
Definition: angle.ipp:105
Definition: tests.cpp:6
RGBA color value type.
Definition: color.hpp:48
auto g() const noexcept -> const std::float_t &
Returns the green component (const).
Definition: color.cpp:89
static auto black() noexcept -> color
Returns a black color.
Definition: color.cpp:45
static auto white() noexcept -> color
Returns a white color.
Definition: color.cpp:49
static auto red() noexcept -> color
Returns a red color.
Definition: color.cpp:53
auto a() const noexcept -> const std::float_t &
Returns the alpha component (const).
Definition: color.cpp:105
color() noexcept
Constructs a white color with full opacity.
Definition: color.cpp:27
static auto cyan() noexcept -> color
Returns a cyan color.
Definition: color.cpp:73
static auto orange() noexcept -> color
Returns an orange color.
Definition: color.cpp:77
static auto blue() noexcept -> color
Returns a blue color.
Definition: color.cpp:61
auto b() const noexcept -> const std::float_t &
Returns the blue component (const).
Definition: color.cpp:97
auto r() const noexcept -> const std::float_t &
Returns the red component (const).
Definition: color.cpp:81
static auto magenta() noexcept -> color
Returns a magenta color.
Definition: color.cpp:65
static auto yellow() noexcept -> color
Returns a yellow color.
Definition: color.cpp:69
static auto green() noexcept -> color
Returns a green color.
Definition: color.cpp:57
auto operator<<(YAML::Emitter &out, const sbx::math::color &color) -> YAML::Emitter &
YAML stream output for color.
Definition: color.cpp:149