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 <string>
7
8namespace asw {
11struct Color {
12 uint8_t r;
13 uint8_t g;
14 uint8_t b;
15 uint8_t a;
16
19 : r(0)
20 , g(0)
21 , b(0)
22 , a(255)
23 {
24 }
25
33 Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255)
34 : r(r)
35 , g(g)
36 , b(b)
37 , a(a)
38 {
39 }
40
49 static Color from_float(float r, float g, float b, float a = 1.0F)
50 {
51 return { static_cast<uint8_t>(r * 255), static_cast<uint8_t>(g * 255),
52 static_cast<uint8_t>(b * 255), static_cast<uint8_t>(a * 255) };
53 }
54
60 static Color from_hex(const std::string& hex)
61 {
62 if (hex.empty() || hex[0] != '#' || (hex.length() != 7 && hex.length() != 9)) {
63 return Color(); // Invalid format, return default color
64 }
65
66 uint8_t r = std::stoi(hex.substr(1, 2), nullptr, 16);
67 uint8_t g = std::stoi(hex.substr(3, 2), nullptr, 16);
68 uint8_t b = std::stoi(hex.substr(5, 2), nullptr, 16);
69 uint8_t a = 255; // Default alpha
70
71 if (hex.length() == 9) {
72 a = std::stoi(hex.substr(7, 2), nullptr, 16);
73 }
74
75 return { r, g, b, a };
76 }
77
82 Color lighten(float percentage) const
83 {
84 return { static_cast<uint8_t>(r + (255 - r) * percentage),
85 static_cast<uint8_t>(g + (255 - g) * percentage),
86 static_cast<uint8_t>(b + (255 - b) * percentage), a };
87 }
88
93 Color darken(float percentage) const
94 {
95 return { static_cast<uint8_t>(r * (1 - percentage)),
96 static_cast<uint8_t>(g * (1 - percentage)), static_cast<uint8_t>(b * (1 - percentage)),
97 a };
98 }
99
104 Color blend(const Color& color) const
105 {
106 float alpha = a / 255.0F;
107 return { static_cast<uint8_t>(r * alpha + color.r * (1 - alpha)),
108 static_cast<uint8_t>(g * alpha + color.g * (1 - alpha)),
109 static_cast<uint8_t>(b * alpha + color.b * (1 - alpha)), 255 };
110 }
111
115 Color invert() const
116 {
117 return { static_cast<uint8_t>(255 - r), static_cast<uint8_t>(255 - g),
118 static_cast<uint8_t>(255 - b), a };
119 }
120
125 {
126 auto gray = static_cast<uint8_t>(0.299F * r + 0.587F * g + 0.114F * b);
127 return { gray, gray, gray, a };
128 }
129
135 Color with_alpha(uint8_t alpha) const
136 {
137 return { r, g, b, alpha };
138 }
139};
140
142namespace color {
143 // Basic color names
144 inline const Color black = Color::from_hex("#000000");
145 inline const Color silver = Color::from_hex("#c0c0c0");
146 inline const Color gray = Color::from_hex("#808080");
147 inline const Color white = Color::from_hex("#ffffff");
148 inline const Color maroon = Color::from_hex("#800000");
149 inline const Color red = Color::from_hex("#ff0000");
150 inline const Color purple = Color::from_hex("#800080");
151 inline const Color fuchsia = Color::from_hex("#ff00ff");
152 inline const Color green = Color::from_hex("#008000");
153 inline const Color lime = Color::from_hex("#00ff00");
154 inline const Color olive = Color::from_hex("#808000");
155 inline const Color yellow = Color::from_hex("#ffff00");
156 inline const Color navy = Color::from_hex("#000080");
157 inline const Color blue = Color::from_hex("#0000ff");
158 inline const Color teal = Color::from_hex("#008080");
159 inline const Color aqua = Color::from_hex("#00ffff");
160
161 // Extended color names
162 inline const Color aliceblue = Color::from_hex("#f0f8ff");
163 inline const Color antiquewhite = Color::from_hex("#faebd7");
164 inline const Color aquamarine = Color::from_hex("#7fffd4");
165 inline const Color azure = Color::from_hex("#f0ffff");
166 inline const Color beige = Color::from_hex("#f5f5dc");
167 inline const Color bisque = Color::from_hex("#ffe4c4");
168 inline const Color blanchedalmond = Color::from_hex("#ffebcd");
169 inline const Color blueviolet = Color::from_hex("#8a2be2");
170 inline const Color brown = Color::from_hex("#a52a2a");
171 inline const Color burlywood = Color::from_hex("#deb887");
172 inline const Color cadetblue = Color::from_hex("#5f9ea0");
173 inline const Color chartreuse = Color::from_hex("#7fff00");
174 inline const Color chocolate = Color::from_hex("#d2691e");
175 inline const Color coral = Color::from_hex("#ff7f50");
176 inline const Color cornflowerblue = Color::from_hex("#6495ed");
177 inline const Color cornsilk = Color::from_hex("#fff8dc");
178 inline const Color crimson = Color::from_hex("#dc143c");
179 inline const Color cyan = Color::from_hex("#00ffff");
180 inline const Color darkblue = Color::from_hex("#00008b");
181 inline const Color darkcyan = Color::from_hex("#008b8b");
182 inline const Color darkgoldenrod = Color::from_hex("#b8860b");
183 inline const Color darkgray = Color::from_hex("#a9a9a9");
184 inline const Color darkgreen = Color::from_hex("#006400");
185 inline const Color darkgrey = Color::from_hex("#a9a9a9");
186 inline const Color darkkhaki = Color::from_hex("#bdb76b");
187 inline const Color darkmagenta = Color::from_hex("#8b008b");
188 inline const Color darkolivegreen = Color::from_hex("#556b2f");
189 inline const Color darkorange = Color::from_hex("#ff8c00");
190 inline const Color darkorchid = Color::from_hex("#9932cc");
191 inline const Color darkred = Color::from_hex("#8b0000");
192 inline const Color darksalmon = Color::from_hex("#e9967a");
193 inline const Color darkseagreen = Color::from_hex("#8fbc8f");
194 inline const Color darkslateblue = Color::from_hex("#483d8b");
195 inline const Color darkslategray = Color::from_hex("#2f4f4f");
196 inline const Color darkslategrey = Color::from_hex("#2f4f4f");
197 inline const Color darkturquoise = Color::from_hex("#00ced1");
198 inline const Color darkviolet = Color::from_hex("#9400d3");
199 inline const Color deeppink = Color::from_hex("#ff1493");
200 inline const Color deepskyblue = Color::from_hex("#00bfff");
201 inline const Color dimgray = Color::from_hex("#696969");
202 inline const Color dimgrey = Color::from_hex("#696969");
203 inline const Color dodgerblue = Color::from_hex("#1e90ff");
204 inline const Color firebrick = Color::from_hex("#b22222");
205 inline const Color floralwhite = Color::from_hex("#fffaf0");
206 inline const Color forestgreen = Color::from_hex("#228b22");
207 inline const Color gainsboro = Color::from_hex("#dcdcdc");
208 inline const Color ghostwhite = Color::from_hex("#f8f8ff");
209 inline const Color gold = Color::from_hex("#ffd700");
210 inline const Color goldenrod = Color::from_hex("#daa520");
211 inline const Color greenyellow = Color::from_hex("#adff2f");
212 inline const Color grey = Color::from_hex("#808080");
213 inline const Color honeydew = Color::from_hex("#f0fff0");
214 inline const Color hotpink = Color::from_hex("#ff69b4");
215 inline const Color indianred = Color::from_hex("#cd5c5c");
216 inline const Color indigo = Color::from_hex("#4b0082");
217 inline const Color ivory = Color::from_hex("#fffff0");
218 inline const Color khaki = Color::from_hex("#f0e68c");
219 inline const Color lavender = Color::from_hex("#e6e6fa");
220 inline const Color lavenderblush = Color::from_hex("#fff0f5");
221 inline const Color lawngreen = Color::from_hex("#7cfc00");
222 inline const Color lemonchiffon = Color::from_hex("#fffacd");
223 inline const Color lightblue = Color::from_hex("#add8e6");
224 inline const Color lightcoral = Color::from_hex("#f08080");
225 inline const Color lightcyan = Color::from_hex("#e0ffff");
226 inline const Color lightgoldenrodyellow = Color::from_hex("#fafad2");
227 inline const Color lightgray = Color::from_hex("#d3d3d3");
228 inline const Color lightgreen = Color::from_hex("#90ee90");
229 inline const Color lightgrey = Color::from_hex("#d3d3d3");
230 inline const Color lightpink = Color::from_hex("#ffb6c1");
231 inline const Color lightsalmon = Color::from_hex("#ffa07a");
232 inline const Color lightseagreen = Color::from_hex("#20b2aa");
233 inline const Color lightskyblue = Color::from_hex("#87cefa");
234 inline const Color lightslategray = Color::from_hex("#778899");
235 inline const Color lightslategrey = Color::from_hex("#778899");
236 inline const Color lightsteelblue = Color::from_hex("#b0c4de");
237 inline const Color lightyellow = Color::from_hex("#ffffe0");
238 inline const Color limegreen = Color::from_hex("#32cd32");
239 inline const Color linen = Color::from_hex("#faf0e6");
240 inline const Color magenta = Color::from_hex("#ff00ff");
241 inline const Color mediumaquamarine = Color::from_hex("#66cdaa");
242 inline const Color mediumblue = Color::from_hex("#0000cd");
243 inline const Color mediumorchid = Color::from_hex("#ba55d3");
244 inline const Color mediumpurple = Color::from_hex("#9370db");
245 inline const Color mediumseagreen = Color::from_hex("#3cb371");
246 inline const Color mediumslateblue = Color::from_hex("#7b68ee");
247 inline const Color mediumspringgreen = Color::from_hex("#00fa9a");
248 inline const Color mediumturquoise = Color::from_hex("#48d1cc");
249 inline const Color mediumvioletred = Color::from_hex("#c71585");
250 inline const Color midnightblue = Color::from_hex("#191970");
251 inline const Color mintcream = Color::from_hex("#f5fffa");
252 inline const Color mistyrose = Color::from_hex("#ffe4e1");
253 inline const Color moccasin = Color::from_hex("#ffe4b5");
254 inline const Color navajowhite = Color::from_hex("#ffdead");
255 inline const Color oldlace = Color::from_hex("#fdf5e6");
256 inline const Color olivedrab = Color::from_hex("#6b8e23");
257 inline const Color orange = Color::from_hex("#ffa500");
258 inline const Color orangered = Color::from_hex("#ff4500");
259 inline const Color orchid = Color::from_hex("#da70d6");
260 inline const Color palegoldenrod = Color::from_hex("#eee8aa");
261 inline const Color palegreen = Color::from_hex("#98fb98");
262 inline const Color paleturquoise = Color::from_hex("#afeeee");
263 inline const Color palevioletred = Color::from_hex("#db7093");
264 inline const Color papayawhip = Color::from_hex("#ffefd5");
265 inline const Color peachpuff = Color::from_hex("#ffdab9");
266 inline const Color peru = Color::from_hex("#cd853f");
267 inline const Color pink = Color::from_hex("#ffc0cb");
268 inline const Color plum = Color::from_hex("#dda0dd");
269 inline const Color powderblue = Color::from_hex("#b0e0e6");
270 inline const Color rebeccapurple = Color::from_hex("#663399");
271 inline const Color rosybrown = Color::from_hex("#bc8f8f");
272 inline const Color royalblue = Color::from_hex("#4169e1");
273 inline const Color saddlebrown = Color::from_hex("#8b4513");
274 inline const Color salmon = Color::from_hex("#fa8072");
275 inline const Color sandybrown = Color::from_hex("#f4a460");
276 inline const Color seagreen = Color::from_hex("#2e8b57");
277 inline const Color seashell = Color::from_hex("#fff5ee");
278 inline const Color sienna = Color::from_hex("#a0522d");
279 inline const Color skyblue = Color::from_hex("#87ceeb");
280 inline const Color slateblue = Color::from_hex("#6a5acd");
281 inline const Color slategray = Color::from_hex("#708090");
282 inline const Color slategrey = Color::from_hex("#708090");
283 inline const Color snow = Color::from_hex("#fffafa");
284 inline const Color springgreen = Color::from_hex("#00ff7f");
285 inline const Color steelblue = Color::from_hex("#4682b4");
286 inline const Color tan = Color::from_hex("#d2b48c");
287 inline const Color thistle = Color::from_hex("#d8bfd8");
288 inline const Color tomato = Color::from_hex("#ff6347");
289 inline const Color turquoise = Color::from_hex("#40e0d0");
290 inline const Color violet = Color::from_hex("#ee82ee");
291 inline const Color wheat = Color::from_hex("#f5deb3");
292 inline const Color whitesmoke = Color::from_hex("#f5f5f5");
293 inline const Color yellowgreen = Color::from_hex("#9acd32");
294
295 // Special
296 inline const Color transparent = Color::from_hex("#00000000");
297} // namespace color
298
299} // namespace asw
300
301#endif // ASW_COLOR_H
const Color royalblue
Definition color.h:272
const Color slateblue
Definition color.h:280
const Color moccasin
Definition color.h:253
const Color seagreen
Definition color.h:276
const Color thistle
Definition color.h:287
const Color skyblue
Definition color.h:279
const Color azure
Definition color.h:165
const Color lightslategrey
Definition color.h:235
const Color mediumpurple
Definition color.h:244
const Color lightgray
Definition color.h:227
const Color aqua
Definition color.h:159
const Color hotpink
Definition color.h:214
const Color springgreen
Definition color.h:284
const Color white
Definition color.h:147
const Color darkcyan
Definition color.h:181
const Color plum
Definition color.h:268
const Color darkturquoise
Definition color.h:197
const Color lightgrey
Definition color.h:229
const Color deeppink
Definition color.h:199
const Color saddlebrown
Definition color.h:273
const Color blanchedalmond
Definition color.h:168
const Color indigo
Definition color.h:216
const Color lightseagreen
Definition color.h:232
const Color darkviolet
Definition color.h:198
const Color gray
Definition color.h:146
const Color mediumseagreen
Definition color.h:245
const Color mediumslateblue
Definition color.h:246
const Color brown
Definition color.h:170
const Color cornflowerblue
Definition color.h:176
const Color greenyellow
Definition color.h:211
const Color aquamarine
Definition color.h:164
const Color midnightblue
Definition color.h:250
const Color forestgreen
Definition color.h:206
const Color limegreen
Definition color.h:238
const Color snow
Definition color.h:283
const Color ghostwhite
Definition color.h:208
const Color sienna
Definition color.h:278
const Color firebrick
Definition color.h:204
const Color transparent
Definition color.h:296
const Color peachpuff
Definition color.h:265
const Color darkred
Definition color.h:191
const Color whitesmoke
Definition color.h:292
const Color darkgrey
Definition color.h:185
const Color coral
Definition color.h:175
const Color gainsboro
Definition color.h:207
const Color aliceblue
Definition color.h:162
const Color lightgoldenrodyellow
Definition color.h:226
const Color mintcream
Definition color.h:251
const Color lemonchiffon
Definition color.h:222
const Color lightsalmon
Definition color.h:231
const Color palevioletred
Definition color.h:263
const Color lawngreen
Definition color.h:221
const Color teal
Definition color.h:158
const Color rebeccapurple
Definition color.h:270
const Color burlywood
Definition color.h:171
const Color paleturquoise
Definition color.h:262
const Color orangered
Definition color.h:258
const Color cornsilk
Definition color.h:177
const Color mediumaquamarine
Definition color.h:241
const Color darkmagenta
Definition color.h:187
const Color orchid
Definition color.h:259
const Color darkslategrey
Definition color.h:196
const Color darkorange
Definition color.h:189
const Color salmon
Definition color.h:274
const Color ivory
Definition color.h:217
const Color lightgreen
Definition color.h:228
const Color honeydew
Definition color.h:213
const Color yellow
Definition color.h:155
const Color goldenrod
Definition color.h:210
const Color lightcyan
Definition color.h:225
const Color lightcoral
Definition color.h:224
const Color cyan
Definition color.h:179
const Color mediumblue
Definition color.h:242
const Color darkkhaki
Definition color.h:186
const Color tomato
Definition color.h:288
const Color rosybrown
Definition color.h:271
const Color indianred
Definition color.h:215
const Color navy
Definition color.h:156
const Color darkslateblue
Definition color.h:194
const Color silver
Definition color.h:145
const Color cadetblue
Definition color.h:172
const Color darkolivegreen
Definition color.h:188
const Color slategray
Definition color.h:281
const Color peru
Definition color.h:266
const Color fuchsia
Definition color.h:151
const Color darkseagreen
Definition color.h:193
const Color turquoise
Definition color.h:289
const Color lightpink
Definition color.h:230
const Color navajowhite
Definition color.h:254
const Color sandybrown
Definition color.h:275
const Color floralwhite
Definition color.h:205
const Color palegreen
Definition color.h:261
const Color antiquewhite
Definition color.h:163
const Color oldlace
Definition color.h:255
const Color darkorchid
Definition color.h:190
const Color green
Definition color.h:152
const Color lavender
Definition color.h:219
const Color lavenderblush
Definition color.h:220
const Color linen
Definition color.h:239
const Color bisque
Definition color.h:167
const Color crimson
Definition color.h:178
const Color dimgrey
Definition color.h:202
const Color magenta
Definition color.h:240
const Color khaki
Definition color.h:218
const Color violet
Definition color.h:290
const Color dimgray
Definition color.h:201
const Color lightslategray
Definition color.h:234
const Color mediumspringgreen
Definition color.h:247
const Color mediumorchid
Definition color.h:243
const Color chartreuse
Definition color.h:173
const Color mistyrose
Definition color.h:252
const Color orange
Definition color.h:257
const Color chocolate
Definition color.h:174
const Color tan
Definition color.h:286
const Color mediumvioletred
Definition color.h:249
const Color black
Definition color.h:144
const Color pink
Definition color.h:267
const Color purple
Definition color.h:150
const Color yellowgreen
Definition color.h:293
const Color olive
Definition color.h:154
const Color darksalmon
Definition color.h:192
const Color gold
Definition color.h:209
const Color lime
Definition color.h:153
const Color slategrey
Definition color.h:282
const Color grey
Definition color.h:212
const Color dodgerblue
Definition color.h:203
const Color olivedrab
Definition color.h:256
const Color darkslategray
Definition color.h:195
const Color steelblue
Definition color.h:285
const Color mediumturquoise
Definition color.h:248
const Color lightyellow
Definition color.h:237
const Color darkgray
Definition color.h:183
const Color powderblue
Definition color.h:269
const Color blueviolet
Definition color.h:169
const Color blue
Definition color.h:157
const Color maroon
Definition color.h:148
const Color deepskyblue
Definition color.h:200
const Color darkgreen
Definition color.h:184
const Color darkgoldenrod
Definition color.h:182
const Color darkblue
Definition color.h:180
const Color lightsteelblue
Definition color.h:236
const Color beige
Definition color.h:166
const Color palegoldenrod
Definition color.h:260
const Color wheat
Definition color.h:291
const Color lightskyblue
Definition color.h:233
const Color seashell
Definition color.h:277
const Color red
Definition color.h:149
const Color papayawhip
Definition color.h:264
const Color lightblue
Definition color.h:223
RGBA color struct with 8-bit channels.
Definition color.h:11
Color with_alpha(uint8_t alpha) const
Adjust the alpha of a color.
Definition color.h:135
Color blend(const Color &color) const
Blend two colors together using alpha blending.
Definition color.h:104
uint8_t b
Definition color.h:14
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:49
uint8_t r
Definition color.h:12
Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Construct a Color from RGBA values.
Definition color.h:33
Color invert() const
Invert a color.
Definition color.h:115
uint8_t g
Definition color.h:13
static Color from_hex(const std::string &hex)
From a hex string (e.g. "#RRGGBBAA") to a Color.
Definition color.h:60
Color grayscale() const
Convert a color to grayscale.
Definition color.h:124
uint8_t a
Definition color.h:15
Color()
Construct a default Color (black, fully opaque).
Definition color.h:18
Color darken(float percentage) const
Darken a color by a given percentage.
Definition color.h:93
Color lighten(float percentage) const
Lighten a color by a given percentage.
Definition color.h:82