|
29 | 29 | #include <sstream>
|
30 | 30 | #include <cmath>
|
31 | 31 | #include <algorithm>
|
32 |
| -#include <map> |
33 | 32 |
|
34 | 33 | using namespace CSSColorParser;
|
35 | 34 |
|
36 | 35 | // http://www.w3.org/TR/css3-color/
|
37 |
| -const std::map<std::string, Color> kCSSColorTable = { |
| 36 | +struct NamedColor { const char *const name; const Color color; }; |
| 37 | +const NamedColor namedColors[] = { |
38 | 38 | { "transparent", { 0, 0, 0, 0 } }, { "aliceblue", { 240, 248, 255, 1 } },
|
39 | 39 | { "antiquewhite", { 250, 235, 215, 1 } }, { "aqua", { 0, 255, 255, 1 } },
|
40 | 40 | { "aquamarine", { 127, 255, 212, 1 } }, { "azure", { 240, 255, 255, 1 } },
|
@@ -111,6 +111,8 @@ const std::map<std::string, Color> kCSSColorTable = {
|
111 | 111 | { "yellow", { 255, 255, 0, 1 } }, { "yellowgreen", { 154, 205, 50, 1 } }
|
112 | 112 | };
|
113 | 113 |
|
| 114 | +const size_t namedColorCount = sizeof (namedColors) / sizeof (NamedColor); |
| 115 | + |
114 | 116 |
|
115 | 117 | template <typename T>
|
116 | 118 | uint8_t clamp_css_byte(T i) { // Clamp to integer 0 .. 255.
|
@@ -187,10 +189,11 @@ Color CSSColorParser::parse(const std::string& css_str) {
|
187 | 189 | // Convert to lowercase.
|
188 | 190 | std::transform(str.begin(), str.end(), str.begin(), ::tolower);
|
189 | 191 |
|
190 |
| - // Color keywords (and transparent) lookup. |
191 |
| - auto it = kCSSColorTable.find(str); |
192 |
| - if (it != kCSSColorTable.end()) { |
193 |
| - return it->second; |
| 192 | + |
| 193 | + for (size_t i = 0; i < namedColorCount; i++) { |
| 194 | + if (str == namedColors[i].name) { |
| 195 | + return namedColors[i].color; |
| 196 | + } |
194 | 197 | }
|
195 | 198 |
|
196 | 199 | // #abc and #abc123 syntax.
|
|
0 commit comments