Audacity 3.2.0
Color.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*!********************************************************************
3
4 Audacity: A Digital Audio Editor
5
6 Color.h
7
8 Dmitry Vedenko
9
10**********************************************************************/
11#pragma once
12
13#include <algorithm>
14#include <cstdint>
15#include <optional>
16
17namespace graphics
18{
19
21
28struct Color final
29{
30public:
32 constexpr Color() noexcept : mABGR(0) {}
33
34 constexpr Color(const Color&) noexcept = default;
35 constexpr Color(Color&&) noexcept = default;
36 Color& operator=(const Color&) noexcept = default;
37 Color& operator=(Color&&) noexcept = default;
38
40 constexpr Color(uint8_t rr, uint8_t gg, uint8_t bb, uint8_t aa = 255) noexcept
41 : mRed(rr)
42 , mGreen(gg)
43 , mBlue(bb)
44 , mAlpha(aa)
45 {
46 }
47
49 static constexpr Color FromABGR(uint32_t abgr) noexcept
50 {
51 return Color(abgr);
52 }
53
55 static constexpr Color FromFloatRGBA(float r, float g, float b, float a) noexcept
56 {
57 return Color(
58 Color::Scale(255, r), Color::Scale(255, g), Color::Scale(255, b),
59 Color::Scale(255, a));
60 }
61
63 constexpr Color WithOpacity(float opacity) const noexcept
64 {
65 return Color(mRed, mGreen, mBlue, Scale(mAlpha, opacity));
66 }
67
69 constexpr Color WithFloatAlpha(float alpha) const noexcept
70 {
71 return Color(mRed, mGreen, mBlue, Scale(255, alpha));
72 }
73
75 constexpr Color WithAlpha(uint8_t alpha) const noexcept
76 {
77 return Color(mRed, mGreen, mBlue, alpha);
78 }
79
81 constexpr Color WithoutAlpha() const noexcept
82 {
83 return Color(mRed, mGreen, mBlue, uint8_t(255));
84 }
85
87 constexpr uint8_t GetRed() const noexcept
88 {
89 return mRed;
90 }
91
93 constexpr uint8_t GetGreen() const noexcept
94 {
95 return mGreen;
96 }
97
99 constexpr uint8_t GetBlue() const noexcept
100 {
101 return mBlue;
102 }
103
105 constexpr uint8_t GetAlpha() const noexcept
106 {
107 return mAlpha;
108 }
109
111 constexpr uint32_t GetRGBA() const noexcept
112 {
113 return (uint32_t(mRed) << 24) + (uint32_t(mGreen) << 16) +
114 (uint32_t(mBlue) << 8) + (uint32_t(mAlpha));
115 }
116
118 constexpr uint32_t GetABGR() const noexcept
119 {
120 return mABGR;
121 }
122
124 constexpr uint32_t GetRGB() const noexcept
125 {
126 return (uint32_t(mRed) << 16) + (uint32_t(mGreen) << 8) +
127 (uint32_t(mBlue));
128 }
129
130 friend constexpr Color operator+(Color lhs, Color rhs) noexcept
131 {
132 return Color(
133 Add(lhs.mRed, rhs.mRed), Add(lhs.mGreen, rhs.mGreen),
134 Add(lhs.mBlue, rhs.mBlue), Add(lhs.mAlpha, rhs.mAlpha));
135 }
136
137 friend constexpr Color operator-(Color lhs, Color rhs) noexcept
138 {
139 return Color(
140 Sub(lhs.mRed, rhs.mRed), Sub(lhs.mGreen, rhs.mGreen),
141 Sub(lhs.mBlue, rhs.mBlue), Sub(lhs.mAlpha, rhs.mAlpha));
142 }
143
144 friend constexpr Color operator*(Color lhs, Color rhs) noexcept
145 {
146 return Color(
147 Mul(lhs.mRed, rhs.mRed), Mul(lhs.mGreen, rhs.mGreen),
148 Mul(lhs.mBlue, rhs.mBlue), Mul(lhs.mAlpha, rhs.mAlpha));
149 }
150
151 template <typename ScaleType>
152 friend constexpr Color lerp(Color lhs, Color rhs, ScaleType t);
153
154 Color& operator+=(Color rhs) noexcept
155 {
156 return *this = *this + rhs;
157 }
158
159 Color& operator-=(Color rhs) noexcept
160 {
161 return *this = *this - rhs;
162 }
163
164 Color& operator*=(Color rhs) noexcept
165 {
166 return *this = *this * rhs;
167 }
168
169 bool operator==(const Color& rhs) const noexcept
170 {
171 return mABGR == rhs.mABGR;
172 }
173 bool operator!=(const Color& rhs) const noexcept
174 {
175 return mABGR != rhs.mABGR;
176 }
177
178private:
179 static constexpr uint8_t Add(uint8_t a, uint8_t b) noexcept
180 {
181 return uint8_t((a + b) & 0xFF);
182 }
183
184 static constexpr uint8_t Sub(uint8_t a, uint8_t b) noexcept
185 {
186 return (a > b) ? a - b : 0;
187 }
188
189 static constexpr uint8_t Mul(uint8_t a, uint8_t b) noexcept
190 {
191 return uint8_t((uint16_t(a) * uint16_t(b) / 255) & 0xFF);
192 }
193
194 static constexpr uint8_t Scale(uint8_t a, float s) noexcept
195 {
196 return uint8_t(a * std::max(0.0f, std::min(1.0f, s)) + 0.5f);
197 }
198
199 template <typename ScaleType>
200 static constexpr uint8_t Lerp(uint8_t a, uint8_t b, ScaleType t)
201 {
202 static_assert(std::is_floating_point<ScaleType>::value);
203
204 const auto lerpValue = (a + (b - a) * t);
205
206 const auto roundLerpValue =
207 static_cast<int16_t>(lerpValue + ScaleType(0.5));
208
209 const auto ui8LerpValue =
210 static_cast<uint8_t>(std::max<decltype(roundLerpValue)>(
211 0, std::min<decltype(roundLerpValue)>(255, roundLerpValue)));
212
213 return ui8LerpValue;
214 }
215
216 explicit constexpr Color(uint32_t abgr) noexcept
217 : mABGR(abgr)
218 {
219 }
220
221 union
222 {
223 uint32_t mABGR;
224 struct
225 {
227 };
228 };
229};
230
232template <typename ScaleType>
233constexpr Color lerp(Color lhs, Color rhs, ScaleType t)
234{
235 return { Color::Lerp(lhs.mRed, rhs.mRed, t),
236 Color::Lerp(lhs.mGreen, rhs.mGreen, t),
237 Color::Lerp(lhs.mBlue, rhs.mBlue, t),
238 Color::Lerp(lhs.mAlpha, rhs.mAlpha, t) };
239}
240
242GRAPHICS_API constexpr Color ColorFromRGA(uint32_t rgba) noexcept
243{
244 return Color(
245 uint8_t((rgba >> 24) & 0xFF), uint8_t((rgba >> 16) & 0xFF),
246 uint8_t((rgba >> 8) & 0xFF), uint8_t((rgba >> 0) & 0xFF));
247}
248
250GRAPHICS_API constexpr Color ColorFromABGR(uint32_t abgr) noexcept
251{
252 return Color::FromABGR(abgr);
253}
254
256GRAPHICS_API constexpr Color ColorFromRGB(uint32_t rgb) noexcept
257{
258 return Color(
259 uint8_t((rgb >> 16) & 0xFF), uint8_t((rgb >> 8) & 0xFF),
260 uint8_t((rgb >> 0) & 0xFF), uint8_t(0xFF));
261}
262
264GRAPHICS_API constexpr Color ColorFromARGB(uint32_t argb) noexcept
265{
266 return Color(
267 uint8_t((argb >> 16) & 0xFF), uint8_t((argb >> 8) & 0xFF),
268 uint8_t((argb >> 0) & 0xFF), uint8_t((argb >> 24) & 0xFF));
269}
270
272GRAPHICS_API constexpr Color
273ColorFromFloatRGBA(float r, float g, float b, float a) noexcept
274{
275 return Color::FromFloatRGBA(r, g, b, a);
276}
277
278namespace Colors
279{
281constexpr Color White = ColorFromABGR(0xFFFFFFFF);
283constexpr Color Black = ColorFromABGR(0xFF000000);
285constexpr Color Transparent = ColorFromABGR(0x00000000);
287constexpr Color Red = ColorFromABGR(0xFF0000FF);
289constexpr Color Green = ColorFromABGR(0xFF00FF00);
291constexpr Color Blue = ColorFromABGR(0xFFFF0000);
292} // namespace Colors
293
294} // namespace graphics
int min(int a, int b)
constexpr Color Transparent
Transparent black color constant.
Definition: Color.h:285
constexpr Color Green
Green color constant.
Definition: Color.h:289
constexpr Color Blue
Blue color constant.
Definition: Color.h:291
constexpr Color White
Opaque white color constant.
Definition: Color.h:281
constexpr Color Black
Opaque black color constant.
Definition: Color.h:283
constexpr Color Red
Red color constant.
Definition: Color.h:287
Definition: Color.h:18
GRAPHICS_API constexpr Color ColorFromFloatRGBA(float r, float g, float b, float a) noexcept
A helper function to create a color from the floating point components.
Definition: Color.h:273
GRAPHICS_API constexpr Color ColorFromABGR(uint32_t abgr) noexcept
A helper function to create a color from a 32-bit ABGR integer.
Definition: Color.h:250
GRAPHICS_API constexpr Color ColorFromARGB(uint32_t argb) noexcept
A helper function to create a color from a 32-bit ARGB integer.
Definition: Color.h:264
constexpr Color lerp(Color lhs, Color rhs, ScaleType t)
Performs linear interpolation between two colors.
Definition: Color.h:233
GRAPHICS_API constexpr Color ColorFromRGA(uint32_t rgba) noexcept
A helper function to create a color from a 32-bit RGBA integer.
Definition: Color.h:242
GRAPHICS_API constexpr Color ColorFromRGB(uint32_t rgb) noexcept
A helper function to create a color from a 32-bit RGB integer. Alpha is set to 255.
Definition: Color.h:256
Class for storing color in 32-bit format.
Definition: Color.h:29
static constexpr uint8_t Mul(uint8_t a, uint8_t b) noexcept
Definition: Color.h:189
bool operator==(const Color &rhs) const noexcept
Definition: Color.h:169
constexpr Color WithoutAlpha() const noexcept
Returns a copy of this color with the alpha set to the max.
Definition: Color.h:81
friend constexpr Color operator*(Color lhs, Color rhs) noexcept
Definition: Color.h:144
constexpr Color() noexcept
Constructs a transparent black color.
Definition: Color.h:32
Color & operator+=(Color rhs) noexcept
Definition: Color.h:154
Color & operator*=(Color rhs) noexcept
Definition: Color.h:164
constexpr Color(Color &&) noexcept=default
uint32_t mABGR
Definition: Color.h:223
static constexpr uint8_t Lerp(uint8_t a, uint8_t b, ScaleType t)
Definition: Color.h:200
constexpr Color WithAlpha(uint8_t alpha) const noexcept
Returns a copy of this color with a new alpha value.
Definition: Color.h:75
constexpr Color(uint32_t abgr) noexcept
Definition: Color.h:216
uint8_t mBlue
Definition: Color.h:226
uint8_t mRed
Definition: Color.h:226
constexpr uint8_t GetBlue() const noexcept
Returns blue component of this color.
Definition: Color.h:99
constexpr uint8_t GetGreen() const noexcept
Returns green component of this color.
Definition: Color.h:93
static constexpr uint8_t Scale(uint8_t a, float s) noexcept
Definition: Color.h:194
constexpr Color(const Color &) noexcept=default
static constexpr Color FromFloatRGBA(float r, float g, float b, float a) noexcept
Constructs a color from individual floating point components. This value is clamped to [0,...
Definition: Color.h:55
constexpr uint32_t GetRGBA() const noexcept
Returns color value as RGBA 32-bit integer.
Definition: Color.h:111
uint8_t mAlpha
Definition: Color.h:226
constexpr Color WithOpacity(float opacity) const noexcept
Returns a copy of this color with alpha component scaled by opacity.
Definition: Color.h:63
bool operator!=(const Color &rhs) const noexcept
Definition: Color.h:173
static constexpr uint8_t Sub(uint8_t a, uint8_t b) noexcept
Definition: Color.h:184
friend constexpr Color lerp(Color lhs, Color rhs, ScaleType t)
Performs linear interpolation between two colors.
Definition: Color.h:233
friend constexpr Color operator+(Color lhs, Color rhs) noexcept
Definition: Color.h:130
constexpr uint32_t GetRGB() const noexcept
Returns color value as RGB integer.
Definition: Color.h:124
constexpr Color WithFloatAlpha(float alpha) const noexcept
Returns a copy of this color with a new alpha value. Value is clamped to [0, 1].
Definition: Color.h:69
constexpr uint32_t GetABGR() const noexcept
Returns color value as ABGR 32-bit integer.
Definition: Color.h:118
constexpr uint8_t GetRed() const noexcept
Returns red component of this color.
Definition: Color.h:87
friend constexpr Color operator-(Color lhs, Color rhs) noexcept
Definition: Color.h:137
uint8_t mGreen
Definition: Color.h:226
constexpr uint8_t GetAlpha() const noexcept
Returns alpha component of this color.
Definition: Color.h:105
static constexpr Color FromABGR(uint32_t abgr) noexcept
Constructs a color from ABGR 32-bit integer value.
Definition: Color.h:49
Color & operator-=(Color rhs) noexcept
Definition: Color.h:159
static constexpr uint8_t Add(uint8_t a, uint8_t b) noexcept
Definition: Color.h:179