ASW Lib
A.D.S. Games SDL Wrapper Library. A library targeted at Allegro4 users who want to switch to SDL3 and use modern c++.
Loading...
Searching...
No Matches
color.h
Go to the documentation of this file.
1#ifndef ASW_COLOR_H
2#define ASW_COLOR_H
3
4#include <SDL3/SDL.h>
5
6#include <algorithm>
7#include <cctype>
8#include <cmath>
9#include <string>
10
11namespace asw {
12
14using FColor = SDL_FColor;
15
18struct Color {
25 static uint8_t to_channel(float value)
26 {
27 return static_cast<uint8_t>(std::lround(std::clamp(value, 0.0F, 255.0F)));
28 }
29
30 uint8_t r;
31 uint8_t g;
32 uint8_t b;
33 uint8_t a;
34
37 : r(0)
38 , g(0)
39 , b(0)
40 , a(255)
41 {
42 }
43
51 Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255)
52 : r(r)
53 , g(g)
54 , b(b)
55 , a(a)
56 {
57 }
58
67 static Color from_float(float r, float g, float b, float a = 1.0F)
68 {
69 return { to_channel(r * 255.0F), to_channel(g * 255.0F), to_channel(b * 255.0F),
70 to_channel(a * 255.0F) };
71 }
72
78 {
79 return { static_cast<float>(r) / 255.0F, static_cast<float>(g) / 255.0F,
80 static_cast<float>(b) / 255.0F, static_cast<float>(a) / 255.0F };
81 }
82
88 static Color from_hex(const std::string& hex)
89 {
90 if (hex.empty() || hex[0] != '#' || (hex.length() != 7 && hex.length() != 9)) {
91 return Color(); // Invalid format, return default color
92 }
93
94 // std::stoi throws on bad digits, so check them first
95 const bool digits_ok = std::all_of(hex.begin() + 1, hex.end(),
96 [](char c) { return std::isxdigit(static_cast<unsigned char>(c)) != 0; });
97 if (!digits_ok) {
98 return Color();
99 }
100
101 const auto r = static_cast<uint8_t>(std::stoi(hex.substr(1, 2), nullptr, 16));
102 const auto g = static_cast<uint8_t>(std::stoi(hex.substr(3, 2), nullptr, 16));
103 const auto b = static_cast<uint8_t>(std::stoi(hex.substr(5, 2), nullptr, 16));
104 uint8_t a = 255; // Default alpha
105
106 if (hex.length() == 9) {
107 a = static_cast<uint8_t>(std::stoi(hex.substr(7, 2), nullptr, 16));
108 }
109
110 return { r, g, b, a };
111 }
112
117 Color lighten(float percentage) const
118 {
119 const float p = std::clamp(percentage, 0.0F, 1.0F);
120 return { to_channel(r + ((255 - r) * p)), to_channel(g + ((255 - g) * p)),
121 to_channel(b + ((255 - b) * p)), a };
122 }
123
128 Color darken(float percentage) const
129 {
130 const float keep = 1.0F - std::clamp(percentage, 0.0F, 1.0F);
131 return { to_channel(r * keep), to_channel(g * keep), to_channel(b * keep), a };
132 }
133
138 Color blend(const Color& color) const
139 {
140 float alpha = a / 255.0F;
141 return { static_cast<uint8_t>(r * alpha + color.r * (1 - alpha)),
142 static_cast<uint8_t>(g * alpha + color.g * (1 - alpha)),
143 static_cast<uint8_t>(b * alpha + color.b * (1 - alpha)), 255 };
144 }
145
152 Color lerp(const Color& color, float t) const
153 {
154 const float p = std::clamp(t, 0.0F, 1.0F);
155 return { to_channel(r + ((color.r - r) * p)), to_channel(g + ((color.g - g) * p)),
156 to_channel(b + ((color.b - b) * p)), to_channel(a + ((color.a - a) * p)) };
157 }
158
162 Color invert() const
163 {
164 return { static_cast<uint8_t>(255 - r), static_cast<uint8_t>(255 - g),
165 static_cast<uint8_t>(255 - b), a };
166 }
167
172 {
173 auto gray = static_cast<uint8_t>(0.299F * r + 0.587F * g + 0.114F * b);
174 return { gray, gray, gray, a };
175 }
176
182 Color with_alpha(uint8_t alpha) const
183 {
184 return { r, g, b, alpha };
185 }
186};
187
189namespace color {
190 // Basic color names
191 inline const Color black = Color::from_hex("#000000");
192 inline const Color silver = Color::from_hex("#c0c0c0");
193 inline const Color gray = Color::from_hex("#808080");
194 inline const Color white = Color::from_hex("#ffffff");
195 inline const Color maroon = Color::from_hex("#800000");
196 inline const Color red = Color::from_hex("#ff0000");
197 inline const Color purple = Color::from_hex("#800080");
198 inline const Color fuchsia = Color::from_hex("#ff00ff");
199 inline const Color green = Color::from_hex("#008000");
200 inline const Color lime = Color::from_hex("#00ff00");
201 inline const Color olive = Color::from_hex("#808000");
202 inline const Color yellow = Color::from_hex("#ffff00");
203 inline const Color navy = Color::from_hex("#000080");
204 inline const Color blue = Color::from_hex("#0000ff");
205 inline const Color teal = Color::from_hex("#008080");
206 inline const Color aqua = Color::from_hex("#00ffff");
207
208 // Extended color names
209 inline const Color aliceblue = Color::from_hex("#f0f8ff");
210 inline const Color antiquewhite = Color::from_hex("#faebd7");
211 inline const Color aquamarine = Color::from_hex("#7fffd4");
212 inline const Color azure = Color::from_hex("#f0ffff");
213 inline const Color beige = Color::from_hex("#f5f5dc");
214 inline const Color bisque = Color::from_hex("#ffe4c4");
215 inline const Color blanchedalmond = Color::from_hex("#ffebcd");
216 inline const Color blueviolet = Color::from_hex("#8a2be2");
217 inline const Color brown = Color::from_hex("#a52a2a");
218 inline const Color burlywood = Color::from_hex("#deb887");
219 inline const Color cadetblue = Color::from_hex("#5f9ea0");
220 inline const Color chartreuse = Color::from_hex("#7fff00");
221 inline const Color chocolate = Color::from_hex("#d2691e");
222 inline const Color coral = Color::from_hex("#ff7f50");
223 inline const Color cornflowerblue = Color::from_hex("#6495ed");
224 inline const Color cornsilk = Color::from_hex("#fff8dc");
225 inline const Color crimson = Color::from_hex("#dc143c");
226 inline const Color cyan = Color::from_hex("#00ffff");
227 inline const Color darkblue = Color::from_hex("#00008b");
228 inline const Color darkcyan = Color::from_hex("#008b8b");
229 inline const Color darkgoldenrod = Color::from_hex("#b8860b");
230 inline const Color darkgray = Color::from_hex("#a9a9a9");
231 inline const Color darkgreen = Color::from_hex("#006400");
232 inline const Color darkgrey = Color::from_hex("#a9a9a9");
233 inline const Color darkkhaki = Color::from_hex("#bdb76b");
234 inline const Color darkmagenta = Color::from_hex("#8b008b");
235 inline const Color darkolivegreen = Color::from_hex("#556b2f");
236 inline const Color darkorange = Color::from_hex("#ff8c00");
237 inline const Color darkorchid = Color::from_hex("#9932cc");
238 inline const Color darkred = Color::from_hex("#8b0000");
239 inline const Color darksalmon = Color::from_hex("#e9967a");
240 inline const Color darkseagreen = Color::from_hex("#8fbc8f");
241 inline const Color darkslateblue = Color::from_hex("#483d8b");
242 inline const Color darkslategray = Color::from_hex("#2f4f4f");
243 inline const Color darkslategrey = Color::from_hex("#2f4f4f");
244 inline const Color darkturquoise = Color::from_hex("#00ced1");
245 inline const Color darkviolet = Color::from_hex("#9400d3");
246 inline const Color deeppink = Color::from_hex("#ff1493");
247 inline const Color deepskyblue = Color::from_hex("#00bfff");
248 inline const Color dimgray = Color::from_hex("#696969");
249 inline const Color dimgrey = Color::from_hex("#696969");
250 inline const Color dodgerblue = Color::from_hex("#1e90ff");
251 inline const Color firebrick = Color::from_hex("#b22222");
252 inline const Color floralwhite = Color::from_hex("#fffaf0");
253 inline const Color forestgreen = Color::from_hex("#228b22");
254 inline const Color gainsboro = Color::from_hex("#dcdcdc");
255 inline const Color ghostwhite = Color::from_hex("#f8f8ff");
256 inline const Color gold = Color::from_hex("#ffd700");
257 inline const Color goldenrod = Color::from_hex("#daa520");
258 inline const Color greenyellow = Color::from_hex("#adff2f");
259 inline const Color grey = Color::from_hex("#808080");
260 inline const Color honeydew = Color::from_hex("#f0fff0");
261 inline const Color hotpink = Color::from_hex("#ff69b4");
262 inline const Color indianred = Color::from_hex("#cd5c5c");
263 inline const Color indigo = Color::from_hex("#4b0082");
264 inline const Color ivory = Color::from_hex("#fffff0");
265 inline const Color khaki = Color::from_hex("#f0e68c");
266 inline const Color lavender = Color::from_hex("#e6e6fa");
267 inline const Color lavenderblush = Color::from_hex("#fff0f5");
268 inline const Color lawngreen = Color::from_hex("#7cfc00");
269 inline const Color lemonchiffon = Color::from_hex("#fffacd");
270 inline const Color lightblue = Color::from_hex("#add8e6");
271 inline const Color lightcoral = Color::from_hex("#f08080");
272 inline const Color lightcyan = Color::from_hex("#e0ffff");
273 inline const Color lightgoldenrodyellow = Color::from_hex("#fafad2");
274 inline const Color lightgray = Color::from_hex("#d3d3d3");
275 inline const Color lightgreen = Color::from_hex("#90ee90");
276 inline const Color lightgrey = Color::from_hex("#d3d3d3");
277 inline const Color lightpink = Color::from_hex("#ffb6c1");
278 inline const Color lightsalmon = Color::from_hex("#ffa07a");
279 inline const Color lightseagreen = Color::from_hex("#20b2aa");
280 inline const Color lightskyblue = Color::from_hex("#87cefa");
281 inline const Color lightslategray = Color::from_hex("#778899");
282 inline const Color lightslategrey = Color::from_hex("#778899");
283 inline const Color lightsteelblue = Color::from_hex("#b0c4de");
284 inline const Color lightyellow = Color::from_hex("#ffffe0");
285 inline const Color limegreen = Color::from_hex("#32cd32");
286 inline const Color linen = Color::from_hex("#faf0e6");
287 inline const Color magenta = Color::from_hex("#ff00ff");
288 inline const Color mediumaquamarine = Color::from_hex("#66cdaa");
289 inline const Color mediumblue = Color::from_hex("#0000cd");
290 inline const Color mediumorchid = Color::from_hex("#ba55d3");
291 inline const Color mediumpurple = Color::from_hex("#9370db");
292 inline const Color mediumseagreen = Color::from_hex("#3cb371");
293 inline const Color mediumslateblue = Color::from_hex("#7b68ee");
294 inline const Color mediumspringgreen = Color::from_hex("#00fa9a");
295 inline const Color mediumturquoise = Color::from_hex("#48d1cc");
296 inline const Color mediumvioletred = Color::from_hex("#c71585");
297 inline const Color midnightblue = Color::from_hex("#191970");
298 inline const Color mintcream = Color::from_hex("#f5fffa");
299 inline const Color mistyrose = Color::from_hex("#ffe4e1");
300 inline const Color moccasin = Color::from_hex("#ffe4b5");
301 inline const Color navajowhite = Color::from_hex("#ffdead");
302 inline const Color oldlace = Color::from_hex("#fdf5e6");
303 inline const Color olivedrab = Color::from_hex("#6b8e23");
304 inline const Color orange = Color::from_hex("#ffa500");
305 inline const Color orangered = Color::from_hex("#ff4500");
306 inline const Color orchid = Color::from_hex("#da70d6");
307 inline const Color palegoldenrod = Color::from_hex("#eee8aa");
308 inline const Color palegreen = Color::from_hex("#98fb98");
309 inline const Color paleturquoise = Color::from_hex("#afeeee");
310 inline const Color palevioletred = Color::from_hex("#db7093");
311 inline const Color papayawhip = Color::from_hex("#ffefd5");
312 inline const Color peachpuff = Color::from_hex("#ffdab9");
313 inline const Color peru = Color::from_hex("#cd853f");
314 inline const Color pink = Color::from_hex("#ffc0cb");
315 inline const Color plum = Color::from_hex("#dda0dd");
316 inline const Color powderblue = Color::from_hex("#b0e0e6");
317 inline const Color rebeccapurple = Color::from_hex("#663399");
318 inline const Color rosybrown = Color::from_hex("#bc8f8f");
319 inline const Color royalblue = Color::from_hex("#4169e1");
320 inline const Color saddlebrown = Color::from_hex("#8b4513");
321 inline const Color salmon = Color::from_hex("#fa8072");
322 inline const Color sandybrown = Color::from_hex("#f4a460");
323 inline const Color seagreen = Color::from_hex("#2e8b57");
324 inline const Color seashell = Color::from_hex("#fff5ee");
325 inline const Color sienna = Color::from_hex("#a0522d");
326 inline const Color skyblue = Color::from_hex("#87ceeb");
327 inline const Color slateblue = Color::from_hex("#6a5acd");
328 inline const Color slategray = Color::from_hex("#708090");
329 inline const Color slategrey = Color::from_hex("#708090");
330 inline const Color snow = Color::from_hex("#fffafa");
331 inline const Color springgreen = Color::from_hex("#00ff7f");
332 inline const Color steelblue = Color::from_hex("#4682b4");
333 inline const Color tan = Color::from_hex("#d2b48c");
334 inline const Color thistle = Color::from_hex("#d8bfd8");
335 inline const Color tomato = Color::from_hex("#ff6347");
336 inline const Color turquoise = Color::from_hex("#40e0d0");
337 inline const Color violet = Color::from_hex("#ee82ee");
338 inline const Color wheat = Color::from_hex("#f5deb3");
339 inline const Color whitesmoke = Color::from_hex("#f5f5f5");
340 inline const Color yellowgreen = Color::from_hex("#9acd32");
341
342 // Special
343 inline const Color transparent = Color::from_hex("#00000000");
344} // namespace color
345
346} // namespace asw
347
348#endif // ASW_COLOR_H
const Color royalblue
Definition color.h:319
const Color slateblue
Definition color.h:327
const Color moccasin
Definition color.h:300
const Color seagreen
Definition color.h:323
const Color thistle
Definition color.h:334
const Color skyblue
Definition color.h:326
const Color azure
Definition color.h:212
const Color lightslategrey
Definition color.h:282
const Color mediumpurple
Definition color.h:291
const Color lightgray
Definition color.h:274
const Color aqua
Definition color.h:206
const Color hotpink
Definition color.h:261
const Color springgreen
Definition color.h:331
const Color white
Definition color.h:194
const Color darkcyan
Definition color.h:228
const Color plum
Definition color.h:315
const Color darkturquoise
Definition color.h:244
const Color lightgrey
Definition color.h:276
const Color deeppink
Definition color.h:246
const Color saddlebrown
Definition color.h:320
const Color blanchedalmond
Definition color.h:215
const Color indigo
Definition color.h:263
const Color lightseagreen
Definition color.h:279
const Color darkviolet
Definition color.h:245
const Color gray
Definition color.h:193
const Color mediumseagreen
Definition color.h:292
const Color mediumslateblue
Definition color.h:293
const Color brown
Definition color.h:217
const Color cornflowerblue
Definition color.h:223
const Color greenyellow
Definition color.h:258
const Color aquamarine
Definition color.h:211
const Color midnightblue
Definition color.h:297
const Color forestgreen
Definition color.h:253
const Color limegreen
Definition color.h:285
const Color snow
Definition color.h:330
const Color ghostwhite
Definition color.h:255
const Color sienna
Definition color.h:325
const Color firebrick
Definition color.h:251
const Color transparent
Definition color.h:343
const Color peachpuff
Definition color.h:312
const Color darkred
Definition color.h:238
const Color whitesmoke
Definition color.h:339
const Color darkgrey
Definition color.h:232
const Color coral
Definition color.h:222
const Color gainsboro
Definition color.h:254
const Color aliceblue
Definition color.h:209
const Color lightgoldenrodyellow
Definition color.h:273
const Color mintcream
Definition color.h:298
const Color lemonchiffon
Definition color.h:269
const Color lightsalmon
Definition color.h:278
const Color palevioletred
Definition color.h:310
const Color lawngreen
Definition color.h:268
const Color teal
Definition color.h:205
const Color rebeccapurple
Definition color.h:317
const Color burlywood
Definition color.h:218
const Color paleturquoise
Definition color.h:309
const Color orangered
Definition color.h:305
const Color cornsilk
Definition color.h:224
const Color mediumaquamarine
Definition color.h:288
const Color darkmagenta
Definition color.h:234
const Color orchid
Definition color.h:306
const Color darkslategrey
Definition color.h:243
const Color darkorange
Definition color.h:236
const Color salmon
Definition color.h:321
const Color ivory
Definition color.h:264
const Color lightgreen
Definition color.h:275
const Color honeydew
Definition color.h:260
const Color yellow
Definition color.h:202
const Color goldenrod
Definition color.h:257
const Color lightcyan
Definition color.h:272
const Color lightcoral
Definition color.h:271
const Color cyan
Definition color.h:226
const Color mediumblue
Definition color.h:289
const Color darkkhaki
Definition color.h:233
const Color tomato
Definition color.h:335
const Color rosybrown
Definition color.h:318
const Color indianred
Definition color.h:262
const Color navy
Definition color.h:203
const Color darkslateblue
Definition color.h:241
const Color silver
Definition color.h:192
const Color cadetblue
Definition color.h:219
const Color darkolivegreen
Definition color.h:235
const Color slategray
Definition color.h:328
const Color peru
Definition color.h:313
const Color fuchsia
Definition color.h:198
const Color darkseagreen
Definition color.h:240
const Color turquoise
Definition color.h:336
const Color lightpink
Definition color.h:277
const Color navajowhite
Definition color.h:301
const Color sandybrown
Definition color.h:322
const Color floralwhite
Definition color.h:252
const Color palegreen
Definition color.h:308
const Color antiquewhite
Definition color.h:210
const Color oldlace
Definition color.h:302
const Color darkorchid
Definition color.h:237
const Color green
Definition color.h:199
const Color lavender
Definition color.h:266
const Color lavenderblush
Definition color.h:267
const Color linen
Definition color.h:286
const Color bisque
Definition color.h:214
const Color crimson
Definition color.h:225
const Color dimgrey
Definition color.h:249
const Color magenta
Definition color.h:287
const Color khaki
Definition color.h:265
const Color violet
Definition color.h:337
const Color dimgray
Definition color.h:248
const Color lightslategray
Definition color.h:281
const Color mediumspringgreen
Definition color.h:294
const Color mediumorchid
Definition color.h:290
const Color chartreuse
Definition color.h:220
const Color mistyrose
Definition color.h:299
const Color orange
Definition color.h:304
const Color chocolate
Definition color.h:221
const Color tan
Definition color.h:333
const Color mediumvioletred
Definition color.h:296
const Color black
Definition color.h:191
const Color pink
Definition color.h:314
const Color purple
Definition color.h:197
const Color yellowgreen
Definition color.h:340
const Color olive
Definition color.h:201
const Color darksalmon
Definition color.h:239
const Color gold
Definition color.h:256
const Color lime
Definition color.h:200
const Color slategrey
Definition color.h:329
const Color grey
Definition color.h:259
const Color dodgerblue
Definition color.h:250
const Color olivedrab
Definition color.h:303
const Color darkslategray
Definition color.h:242
const Color steelblue
Definition color.h:332
const Color mediumturquoise
Definition color.h:295
const Color lightyellow
Definition color.h:284
const Color darkgray
Definition color.h:230
const Color powderblue
Definition color.h:316
const Color blueviolet
Definition color.h:216
const Color blue
Definition color.h:204
const Color maroon
Definition color.h:195
const Color deepskyblue
Definition color.h:247
const Color darkgreen
Definition color.h:231
const Color darkgoldenrod
Definition color.h:229
const Color darkblue
Definition color.h:227
const Color lightsteelblue
Definition color.h:283
const Color beige
Definition color.h:213
const Color palegoldenrod
Definition color.h:307
const Color wheat
Definition color.h:338
const Color lightskyblue
Definition color.h:280
const Color seashell
Definition color.h:324
const Color red
Definition color.h:196
const Color papayawhip
Definition color.h:311
const Color lightblue
Definition color.h:270
Definition camera.cpp:16
SDL_FColor FColor
A colour with each channel as a float from 0.0 to 1.0.
Definition color.h:14
RGBA color struct with 8-bit channels.
Definition color.h:18
Color with_alpha(uint8_t alpha) const
Adjust the alpha of a color.
Definition color.h:182
Color blend(const Color &color) const
Blend two colors together using alpha blending.
Definition color.h:138
uint8_t b
Definition color.h:32
static Color from_float(float r, float g, float b, float a=1.0F)
From float RGBA values (0.0-1.0) to Color.
Definition color.h:67
uint8_t r
Definition color.h:30
Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Construct a Color from RGBA values.
Definition color.h:51
Color invert() const
Invert a color.
Definition color.h:162
static uint8_t to_channel(float value)
Round a 0-255 channel value and clamp it into range. Casting an out of range float straight to uint8_...
Definition color.h:25
uint8_t g
Definition color.h:31
static Color from_hex(const std::string &hex)
From a hex string (e.g. "#RRGGBBAA") to a Color.
Definition color.h:88
Color lerp(const Color &color, float t) const
Mix towards another color, including alpha.
Definition color.h:152
Color grayscale() const
Convert a color to grayscale.
Definition color.h:171
uint8_t a
Definition color.h:33
Color()
Construct a default Color (black, fully opaque).
Definition color.h:36
Color darken(float percentage) const
Darken a color by a given percentage.
Definition color.h:128
Color lighten(float percentage) const
Lighten a color by a given percentage.
Definition color.h:117
FColor to_fcolor() const
To a float colour, with each channel from 0.0 to 1.0.
Definition color.h:77