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 {
11 struct Color {
12 uint8_t r, g, b, a;
13
15 Color() : r(0), g(0), b(0), a(255) {}
16
24 Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255)
25 : r(r), g(g), b(b), a(a) {}
26
31 SDL_Color toSDLColor() const { return SDL_Color{r, g, b, a}; }
32
41 static Color fromFloat(float r, float g, float b, float a = 1.0F) {
42 return {static_cast<uint8_t>(r * 255), static_cast<uint8_t>(g * 255),
43 static_cast<uint8_t>(b * 255), static_cast<uint8_t>(a * 255)};
44 }
45
49 static Color fromSDLColor(const SDL_Color& sdlColor) {
50 return {sdlColor.r, sdlColor.g, sdlColor.b, sdlColor.a};
51 }
52
58 static Color fromHex(const std::string& hex) {
59 if (hex.empty() || hex[0] != '#' ||
60 (hex.length() != 7 && hex.length() != 9)) {
61 return Color(); // Invalid format, return default color
62 }
63
64 uint8_t r = std::stoi(hex.substr(1, 2), nullptr, 16);
65 uint8_t g = std::stoi(hex.substr(3, 2), nullptr, 16);
66 uint8_t b = std::stoi(hex.substr(5, 2), nullptr, 16);
67 uint8_t a = 255; // Default alpha
68
69 if (hex.length() == 9) {
70 a = std::stoi(hex.substr(7, 2), nullptr, 16);
71 }
72
73 return {r, g, b, a};
74 }
75 };
76
78 namespace color {
79 // Basic color names
80 inline const Color black = Color::fromHex("#000000");
81 inline const Color silver = Color::fromHex("#c0c0c0");
82 inline const Color gray = Color::fromHex("#808080");
83 inline const Color white = Color::fromHex("#ffffff");
84 inline const Color maroon = Color::fromHex("#800000");
85 inline const Color red = Color::fromHex("#ff0000");
86 inline const Color purple = Color::fromHex("#800080");
87 inline const Color fuchsia = Color::fromHex("#ff00ff");
88 inline const Color green = Color::fromHex("#008000");
89 inline const Color lime = Color::fromHex("#00ff00");
90 inline const Color olive = Color::fromHex("#808000");
91 inline const Color yellow = Color::fromHex("#ffff00");
92 inline const Color navy = Color::fromHex("#000080");
93 inline const Color blue = Color::fromHex("#0000ff");
94 inline const Color teal = Color::fromHex("#008080");
95 inline const Color aqua = Color::fromHex("#00ffff");
96
97 // Extended color names
98 inline const Color aliceblue = Color::fromHex("#f0f8ff");
99 inline const Color antiquewhite = Color::fromHex("#faebd7");
100 inline const Color aquamarine = Color::fromHex("#7fffd4");
101 inline const Color azure = Color::fromHex("#f0ffff");
102 inline const Color beige = Color::fromHex("#f5f5dc");
103 inline const Color bisque = Color::fromHex("#ffe4c4");
104 inline const Color blanchedalmond = Color::fromHex("#ffebcd");
105 inline const Color blueviolet = Color::fromHex("#8a2be2");
106 inline const Color brown = Color::fromHex("#a52a2a");
107 inline const Color burlywood = Color::fromHex("#deb887");
108 inline const Color cadetblue = Color::fromHex("#5f9ea0");
109 inline const Color chartreuse = Color::fromHex("#7fff00");
110 inline const Color chocolate = Color::fromHex("#d2691e");
111 inline const Color coral = Color::fromHex("#ff7f50");
112 inline const Color cornflowerblue = Color::fromHex("#6495ed");
113 inline const Color cornsilk = Color::fromHex("#fff8dc");
114 inline const Color crimson = Color::fromHex("#dc143c");
115 inline const Color cyan = Color::fromHex("#00ffff");
116 inline const Color darkblue = Color::fromHex("#00008b");
117 inline const Color darkcyan = Color::fromHex("#008b8b");
118 inline const Color darkgoldenrod = Color::fromHex("#b8860b");
119 inline const Color darkgray = Color::fromHex("#a9a9a9");
120 inline const Color darkgreen = Color::fromHex("#006400");
121 inline const Color darkgrey = Color::fromHex("#a9a9a9");
122 inline const Color darkkhaki = Color::fromHex("#bdb76b");
123 inline const Color darkmagenta = Color::fromHex("#8b008b");
124 inline const Color darkolivegreen = Color::fromHex("#556b2f");
125 inline const Color darkorange = Color::fromHex("#ff8c00");
126 inline const Color darkorchid = Color::fromHex("#9932cc");
127 inline const Color darkred = Color::fromHex("#8b0000");
128 inline const Color darksalmon = Color::fromHex("#e9967a");
129 inline const Color darkseagreen = Color::fromHex("#8fbc8f");
130 inline const Color darkslateblue = Color::fromHex("#483d8b");
131 inline const Color darkslategray = Color::fromHex("#2f4f4f");
132 inline const Color darkslategrey = Color::fromHex("#2f4f4f");
133 inline const Color darkturquoise = Color::fromHex("#00ced1");
134 inline const Color darkviolet = Color::fromHex("#9400d3");
135 inline const Color deeppink = Color::fromHex("#ff1493");
136 inline const Color deepskyblue = Color::fromHex("#00bfff");
137 inline const Color dimgray = Color::fromHex("#696969");
138 inline const Color dimgrey = Color::fromHex("#696969");
139 inline const Color dodgerblue = Color::fromHex("#1e90ff");
140 inline const Color firebrick = Color::fromHex("#b22222");
141 inline const Color floralwhite = Color::fromHex("#fffaf0");
142 inline const Color forestgreen = Color::fromHex("#228b22");
143 inline const Color gainsboro = Color::fromHex("#dcdcdc");
144 inline const Color ghostwhite = Color::fromHex("#f8f8ff");
145 inline const Color gold = Color::fromHex("#ffd700");
146 inline const Color goldenrod = Color::fromHex("#daa520");
147 inline const Color greenyellow = Color::fromHex("#adff2f");
148 inline const Color grey = Color::fromHex("#808080");
149 inline const Color honeydew = Color::fromHex("#f0fff0");
150 inline const Color hotpink = Color::fromHex("#ff69b4");
151 inline const Color indianred = Color::fromHex("#cd5c5c");
152 inline const Color indigo = Color::fromHex("#4b0082");
153 inline const Color ivory = Color::fromHex("#fffff0");
154 inline const Color khaki = Color::fromHex("#f0e68c");
155 inline const Color lavender = Color::fromHex("#e6e6fa");
156 inline const Color lavenderblush = Color::fromHex("#fff0f5");
157 inline const Color lawngreen = Color::fromHex("#7cfc00");
158 inline const Color lemonchiffon = Color::fromHex("#fffacd");
159 inline const Color lightblue = Color::fromHex("#add8e6");
160 inline const Color lightcoral = Color::fromHex("#f08080");
161 inline const Color lightcyan = Color::fromHex("#e0ffff");
162 inline const Color lightgoldenrodyellow = Color::fromHex("#fafad2");
163 inline const Color lightgray = Color::fromHex("#d3d3d3");
164 inline const Color lightgreen = Color::fromHex("#90ee90");
165 inline const Color lightgrey = Color::fromHex("#d3d3d3");
166 inline const Color lightpink = Color::fromHex("#ffb6c1");
167 inline const Color lightsalmon = Color::fromHex("#ffa07a");
168 inline const Color lightseagreen = Color::fromHex("#20b2aa");
169 inline const Color lightskyblue = Color::fromHex("#87cefa");
170 inline const Color lightslategray = Color::fromHex("#778899");
171 inline const Color lightslategrey = Color::fromHex("#778899");
172 inline const Color lightsteelblue = Color::fromHex("#b0c4de");
173 inline const Color lightyellow = Color::fromHex("#ffffe0");
174 inline const Color limegreen = Color::fromHex("#32cd32");
175 inline const Color linen = Color::fromHex("#faf0e6");
176 inline const Color magenta = Color::fromHex("#ff00ff");
177 inline const Color mediumaquamarine = Color::fromHex("#66cdaa");
178 inline const Color mediumblue = Color::fromHex("#0000cd");
179 inline const Color mediumorchid = Color::fromHex("#ba55d3");
180 inline const Color mediumpurple = Color::fromHex("#9370db");
181 inline const Color mediumseagreen = Color::fromHex("#3cb371");
182 inline const Color mediumslateblue = Color::fromHex("#7b68ee");
183 inline const Color mediumspringgreen = Color::fromHex("#00fa9a");
184 inline const Color mediumturquoise = Color::fromHex("#48d1cc");
185 inline const Color mediumvioletred = Color::fromHex("#c71585");
186 inline const Color midnightblue = Color::fromHex("#191970");
187 inline const Color mintcream = Color::fromHex("#f5fffa");
188 inline const Color mistyrose = Color::fromHex("#ffe4e1");
189 inline const Color moccasin = Color::fromHex("#ffe4b5");
190 inline const Color navajowhite = Color::fromHex("#ffdead");
191 inline const Color oldlace = Color::fromHex("#fdf5e6");
192 inline const Color olivedrab = Color::fromHex("#6b8e23");
193 inline const Color orange = Color::fromHex("#ffa500");
194 inline const Color orangered = Color::fromHex("#ff4500");
195 inline const Color orchid = Color::fromHex("#da70d6");
196 inline const Color palegoldenrod = Color::fromHex("#eee8aa");
197 inline const Color palegreen = Color::fromHex("#98fb98");
198 inline const Color paleturquoise = Color::fromHex("#afeeee");
199 inline const Color palevioletred = Color::fromHex("#db7093");
200 inline const Color papayawhip = Color::fromHex("#ffefd5");
201 inline const Color peachpuff = Color::fromHex("#ffdab9");
202 inline const Color peru = Color::fromHex("#cd853f");
203 inline const Color pink = Color::fromHex("#ffc0cb");
204 inline const Color plum = Color::fromHex("#dda0dd");
205 inline const Color powderblue = Color::fromHex("#b0e0e6");
206 inline const Color rebeccapurple = Color::fromHex("#663399");
207 inline const Color rosybrown = Color::fromHex("#bc8f8f");
208 inline const Color royalblue = Color::fromHex("#4169e1");
209 inline const Color saddlebrown = Color::fromHex("#8b4513");
210 inline const Color salmon = Color::fromHex("#fa8072");
211 inline const Color sandybrown = Color::fromHex("#f4a460");
212 inline const Color seagreen = Color::fromHex("#2e8b57");
213 inline const Color seashell = Color::fromHex("#fff5ee");
214 inline const Color sienna = Color::fromHex("#a0522d");
215 inline const Color skyblue = Color::fromHex("#87ceeb");
216 inline const Color slateblue = Color::fromHex("#6a5acd");
217 inline const Color slategray = Color::fromHex("#708090");
218 inline const Color slategrey = Color::fromHex("#708090");
219 inline const Color snow = Color::fromHex("#fffafa");
220 inline const Color springgreen = Color::fromHex("#00ff7f");
221 inline const Color steelblue = Color::fromHex("#4682b4");
222 inline const Color tan = Color::fromHex("#d2b48c");
223 inline const Color thistle = Color::fromHex("#d8bfd8");
224 inline const Color tomato = Color::fromHex("#ff6347");
225 inline const Color turquoise = Color::fromHex("#40e0d0");
226 inline const Color violet = Color::fromHex("#ee82ee");
227 inline const Color wheat = Color::fromHex("#f5deb3");
228 inline const Color whitesmoke = Color::fromHex("#f5f5f5");
229 inline const Color yellowgreen = Color::fromHex("#9acd32");
230
231 // Special
232 inline const Color transparent = Color::fromHex("#00000000");
233 } // namespace color
234
235} // namespace asw
236
237#endif // ASW_COLOR_H
const Color royalblue
Definition color.h:208
const Color slateblue
Definition color.h:216
const Color moccasin
Definition color.h:189
const Color seagreen
Definition color.h:212
const Color thistle
Definition color.h:223
const Color skyblue
Definition color.h:215
const Color azure
Definition color.h:101
const Color lightslategrey
Definition color.h:171
const Color mediumpurple
Definition color.h:180
const Color lightgray
Definition color.h:163
const Color aqua
Definition color.h:95
const Color hotpink
Definition color.h:150
const Color springgreen
Definition color.h:220
const Color white
Definition color.h:83
const Color darkcyan
Definition color.h:117
const Color plum
Definition color.h:204
const Color darkturquoise
Definition color.h:133
const Color lightgrey
Definition color.h:165
const Color deeppink
Definition color.h:135
const Color saddlebrown
Definition color.h:209
const Color blanchedalmond
Definition color.h:104
const Color indigo
Definition color.h:152
const Color lightseagreen
Definition color.h:168
const Color darkviolet
Definition color.h:134
const Color gray
Definition color.h:82
const Color mediumseagreen
Definition color.h:181
const Color mediumslateblue
Definition color.h:182
const Color brown
Definition color.h:106
const Color cornflowerblue
Definition color.h:112
const Color greenyellow
Definition color.h:147
const Color aquamarine
Definition color.h:100
const Color midnightblue
Definition color.h:186
const Color forestgreen
Definition color.h:142
const Color limegreen
Definition color.h:174
const Color snow
Definition color.h:219
const Color ghostwhite
Definition color.h:144
const Color sienna
Definition color.h:214
const Color firebrick
Definition color.h:140
const Color transparent
Definition color.h:232
const Color peachpuff
Definition color.h:201
const Color darkred
Definition color.h:127
const Color whitesmoke
Definition color.h:228
const Color darkgrey
Definition color.h:121
const Color coral
Definition color.h:111
const Color gainsboro
Definition color.h:143
const Color aliceblue
Definition color.h:98
const Color lightgoldenrodyellow
Definition color.h:162
const Color mintcream
Definition color.h:187
const Color lemonchiffon
Definition color.h:158
const Color lightsalmon
Definition color.h:167
const Color palevioletred
Definition color.h:199
const Color lawngreen
Definition color.h:157
const Color teal
Definition color.h:94
const Color rebeccapurple
Definition color.h:206
const Color burlywood
Definition color.h:107
const Color paleturquoise
Definition color.h:198
const Color orangered
Definition color.h:194
const Color cornsilk
Definition color.h:113
const Color mediumaquamarine
Definition color.h:177
const Color darkmagenta
Definition color.h:123
const Color orchid
Definition color.h:195
const Color darkslategrey
Definition color.h:132
const Color darkorange
Definition color.h:125
const Color salmon
Definition color.h:210
const Color ivory
Definition color.h:153
const Color lightgreen
Definition color.h:164
const Color honeydew
Definition color.h:149
const Color yellow
Definition color.h:91
const Color goldenrod
Definition color.h:146
const Color lightcyan
Definition color.h:161
const Color lightcoral
Definition color.h:160
const Color cyan
Definition color.h:115
const Color mediumblue
Definition color.h:178
const Color darkkhaki
Definition color.h:122
const Color tomato
Definition color.h:224
const Color rosybrown
Definition color.h:207
const Color indianred
Definition color.h:151
const Color navy
Definition color.h:92
const Color darkslateblue
Definition color.h:130
const Color silver
Definition color.h:81
const Color cadetblue
Definition color.h:108
const Color darkolivegreen
Definition color.h:124
const Color slategray
Definition color.h:217
const Color peru
Definition color.h:202
const Color fuchsia
Definition color.h:87
const Color darkseagreen
Definition color.h:129
const Color turquoise
Definition color.h:225
const Color lightpink
Definition color.h:166
const Color navajowhite
Definition color.h:190
const Color sandybrown
Definition color.h:211
const Color floralwhite
Definition color.h:141
const Color palegreen
Definition color.h:197
const Color antiquewhite
Definition color.h:99
const Color oldlace
Definition color.h:191
const Color darkorchid
Definition color.h:126
const Color green
Definition color.h:88
const Color lavender
Definition color.h:155
const Color lavenderblush
Definition color.h:156
const Color linen
Definition color.h:175
const Color bisque
Definition color.h:103
const Color crimson
Definition color.h:114
const Color dimgrey
Definition color.h:138
const Color magenta
Definition color.h:176
const Color khaki
Definition color.h:154
const Color violet
Definition color.h:226
const Color dimgray
Definition color.h:137
const Color lightslategray
Definition color.h:170
const Color mediumspringgreen
Definition color.h:183
const Color mediumorchid
Definition color.h:179
const Color chartreuse
Definition color.h:109
const Color mistyrose
Definition color.h:188
const Color orange
Definition color.h:193
const Color chocolate
Definition color.h:110
const Color tan
Definition color.h:222
const Color mediumvioletred
Definition color.h:185
const Color black
Definition color.h:80
const Color pink
Definition color.h:203
const Color purple
Definition color.h:86
const Color yellowgreen
Definition color.h:229
const Color olive
Definition color.h:90
const Color darksalmon
Definition color.h:128
const Color gold
Definition color.h:145
const Color lime
Definition color.h:89
const Color slategrey
Definition color.h:218
const Color grey
Definition color.h:148
const Color dodgerblue
Definition color.h:139
const Color olivedrab
Definition color.h:192
const Color darkslategray
Definition color.h:131
const Color steelblue
Definition color.h:221
const Color mediumturquoise
Definition color.h:184
const Color lightyellow
Definition color.h:173
const Color darkgray
Definition color.h:119
const Color powderblue
Definition color.h:205
const Color blueviolet
Definition color.h:105
const Color blue
Definition color.h:93
const Color maroon
Definition color.h:84
const Color deepskyblue
Definition color.h:136
const Color darkgreen
Definition color.h:120
const Color darkgoldenrod
Definition color.h:118
const Color darkblue
Definition color.h:116
const Color lightsteelblue
Definition color.h:172
const Color beige
Definition color.h:102
const Color palegoldenrod
Definition color.h:196
const Color wheat
Definition color.h:227
const Color lightskyblue
Definition color.h:169
const Color seashell
Definition color.h:213
const Color red
Definition color.h:85
const Color papayawhip
Definition color.h:200
const Color lightblue
Definition color.h:159
RGBA color struct with 8-bit channels.
Definition color.h:11
static Color fromHex(const std::string &hex)
From a hex string (e.g. "#RRGGBBAA") to a Color.
Definition color.h:58
uint8_t b
Definition color.h:12
uint8_t r
Definition color.h:12
static Color fromSDLColor(const SDL_Color &sdlColor)
Convert from an SDL_Color to a Color.
Definition color.h:49
Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Construct a Color from RGBA values.
Definition color.h:24
SDL_Color toSDLColor() const
Convert to an SDL_Color.
Definition color.h:31
uint8_t g
Definition color.h:12
static Color fromFloat(float r, float g, float b, float a=1.0F)
From float RGBA values (0.0-1.0) to Color.
Definition color.h:41
uint8_t a
Definition color.h:12
Color()
Construct a default Color (black, fully opaque).
Definition color.h:15