1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include "color.hh"
#include <cstdio>
#include <iostream>
#include <map>
namespace {
struct ColorNames {
typedef std::map<std::string, Color> Map;
Map m;
ColorNames() {
m["none"] = Color(0.0, 0.0, 0.0, 0.0);
m["black"] = Color(0.0, 0.0, 0.0);
m["gray"] = Color(0.5, 0.5, 0.5);
m["silver"] = Color(0.75, 0.75, 0.75);
m["white"] = Color(1.0, 1.0, 1.0);
m["maroon"] = Color(0.5, 0.0, 0.0);
m["red"] = Color(1.0, 0.0, 0.0);
m["green"] = Color(0.0, 0.5, 0.0);
m["lime"] = Color(0.0, 1.0, 0.0);
m["navy"] = Color(0.0, 0.0, 0.5);
m["blue"] = Color(0.0, 0.0, 1.0);
m["purple"] = Color(0.5, 0.0, 0.5);
m["fuchsia"] = Color(1.0, 0.5, 1.0);
m["olive"] = Color(0.5, 0.5, 0.0);
m["yellow"] = Color(1.0, 1.0, 0.0);
m["teal"] = Color(0.0, 0.5, 0.5);
m["aqua"] = Color(0.0, 1.0, 1.0);
}
} colors;
}
Color::Color(std::string const& str) {
unsigned int r = 0, g = 0, b = 0;
if (str.size() == 7 && str[0] == '#' && sscanf(str.c_str() + 1, "%02x %02x %02x", &r, &g, &b) == 3) {
*this = Color(r / 255.0, g / 255.0, b / 255.0);
return;
}
ColorNames::Map::const_iterator it = colors.m.find(str);
if (it != colors.m.end()) { *this = it->second; return; }
std::cerr << "WARNING: Unknown color: " << str << " (using magenta to hilight)" << std::endl;
*this = Color(1.0, 0.0, 1.0);
}
|