sandbox
Loading...
Searching...
No Matches
color.hpp
1#ifndef LIBSBX_MATH_COLOR_HPP_
2#define LIBSBX_MATH_COLOR_HPP_
3
4#include <cmath>
5
6#include <yaml-cpp/yaml.h>
7
9
10#include <libsbx/math/vector4.hpp>
11
12namespace sbx::math {
13
14class color {
15
16public:
17
18 static const color black;
19 static const color white;
20 static const color red;
21 static const color green;
22 static const color blue;
23
24 color(std::uint32_t rgba) noexcept;
25
26 color(std::float_t red, std::float_t green, std::float_t blue, std::float_t alpha) noexcept;
27
28 auto r() const noexcept -> const std::float_t&;
29
30 auto r() noexcept -> std::float_t&;
31
32 auto g() const noexcept -> const std::float_t&;
33
34 auto g() noexcept -> std::float_t&;
35
36 auto b() const noexcept -> const std::float_t&;
37
38 auto b() noexcept -> std::float_t&;
39
40 auto a() const noexcept -> const std::float_t&;
41
42 auto a() noexcept -> std::float_t&;
43
44private:
45
46 std::float_t _red;
47 std::float_t _green;
48 std::float_t _blue;
49 std::float_t _alpha;
50
51}; // class color
52
53auto operator==(const color& lhs, const color& rhs) noexcept -> bool;
54
55} // namespace sbx::math
56
57template<>
58struct YAML::convert<sbx::math::color> {
59 static auto encode(const sbx::math::color& color) -> Node {
60 auto node = Node{};
61
62 node["r"] = color.r();
63 node["g"] = color.g();
64 node["b"] = color.b();
65 node["a"] = color.a();
66
67 return node;
68 }
69
70 static auto decode(const Node& node, sbx::math::color& color) -> bool {
71 if (!node.IsMap() || node.size() != 4) {
72 return false;
73 }
74
75 color.r() = node["r"].as<std::float_t>();
76 color.g() = node["g"].as<std::float_t>();
77 color.b() = node["b"].as<std::float_t>();
78 color.a() = node["a"].as<std::float_t>();
79
80 return true;
81 }
82}; // struct YAML::convert
83
84template<>
85struct std::hash<sbx::math::color> {
86 auto operator()(const sbx::math::color& color) const noexcept -> std::size_t {
87 auto hash = std::size_t{0};
88 sbx::utility::hash_combine(hash, color.r(), color.g(), color.b(), color.a());
89 return hash;
90 }
91}; // struct std::hash
92
93#endif // LIBSBX_MATH_COLOR_HPP_
Definition: color.hpp:14