galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
Colour.cpp
Go to the documentation of this file.
1
7
8#include <algorithm>
9
10#include "galaxy/math/Math.hpp"
11
12#include "Colour.hpp"
13
14namespace galaxy
15{
16 Colour::Colour() noexcept
17 : m_red {0}
18 , m_green {0}
19 , m_blue {0}
20 , m_alpha {OPAQUE}
21 {
22 }
23
24 Colour::Colour(const std::uint8_t r, const std::uint8_t g, const std::uint8_t b, const std::uint8_t a) noexcept
25 {
26 this->r(r);
27 this->g(g);
28 this->b(b);
29 this->a(a);
30 }
31
32 Colour::Colour(Colour&& c) noexcept
33 {
34 this->m_red = c.m_red;
35 this->m_green = c.m_green;
36 this->m_blue = c.m_blue;
37 this->m_alpha = c.m_alpha;
38 }
39
40 Colour::Colour(const Colour& c) noexcept
41 {
42 this->m_red = c.m_red;
43 this->m_green = c.m_green;
44 this->m_blue = c.m_blue;
45 this->m_alpha = c.m_alpha;
46 }
47
49 {
50 if (this != &c)
51 {
52 this->m_red = c.m_red;
53 this->m_green = c.m_green;
54 this->m_blue = c.m_blue;
55 this->m_alpha = c.m_alpha;
56 }
57
58 return *this;
59 }
60
61 Colour& Colour::operator=(const Colour& c) noexcept
62 {
63 if (this != &c)
64 {
65 this->m_red = c.m_red;
66 this->m_green = c.m_green;
67 this->m_blue = c.m_blue;
68 this->m_alpha = c.m_alpha;
69 }
70
71 return *this;
72 }
73
74 Colour::~Colour() noexcept
75 {
76 }
77
78 void Colour::r(const std::uint8_t r) noexcept
79 {
80 m_red = std::clamp(r, LOWER, UPPER);
81 }
82
83 void Colour::g(const std::uint8_t g) noexcept
84 {
85 m_green = std::clamp(g, LOWER, UPPER);
86 }
87
88 void Colour::b(const std::uint8_t b) noexcept
89 {
90 m_blue = std::clamp(b, LOWER, UPPER);
91 }
92
93 void Colour::a(const std::uint8_t a) noexcept
94 {
95 m_alpha = std::clamp(a, LOWER, UPPER);
96 }
97
98 std::uint8_t Colour::r() const noexcept
99 {
100 return m_red;
101 }
102
103 std::uint8_t Colour::g() const noexcept
104 {
105 return m_green;
106 }
107
108 std::uint8_t Colour::b() const noexcept
109 {
110 return m_blue;
111 }
112
113 std::uint8_t Colour::a() const noexcept
114 {
115 return m_alpha;
116 }
117
118 void Colour::set_from_norm(const glm::vec4& rgba) noexcept
119 {
120 m_red = std::clamp(static_cast<std::uint8_t>(rgba.x * OFFSET), LOWER, UPPER);
121 m_green = std::clamp(static_cast<std::uint8_t>(rgba.y * OFFSET), LOWER, UPPER);
122 m_blue = std::clamp(static_cast<std::uint8_t>(rgba.z * OFFSET), LOWER, UPPER);
123 m_alpha = std::clamp(static_cast<std::uint8_t>(rgba.w * OFFSET), LOWER, UPPER);
124 }
125
126 glm::vec4 Colour::normalize() noexcept
127 {
128 auto vec4 = glm::vec4();
129
130 vec4.x = std::clamp(math::normalize(m_red, OFFSET), 0.0f, 1.0f);
131 vec4.y = std::clamp(math::normalize(m_green, OFFSET), 0.0f, 1.0f);
132 vec4.z = std::clamp(math::normalize(m_blue, OFFSET), 0.0f, 1.0f);
133 vec4.w = std::clamp(math::normalize(m_alpha, OFFSET), 0.0f, 1.0f);
134
135 return vec4;
136 }
137
138 std::array<std::uint8_t, 4> Colour::array() noexcept
139 {
140 return {m_red, m_green, m_blue, m_alpha};
141 }
142} // namespace galaxy
Represents an RGBA colour.
Definition Colour.hpp:24
Colour & operator=(Colour &&) noexcept
Move assignment operator.
Definition Colour.cpp:48
std::array< std::uint8_t, 4 > array() noexcept
Get as array.
Definition Colour.cpp:138
std::uint8_t m_blue
Blue channel.
Definition Colour.hpp:220
std::uint8_t m_alpha
Alpha channel.
Definition Colour.hpp:225
std::uint8_t a() const noexcept
Get alpha.
Definition Colour.cpp:113
std::uint8_t m_red
Red channel.
Definition Colour.hpp:210
glm::vec4 normalize() noexcept
Get normalized rgba vec4.
Definition Colour.cpp:126
void set_from_norm(const glm::vec4 &rgba) noexcept
Set RGBA from normalizaed values.
Definition Colour.cpp:118
Colour() noexcept
Constructor.
Definition Colour.cpp:16
std::uint8_t b() const noexcept
Get blue.
Definition Colour.cpp:108
std::uint8_t r() const noexcept
Get red.
Definition Colour.cpp:98
std::uint8_t g() const noexcept
Get green.
Definition Colour.cpp:103
~Colour() noexcept
Destructor.
Definition Colour.cpp:74
std::uint8_t m_green
Green channel.
Definition Colour.hpp:215
static const constexpr std::uint8_t OFFSET
Colour offsets for normalization.
Definition Colour.hpp:39
float constexpr normalize(const Arithmetic val, const Arithmetic max) noexcept
Calc normalized value from range.
Definition Math.hpp:29
Animated.cpp galaxy.
Definition Animated.cpp:16