This repository was archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathColor.h
More file actions
404 lines (321 loc) · 15.5 KB
/
Color.h
File metadata and controls
404 lines (321 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#pragma once
#ifndef GUID_B9A272CC_A6CB_43C5_9096_25CCA86D59C0
#define GUID_B9A272CC_A6CB_43C5_9096_25CCA86D59C0
/*******************************************************************************
*******************************************************************************
* Copyright (c) 2009-2023 ectotropic (ectotropic@gmail.com, *
* https://github.com/ectotropic) *
* *
* This program is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation, either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************
*******************************************************************************/
//--------------------------------------
//
#include "Util/TypeTraits.h"
//--------------------------------------
//--------------------------------------
//
#include <cstdint>
#include <array>
#include <limits>
//--------------------------------------
namespace Color::detail {
//**************************************************************************
// ColorCommonT
//**************************************************************************
template <typename ComponentT, std::size_t CountT>
class ColorCommonT : public std::array<ComponentT, CountT> {
private:
static_assert((CountT > 0) && (CountT <= 4),
"ColorCommonT unsupported component count");
private:
using this_type = ColorCommonT<ComponentT, CountT>;
using base_type = std::array<ComponentT, CountT>;
public:
using component_type = base_type::value_type;
public:
inline static constexpr const auto Count { CountT };
inline static constexpr const auto ChannelMax{ std::numeric_limits<component_type>::max() };
inline static constexpr const auto ChannelMin{ std::numeric_limits<component_type>::min() };
protected: // construction
template <typename ...ArgPackT>
constexpr ColorCommonT(ArgPackT&& ...args) noexcept :
base_type{ std::forward<ArgPackT>(args)... } {}
public: // operators
constexpr this_type& operator+=(const this_type& other) noexcept {
for (size_type i = 0; i < size(); ++i) {
(*this)[i] += other[i];
}
return *this;
}
constexpr this_type& operator-=(const this_type& other) noexcept {
for (size_type i = 0; i < size(); ++i) {
(*this)[i] -= other[i];
}
return *this;
}
constexpr this_type& operator*=(const this_type& other) noexcept {
for (size_type i = 0; i < size(); ++i) {
(*this)[i] *= other[i];
}
return *this;
}
constexpr this_type& operator/=(const this_type& other) noexcept {
for (size_type i = 0; i < size(); ++i) {
(*this)[i] /= other[i];
}
return *this;
}
constexpr this_type& operator+=(value_type value) noexcept {
for (size_type i = 0; i < size(); ++i) {
(*this)[i] += value;
}
return *this;
}
constexpr this_type& operator-=(value_type value) noexcept {
for (size_type i = 0; i < size(); ++i) {
(*this)[i] -= value;
}
return *this;
}
constexpr this_type& operator*=(value_type value) noexcept {
for (size_type i = 0; i < size(); ++i) {
(*this)[i] *= value;
}
return *this;
}
constexpr this_type& operator/=(value_type value) noexcept {
for (size_type i = 0; i < size(); ++i) {
(*this)[i] /= value;
}
return *this;
}
constexpr this_type operator+(const this_type& other) const noexcept {
return this_type{ *this } += other;
}
constexpr this_type operator-(const this_type& other) const noexcept {
return this_type{ *this } -= other;
}
constexpr this_type operator*(const this_type& other) const noexcept {
return this_type{ *this } *= other;
}
constexpr this_type operator/(const this_type& other) const noexcept {
return this_type{ *this } /= other;
}
constexpr this_type operator+(value_type other) const noexcept {
return this_type{ *this } += other;
}
constexpr this_type operator-(value_type other) const noexcept {
return this_type{ *this } -= other;
}
constexpr this_type operator*(value_type other) const noexcept {
return this_type{ *this } *= other;
}
constexpr this_type operator/(value_type other) const noexcept {
return this_type{ *this } /= other;
}
}; // template <...> class ColorCommonT
} // namespace Color::detail
//==============================================================================
namespace Color {
//**************************************************************************
// ColorT
//**************************************************************************
template <typename ComponentT, std::size_t CountT>
class ColorT {};
//==========================================================================
template <typename ComponentT>
class ColorT<ComponentT, 1> final :
public ::Color::detail::ColorCommonT<ComponentT, 1> {
private:
using this_type = ColorT<ComponentT, 1>;
using base_type = ColorCommonT<ComponentT, 1>;
public:
using typename base_type::component_type;
public:
ColorT() = default;
constexpr ColorT(const this_type& other) noexcept :
base_type{ other } {}
constexpr ColorT( this_type&& other) noexcept :
base_type{ std::forward<this_type>(other) } {}
explicit constexpr ColorT(component_type _1) noexcept :
base_type{ _1 } {}
explicit constexpr ColorT(const component_type p[1]) noexcept :
base_type{ p[0] } {}
this_type& operator=(const this_type& ) = default;
this_type& operator=( this_type&&) = default;
public: // setters
constexpr void r(component_type val) noexcept { (*this)[0] = val; }
public: // getters
constexpr decltype(auto) r() noexcept { return (*this)[0]; }
constexpr decltype(auto) r() const noexcept { return (*this)[0]; }
public: // conversion
template <typename OtherComponentT>
explicit operator ColorT<OtherComponentT, 1>() const noexcept {
return color_cast<ColorT<OtherComponentT, 1>>(*this);
}
}; // template <...> class ColorT final
//==========================================================================
template <typename ComponentT>
class ColorT<ComponentT, 2> final :
public ::Color::detail::ColorCommonT<ComponentT, 2> {
private:
using this_type = ColorT<ComponentT, 2>;
using base_type = ColorCommonT<ComponentT, 2>;
public:
using typename base_type::component_type;
public:
ColorT() = default;
constexpr ColorT(const this_type& other) : base_type{ other } {}
constexpr ColorT( this_type&& other) : base_type{ std::forward<this_type>(other) } {}
constexpr ColorT(component_type _1, component_type _2) :
base_type{ _1, _2 } {}
explicit constexpr ColorT(const component_type p[2]) :
base_type{ p[0], p[1] } {}
this_type& operator=(const this_type& ) = default;
this_type& operator=( this_type&&) = default;
public: // setters
constexpr void r(component_type val) noexcept { (*this)[0] = val; }
constexpr void g(component_type val) noexcept { (*this)[1] = val; }
public: // getters
constexpr decltype(auto) r() noexcept { return (*this)[0]; }
constexpr decltype(auto) r() const noexcept { return (*this)[0]; }
constexpr decltype(auto) g() noexcept { return (*this)[1]; }
constexpr decltype(auto) g() const noexcept { return (*this)[1]; }
public: // conversion
template <typename OtherComponentT>
explicit operator ColorT<OtherComponentT, 2>() const noexcept {
return color_cast<ColorT<OtherComponentT, 2>>(*this);
}
}; // template <...> class ColorT final
//==========================================================================
template <typename ComponentT>
class ColorT<ComponentT, 3> final :
public ::Color::detail::ColorCommonT<ComponentT, 3> {
private:
using this_type = ColorT<ComponentT, 3>;
using base_type = ColorCommonT<ComponentT, 3>;
public:
using typename base_type::component_type;
public:
ColorT() = default;
constexpr ColorT(const this_type& other) noexcept : base_type{ other } {}
constexpr ColorT( this_type&& other) noexcept : base_type{ std::forward<this_type>(other) } {}
constexpr ColorT(component_type _1, component_type _2, component_type _3) noexcept :
base_type{ _1, _2, _3 } {}
explicit constexpr ColorT(const component_type p[3]) noexcept :
base_type{ p[0], p[1], p[2] } {}
this_type& operator=(const this_type& ) = default;
this_type& operator=( this_type&&) = default;
public: // setters
constexpr void r(component_type val) noexcept { (*this)[0] = val; }
constexpr void g(component_type val) noexcept { (*this)[1] = val; }
constexpr void b(component_type val) noexcept { (*this)[2] = val; }
public: // getters
constexpr decltype(auto) r() noexcept { return (*this)[0]; }
constexpr decltype(auto) r() const noexcept { return (*this)[0]; }
constexpr decltype(auto) g() noexcept { return (*this)[1]; }
constexpr decltype(auto) g() const noexcept { return (*this)[1]; }
constexpr decltype(auto) b() noexcept { return (*this)[2]; }
constexpr decltype(auto) b() const noexcept { return (*this)[2]; }
public: // conversion
template <typename OtherComponentT>
explicit operator ColorT<OtherComponentT, 3>() const noexcept {
return color_cast<ColorT<OtherComponentT, 3>>(*this);
}
}; // template <...> class ColorT final
//==========================================================================
template <typename ComponentT>
class ColorT<ComponentT, 4> final :
public ::Color::detail::ColorCommonT<ComponentT, 4> {
private:
using this_type = ColorT<ComponentT, 4>;
using base_type = ::Color::detail::ColorCommonT<ComponentT, 4>;
public:
using typename base_type::component_type;
public:
~ColorT() = default;
ColorT() = default;
constexpr ColorT(const this_type& other) noexcept : base_type{ other } {}
constexpr ColorT( this_type&& other) noexcept : base_type{ std::forward<this_type>(other) } {}
constexpr ColorT(component_type _1, component_type _2,
component_type _3, component_type _4) noexcept :
base_type{ _1, _2, _3, _4 } {}
explicit constexpr ColorT(const component_type p[4]) noexcept :
base_type{ p[0], p[1], p[2], p[3] } {}
this_type& operator=(const this_type& ) = default;
this_type& operator=( this_type&&) = default;
public: // setters
constexpr void r(component_type val) noexcept { (*this)[0] = val; }
constexpr void g(component_type val) noexcept { (*this)[1] = val; }
constexpr void b(component_type val) noexcept { (*this)[2] = val; }
constexpr void a(component_type val) noexcept { (*this)[3] = val; }
public: // getters
constexpr decltype(auto) r() noexcept { return (*this)[0]; }
constexpr decltype(auto) r() const noexcept { return (*this)[0]; }
constexpr decltype(auto) g() noexcept { return (*this)[1]; }
constexpr decltype(auto) g() const noexcept { return (*this)[1]; }
constexpr decltype(auto) b() noexcept { return (*this)[2]; }
constexpr decltype(auto) b() const noexcept { return (*this)[2]; }
constexpr decltype(auto) a() noexcept { return (*this)[3]; }
constexpr decltype(auto) a() const noexcept { return (*this)[3]; }
public: // conversion
template <typename OtherComponentT>
explicit operator ColorT<OtherComponentT, 4>() const noexcept {
return color_cast<ColorT<OtherComponentT, 4>>(*this);
}
}; // template <...> class ColorT final
//**************************************************************************
// ColorPaletteT
//**************************************************************************
template <typename ColorT>
struct ColorPaletteT {
using color_type = ::util::remove_cvref_t<ColorT>;
color_type Primary{ 0 };
color_type Secondary{ 0 };
color_type Background{ 0 };
}; // template <...> class ColorPaletteT
//-------------------------------------------------------------------------
template <typename ColorT, std::size_t CountT>
struct ColorPaletteT<::Color::ColorT<ColorT, CountT>> {
using color_type = ::Color::ColorT<ColorT, CountT>;
color_type Primary{ };
color_type Secondary{ };
color_type Background{ };
};
//**************************************************************************
// Specialisations
//**************************************************************************
template <typename TypeT>
using Color1T = ::Color::ColorT<TypeT, 1>;
template <typename TypeT>
using Color2T = ::Color::ColorT<TypeT, 2>;
template <typename TypeT>
using Color3T = ::Color::ColorT<TypeT, 3>;
template <typename TypeT>
using Color4T = ::Color::ColorT<TypeT, 4>;
// Color
using Color3ub = ::Color::Color3T<std::uint8_t>;
using Color3f = ::Color::Color3T<float>;
using Color4ub = ::Color::Color4T<std::uint8_t>;
using Color4f = ::Color::Color4T<float>;
// ColorPalette
using ColorPalette = ::Color::ColorPaletteT<std::uint32_t>;
using ColorPalette3ub = ::Color::ColorPaletteT<Color3ub>;
using ColorPalette4ub = ::Color::ColorPaletteT<Color4ub>;
using ColorPalette3f = ::Color::ColorPaletteT<Color3f>;
using ColorPalette4f = ::Color::ColorPaletteT<Color4f>;
} // namespace Color
#endif // GUID_B9A272CC_A6CB_43C5_9096_25CCA86D59C0