don't use std::map for checking color constants

This commit is contained in:
Konstantin Käfer
2014-10-30 16:43:16 -04:00
parent ae3ef12142
commit 8712beff80

View File

@@ -29,12 +29,12 @@
#include <sstream>
#include <cmath>
#include <algorithm>
#include <map>
using namespace CSSColorParser;
// http://www.w3.org/TR/css3-color/
const std::map<std::string, Color> kCSSColorTable = {
struct NamedColor { const char *const name; const Color color; };
const NamedColor namedColors[] = {
{ "transparent", { 0, 0, 0, 0 } }, { "aliceblue", { 240, 248, 255, 1 } },
{ "antiquewhite", { 250, 235, 215, 1 } }, { "aqua", { 0, 255, 255, 1 } },
{ "aquamarine", { 127, 255, 212, 1 } }, { "azure", { 240, 255, 255, 1 } },
@@ -111,6 +111,8 @@ const std::map<std::string, Color> kCSSColorTable = {
{ "yellow", { 255, 255, 0, 1 } }, { "yellowgreen", { 154, 205, 50, 1 } }
};
const size_t namedColorCount = sizeof (namedColors) / sizeof (NamedColor);
template <typename T>
uint8_t clamp_css_byte(T i) { // Clamp to integer 0 .. 255.
@@ -187,10 +189,11 @@ Color CSSColorParser::parse(const std::string& css_str) {
// Convert to lowercase.
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
// Color keywords (and transparent) lookup.
auto it = kCSSColorTable.find(str);
if (it != kCSSColorTable.end()) {
return it->second;
for (size_t i = 0; i < namedColorCount; i++) {
if (str == namedColors[i].name) {
return namedColors[i].color;
}
}
// #abc and #abc123 syntax.