opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
rgba.h
Go to the documentation of this file.
1#pragma once
2
8
9#include <algorithm>
10#include <concepts>
11#include <cstddef>
12#include <functional>
13#include <ostream>
14#include <tuple>
15#include <utility>
16
17namespace osc
18{
19 template<ColorComponent T>
20 struct alignas(std::max(alignof(T), alignof(uint32_t))) Rgba final {
21
22 using value_type = T;
27 using const_iterator = const value_type*;
28
29 static constexpr Rgba clear() { return {0.0f, 0.0f, 0.0f, 0.0f}; }
30
31 static constexpr Rgba white() { return Rgb<T>::white(); }
32 static constexpr Rgba lightest_grey() { return Rgb<T>::lightest_grey(); }
33 static constexpr Rgba lighter_grey() { return Rgb<T>::lighter_grey(); }
34 static constexpr Rgba light_grey() { return Rgb<T>::light_grey(); }
35 static constexpr Rgba dark_grey() { return Rgb<T>::dark_grey(); }
36 static constexpr Rgba darker_grey() { return Rgb<T>::darker_grey(); }
37 static constexpr Rgba darkest_grey() { return Rgb<T>::darkest_grey(); }
38 static constexpr Rgba black() { return Rgb<T>::black(); }
39 static constexpr Rgba muted_red() { return Rgb<T>::muted_red(); }
40 static constexpr Rgba red() { return Rgb<T>::red(); }
41 static constexpr Rgba muted_green() { return Rgb<T>::muted_green(); }
42 static constexpr Rgba green() { return Rgb<T>::green(); }
43 static constexpr Rgba dark_green() { return Rgb<T>::dark_green(); }
44 static constexpr Rgba muted_blue() { return Rgb<T>::muted_blue(); }
45 static constexpr Rgba blue() { return Rgb<T>::blue(); }
46 static constexpr Rgba cyan() { return Rgb<T>::cyan(); }
47 static constexpr Rgba magenta() { return Rgb<T>::magenta(); }
48 static constexpr Rgba muted_yellow() { return Rgb<T>::muted_yellow(); }
49 static constexpr Rgba yellow() { return Rgb<T>::yellow(); }
50 static constexpr Rgba orange() { return Rgb<T>::orange(); }
51 static constexpr Rgba purple() { return Rgb<T>::purple(); }
52
53 Rgba() = default;
54
55 explicit constexpr Rgba(value_type v) :
56 r{v}, g{v}, b{v}, a(1.0f)
57 {}
58
59 constexpr Rgba(value_type v, value_type alpha) :
60 r{v}, g{v}, b{v}, a{alpha}
61 {}
62
63 explicit constexpr Rgba(const Vector<value_type, 3>& v) :
64 r{v.x()}, g{v.y()}, b{v.z()}, a(1.0f)
65 {}
66
67 constexpr Rgba(const Vector<value_type, 3>& v, value_type alpha) :
68 r{v.x()}, g{v.y()}, b{v.z()}, a{alpha}
69 {}
70
71 explicit constexpr Rgba(const Vector<value_type, 4>& v) :
72 r{v.x()}, g{v.y()}, b{v.z()}, a{v.w()}
73 {}
74
75 template<ColorComponent U>
76 requires std::constructible_from<T, const U&>
77 explicit constexpr Rgba(const Vector<U, 4>& v) :
78 r{static_cast<T>(v.x())},
79 g{static_cast<T>(v.y())},
80 b{static_cast<T>(v.z())},
81 a{static_cast<T>(v.w())}
82 {}
83
85 r{r_}, g{g_}, b{b_}, a{a_}
86 {}
87
89 r{r_}, g{g_}, b{b_}, a(1.0f)
90 {}
91
92 template<ColorComponent U>
93 requires std::constructible_from<T, const U&>
94 explicit (not (std::convertible_to<U, T>))
95 constexpr Rgba(const Rgba<U>& v) :
96 r{static_cast<T>(v.r)},
97 g{static_cast<T>(v.g)},
98 b{static_cast<T>(v.b)},
99 a{static_cast<T>(v.a)}
100 {}
101
102 constexpr Rgba(const Rgb<T>& rgb, T alpha = T{1.0f}) :
103 r{rgb.r},
104 g{rgb.g},
105 b{rgb.b},
106 a{alpha}
107 {}
108
109 constexpr reference operator[](size_type pos) { return *(begin() + pos); }
110 constexpr const_reference operator[](size_type pos) const { return *(begin() + pos); }
111
112 constexpr size_t size() const { return 4; }
113
114 constexpr iterator begin() { return &r; }
115 constexpr const_iterator begin() const { return &r; }
116
117 constexpr iterator end() { return &r + size(); }
118 constexpr const_iterator end() const { return &r + size(); }
119
120 friend bool operator==(const Rgba&, const Rgba&) = default;
121
122 constexpr friend Rgba& operator*=(Rgba& lhs, const Rgba& rhs)
123 {
124 lhs.r *= rhs.r;
125 lhs.g *= rhs.g;
126 lhs.b *= rhs.b;
127 lhs.a *= rhs.a;
128
129 return lhs;
130 }
131
132 constexpr friend Rgba operator*(const Rgba& lhs, const Rgba& rhs)
133 {
134 Rgba copy{lhs};
135 copy *= rhs;
136 return copy;
137 }
138
139 constexpr friend Rgba operator*(value_type lhs, const Rgba& rhs)
140 {
141 return Rgba{
142 lhs * rhs.r,
143 lhs * rhs.g,
144 lhs * rhs.b,
145 lhs * rhs.a,
146 };
147 }
148
149 constexpr Rgba with_alpha(value_type a_) const
150 {
151 return Rgba{r, g, b, a_};
152 }
153
154 constexpr Rgba with_element(size_type pos, value_type value) const
155 {
156 Rgba copy{*this};
157 copy[pos] = value;
158 return copy;
159 }
160
161 constexpr Rgb<T> rgb() const
162 {
163 return {r, g, b};
164 }
165
170 };
171
172 template<ColorComponent T>
173 std::ostream& operator<<(std::ostream& o, const Rgba<T>& rgba)
174 {
175 return o << "Rgba{r = " << rgba.r << ", g = " << rgba.g << ", b = " << rgba.b << ", a = " << rgba.a << "}";
176 }
177
178 // returns a pointer to the first component of the `Rgba<T>`
179 template<ColorComponent T>
180 constexpr const T* value_ptr(const Rgba<T>& rgba)
181 {
182 return &rgba.r;
183 }
184
185 // returns a mutable pointer to the first component of the `Rgba<T>`
186 template<ColorComponent T>
187 constexpr T* value_ptr(Rgba<T>& rgba)
188 {
189 return &rgba.r;
190 }
191
192 // when handled as a tuple-like object, a `Rgba<T>` decomposes into its components (incl. alpha)
193
194 template<size_t I, ColorComponent T>
195 constexpr const T& get(const Rgba<T>& rgba) { return rgba[I]; }
196
197 template<size_t I, ColorComponent T>
198 constexpr T& get(Rgba<T>& rgba) { return rgba[I]; }
199
200 template<size_t I, ColorComponent T>
201 constexpr T&& get(Rgba<T>&& rgba) { return std::move(rgba[I]); }
202
203 template<size_t I, ColorComponent T>
204 constexpr const T&& get(const Rgba<T>&& rgba) { return std::move(rgba[I]); }
205
206 // returns a `Rgba<U>` containing `op(xv)` for each `xv` component in `x`
207 template<ColorComponent T, std::invocable<const T&> UnaryOperation>
208 constexpr auto map(const Rgba<T>& x, UnaryOperation op) -> Rgba<decltype(std::invoke(op, x[0]))>
209 {
210 Rgba<decltype(std::invoke(op, x[0]))> rv{};
211 for (size_t i = 0; i < 4; ++i) {
212 rv[i] = std::invoke(op, x[i]);
213 }
214 return rv;
215 }
216
217 // returns a `Rgba<U>` containing `op(xv, yv)` for each `(xv, yv)` component in `x` and `y`
218 template<ColorComponent T, std::invocable<const T&, const T&> BinaryOperation>
219 constexpr auto map(const Rgba<T>& x, const Rgba<T>& y, BinaryOperation op) -> Rgba<decltype(std::invoke(op, x[0], y[0]))>
220 {
221 Rgba<decltype(std::invoke(op, x[0], y[0]))> rv{};
222 for (size_t i = 0; i < 4; ++i) {
223 rv[i] = std::invoke(op, x[i], y[i]);
224 }
225 return rv;
226 }
227
228 // returns a `Rgba<T>` containing `lerp(xv, yv, t)` for each `(xv, yv)` component in `x` and `y`
229 template<ColorComponent T, typename TInterpolant>
230 constexpr auto lerp(const Rgba<T>& x, const Rgba<T>& y, TInterpolant t)
231 {
232 return map(x, y, [&t](const T& xv, const T& yv) { return lerp(xv, yv, t); });
233 }
234
235 // returns an `Rgba<T>` containing `saturate(xv)` for each `xv` component in `x`
236 template<ColorComponent T>
237 constexpr Rgba<T> saturate(const Rgba<T>& x)
238 {
239 return map(x, [](const T& xv) { return saturate(xv); });
240 }
241}
242
243// define compile-time size for `Rgba<T>`
244template<osc::ColorComponent T>
245struct std::tuple_size<osc::Rgba<T>> {
246 static inline constexpr size_t value = 4;
247};
248
249template<size_t I, osc::ColorComponent T>
250struct std::tuple_element<I, osc::Rgba<T>> {
251 using type = T;
252};
253
254// define hashing function for `Rgba<T>`
255template<osc::ColorComponent T>
256struct std::hash<osc::Rgba<T>> final {
257 size_t operator()(const osc::Rgba<T>& rgba) const noexcept
258 {
259 return osc::hash_of(rgba.r, rgba.g, rgba.b, rgba.a);
260 }
261};
Definition vector.h:63
Definition custom_decoration_generator.h:5
std::ostream & operator<<(std::ostream &, const Object &)
constexpr auto map(const Rgba< T > &x, UnaryOperation op) -> Rgba< decltype(std::invoke(op, x[0]))>
Definition rgba.h:208
constexpr const T & get(const Rgb< T > &v)
Definition rgb.h:88
constexpr auto lerp(const Rgba< T > &x, const Rgba< T > &y, TInterpolant t)
Definition rgba.h:230
constexpr Rgba< T > saturate(const Rgba< T > &x)
Definition rgba.h:237
size_t hash_of(const T &v) noexcept(noexcept(std::hash< T >{}(v)))
Definition hash_helpers.h:24
constexpr U to(T &&value)
Definition conversion.h:56
constexpr const T * value_ptr(const Rgba< T > &rgba)
Definition rgba.h:180
Definition rgb.h:13
static constexpr Rgb muted_yellow()
Definition rgb.h:39
static constexpr Rgb dark_grey()
Definition rgb.h:26
static constexpr Rgb muted_green()
Definition rgb.h:32
static constexpr Rgb muted_blue()
Definition rgb.h:35
static constexpr Rgb magenta()
Definition rgb.h:38
static constexpr Rgb yellow()
Definition rgb.h:40
static constexpr Rgb blue()
Definition rgb.h:36
static constexpr Rgb black()
Definition rgb.h:29
static constexpr Rgb muted_red()
Definition rgb.h:30
static constexpr Rgb red()
Definition rgb.h:31
static constexpr Rgb lightest_grey()
Definition rgb.h:23
static constexpr Rgb orange()
Definition rgb.h:41
static constexpr Rgb darkest_grey()
Definition rgb.h:28
static constexpr Rgb purple()
Definition rgb.h:42
static constexpr Rgb cyan()
Definition rgb.h:37
static constexpr Rgb lighter_grey()
Definition rgb.h:24
static constexpr Rgb darker_grey()
Definition rgb.h:27
static constexpr Rgb white()
Definition rgb.h:22
static constexpr Rgb green()
Definition rgb.h:33
static constexpr Rgb light_grey()
Definition rgb.h:25
static constexpr Rgb dark_green()
Definition rgb.h:34
Definition rgba.h:20
static constexpr Rgba blue()
Definition rgba.h:45
const value_type & const_reference
Definition rgba.h:24
static constexpr Rgba black()
Definition rgba.h:38
T value_type
Definition rgba.h:22
constexpr Rgba(value_type v, value_type alpha)
Definition rgba.h:59
static constexpr Rgba orange()
Definition rgba.h:50
static constexpr Rgba red()
Definition rgba.h:40
value_type r
Definition rgba.h:166
constexpr Rgba(const Rgb< T > &rgb, T alpha=T{1.0f})
Definition rgba.h:102
static constexpr Rgba cyan()
Definition rgba.h:46
static constexpr Rgba dark_green()
Definition rgba.h:43
static constexpr Rgba purple()
Definition rgba.h:51
constexpr Rgba(value_type r_, value_type g_, value_type b_)
Definition rgba.h:88
static constexpr Rgba muted_yellow()
Definition rgba.h:48
constexpr reference operator[](size_type pos)
Definition rgba.h:109
constexpr iterator end()
Definition rgba.h:117
constexpr const_iterator begin() const
Definition rgba.h:115
Rgba()=default
constexpr friend Rgba operator*(const Rgba &lhs, const Rgba &rhs)
Definition rgba.h:132
constexpr Rgb< T > rgb() const
Definition rgba.h:161
value_type a
Definition rgba.h:169
constexpr Rgba(value_type v)
Definition rgba.h:55
static constexpr Rgba darkest_grey()
Definition rgba.h:37
constexpr size_t size() const
Definition rgba.h:112
static constexpr Rgba muted_green()
Definition rgba.h:41
static constexpr Rgba darker_grey()
Definition rgba.h:36
size_t size_type
Definition rgba.h:25
static constexpr Rgba white()
Definition rgba.h:31
not(std::convertible_to< U, T >)) const expr Rgba(const Rgba< U > &v)
Definition rgba.h:94
static constexpr Rgba yellow()
Definition rgba.h:49
value_type & reference
Definition rgba.h:23
static constexpr Rgba green()
Definition rgba.h:42
value_type * iterator
Definition rgba.h:26
constexpr friend Rgba & operator*=(Rgba &lhs, const Rgba &rhs)
Definition rgba.h:122
constexpr iterator begin()
Definition rgba.h:114
constexpr friend Rgba operator*(value_type lhs, const Rgba &rhs)
Definition rgba.h:139
constexpr Rgba(const Vector< value_type, 4 > &v)
Definition rgba.h:71
static constexpr Rgba lighter_grey()
Definition rgba.h:33
constexpr const_reference operator[](size_type pos) const
Definition rgba.h:110
friend bool operator==(const Rgba &, const Rgba &)=default
constexpr Rgba(const Vector< value_type, 3 > &v)
Definition rgba.h:63
static constexpr Rgba muted_red()
Definition rgba.h:39
static constexpr Rgba light_grey()
Definition rgba.h:34
constexpr Rgba(const Vector< U, 4 > &v)
Definition rgba.h:77
static constexpr Rgba dark_grey()
Definition rgba.h:35
constexpr Rgba with_element(size_type pos, value_type value) const
Definition rgba.h:154
value_type b
Definition rgba.h:168
static constexpr Rgba clear()
Definition rgba.h:29
static constexpr Rgba magenta()
Definition rgba.h:47
const value_type * const_iterator
Definition rgba.h:27
static constexpr Rgba muted_blue()
Definition rgba.h:44
value_type g
Definition rgba.h:167
constexpr Rgba(const Vector< value_type, 3 > &v, value_type alpha)
Definition rgba.h:67
constexpr const_iterator end() const
Definition rgba.h:118
static constexpr Rgba lightest_grey()
Definition rgba.h:32
constexpr Rgba with_alpha(value_type a_) const
Definition rgba.h:149
constexpr Rgba(value_type r_, value_type g_, value_type b_, value_type a_)
Definition rgba.h:84
size_t operator()(const osc::Rgba< T > &rgba) const noexcept
Definition rgba.h:257