-
Notifications
You must be signed in to change notification settings - Fork 0
/
RGBA.h
175 lines (152 loc) · 3.22 KB
/
RGBA.h
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
//
// Created by Keith on 2/1/2021.
//
#pragma once
#include <algorithm>
#include <cstdint>
#include <limits>
#include <ostream>
#include <print>
template <typename T>
constexpr T k_default_alpha = std::numeric_limits<T>::max();
template <>
constexpr float k_default_alpha<float> = 1.0f;
template <typename T>
struct RGBA
{
using value_type = T;
using size_type = std::uint32_t;
constexpr RGBA() noexcept
: r(0)
, g(0)
, b(0)
, a(k_default_alpha<T>)
{
}
constexpr RGBA(T ir, T ig, T ib, T ia = k_default_alpha<T>) noexcept
: r(ir)
, g(ig)
, b(ib)
, a(ia)
{
}
T& operator[](size_type idx) noexcept
{
switch (idx) {
case 0:
return r;
case 1:
return g;
case 2:
return b;
case 3:
return a;
default:
assert(!"Should not get here");
}
return r;
}
T operator[](size_type idx) const noexcept
{
switch (idx) {
case 0:
return r;
case 1:
return g;
case 2:
return b;
case 3:
return a;
default:
assert(!"Should not get here");
}
return r;
}
T r;
T g;
T b;
T a;
};
template <typename T>
RGBA<T>& operator+=(RGBA<T>& a, const RGBA<T>& b) noexcept
{
a.r += b.r;
a.g += b.g;
a.b += b.b;
a.a += b.a;
return a;
}
template <typename T>
RGBA<T>& operator*=(RGBA<T>& a, T b) noexcept
{
a.r *= b;
a.g *= b;
a.b *= b;
a.a *= a;
return a;
}
template <typename T>
RGBA<T>& operator/=(RGBA<T>& a, T b) noexcept
{
a.r /= b;
a.g /= b;
a.b /= b;
a.a /= b;
return a;
}
// Pass-by-value on purpose
template <typename T>
RGBA<T> operator+(RGBA<T> a, const RGBA<T>& b) noexcept
{
return a += b;
}
// Pass-by-value on purpose
template <typename T>
RGBA<T> operator*(RGBA<T> a, T b) noexcept
{
return a *= b;
}
// Pass-by-value on purpose
template <typename T>
RGBA<T> operator*(T b, RGBA<T> a) noexcept
{
return a *= b;
}
// Pass-by-value on purpose
template <typename T>
RGBA<T> operator/(RGBA<T> a, T b) noexcept
{
return a /= b;
}
template <typename T>
inline std::ostream& operator<<(std::ostream& outs, const RGBA<T>& c)
{
std::print(outs, "{} {} {} {}", c.r, c.g, c.g, c.a);
return outs;
}
using RGBAf = RGBA<float>;
using RGBA8 = RGBA<std::uint8_t>;
using RGBA16 = RGBA<std::uint16_t>;
using RGBA32 = RGBA<std::uint32_t>;
template <typename T>
inline RGBAf to_float(const RGBA<T>& c) noexcept
{
constexpr auto max = std::numeric_limits<typename RGBA<T>::value_type>::max();
static_assert(max > 0); // That'd be weird, right?
constexpr float maxf = static_cast<float>(max);
return { static_cast<float>(c.r) / maxf,
static_cast<float>(c.g) / maxf,
static_cast<float>(c.b) / maxf,
static_cast<float>(c.a) / maxf };
}
inline const RGBAf& to_float(const RGBAf& c) noexcept
{
return c;
}
inline RGBAf clamp(const RGBAf& c) noexcept
{
return { std::clamp(c.r, 0.0f, 1.0f),
std::clamp(c.g, 0.0f, 1.0f),
std::clamp(c.b, 0.0f, 1.0f),
std::clamp(c.a, 0.0f, 1.0f) };
}