Allow Color to be constexpr

master
Unknown 2018-05-21 11:55:19 +02:00
parent 8e58e84218
commit 373121c38d
2 changed files with 37 additions and 47 deletions

View File

@ -5,44 +5,36 @@
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <iomanip> #include <iomanip>
#include <map>
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <unordered_map>
class ColorTable : public std::map<std::string, Color> static const std::unordered_map<std::string, const Color> colorTable{
{ {"white", Color(0xff, 0xff, 0xff)},
public: {"black", Color(0, 0, 0)},
ColorTable() {"gray", Color(0x7f, 0x7f, 0x7f)},
{ {"grey", Color(0x7f, 0x7f, 0x7f)},
map<std::string, Color> &table = *this;
table["white"] = Color(0xff, 0xff, 0xff);
table["black"] = Color(0, 0, 0);
table["gray"] = Color(0x7f, 0x7f, 0x7f);
table["grey"] = Color(0x7f, 0x7f, 0x7f);
table["red"] = Color(0xff, 0, 0); {"red", Color(0xff, 0, 0)},
table["green"] = Color(0, 0xff, 0); {"green", Color(0, 0xff, 0)},
table["blue"] = Color(0, 0, 0xff); {"blue", Color(0, 0, 0xff)},
table["yellow"] = Color(0xff, 0xff, 0); {"yellow", Color(0xff, 0xff, 0)},
table["magenta"] = Color(0xff, 0, 0xff); {"magenta", Color(0xff, 0, 0xff)},
table["fuchsia"] = Color(0xff, 0, 0xff); {"fuchsia", Color(0xff, 0, 0xff)},
table["cyan"] = Color(0, 0xff, 0xff); {"cyan", Color(0, 0xff, 0xff)},
table["aqua"] = Color(0, 0xff, 0xff); {"aqua", Color(0, 0xff, 0xff)},
table["orange"] = Color(0xff, 0x7f, 0); {"orange", Color(0xff, 0x7f, 0)},
table["chartreuse"] = Color(0x7f, 0xff, 0); {"chartreuse", Color(0x7f, 0xff, 0)},
table["pink"] = Color(0xff, 0, 0x7f); {"pink", Color(0xff, 0, 0x7f)},
table["violet"] = Color(0x7f, 0, 0xff); {"violet", Color(0x7f, 0, 0xff)},
table["springgreen"] = Color(0, 0xff, 0x7f); {"springgreen", Color(0, 0xff, 0x7f)},
table["azure"] = Color(0, 0x7f, 0xff); {"azure", Color(0, 0x7f, 0xff)},
table["brown"] = Color(0x7f, 0x3f, 0); {"brown", Color(0x7f, 0x3f, 0)},
}
}; };
ColorTable colorTable;
// alpha: // alpha:
// 0: don't expect/allow alpha // 0: don't expect/allow alpha
@ -57,11 +49,11 @@ Color::Color(const std::string &color, int alpha)
colormod = color.substr(pos); colormod = color.substr(pos);
} }
if (basecolor[0] != '#') { // Color name if (basecolor[0] != '#') { // Color name
std::transform(basecolor.begin(), basecolor.end(), basecolor.begin(), ::tolower); std::transform(basecolor.begin(), basecolor.end(), basecolor.begin(), ::tolower);
if (colorTable.count(basecolor) > 0) { auto colorTableIter = colorTable.find(basecolor);
*this = colorTable[basecolor]; if (colorTableIter != colorTable.end()) {
*this = colorTableIter->second;
} }
else { else {
throw std::runtime_error(std::string("Symbolic color '") + color + "' not known, or color does not begin with #"); throw std::runtime_error(std::string("Symbolic color '") + color + "' not known, or color does not begin with #");
@ -219,4 +211,3 @@ Color::Color(const std::string &color, int alpha)
} }
} }
} }

View File

@ -4,15 +4,15 @@
#include <string> #include <string>
struct Color { struct Color {
Color() = default; constexpr Color() = default;
Color(uint32_t c): r((c >> 16) & 0xff), g((c >> 8) & 0xff), b((c >> 0) & 0xff ), a((c >> 24) & 0xff) {}; constexpr Color(uint32_t c) : r((c >> 16) & 0xff), g((c >> 8) & 0xff), b((c >> 0) & 0xff), a((c >> 24) & 0xff){};
Color(uint8_t r, uint8_t g, uint8_t b): r(r), g(g), b(b), a(0xff) {}; constexpr Color(uint8_t r, uint8_t g, uint8_t b) : r(r), g(g), b(b), a(0xff){};
Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a): r(r), g(g), b(b), a(a) {}; constexpr Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a) : r(r), g(g), b(b), a(a){};
Color(const std::string& color, int alpha = 1); Color(const std::string &color, int alpha = 1);
Color &operator=(const Color &c) = default; Color &operator=(const Color &c) = default;
unsigned to_uint() const { return (unsigned(a) << 24) + (unsigned(r) << 16) + (unsigned(g) << 8) + unsigned(b); } constexpr unsigned to_uint() const { return (unsigned(a) << 24) + (unsigned(r) << 16) + (unsigned(g) << 8) + unsigned(b); }
//libgd treats 127 as transparent, and 0 as opaque ... //libgd treats 127 as transparent, and 0 as opaque ...
int to_libgd() const { return (((0xff - int(a)) >> 1) << 24) + (int(r) << 16) + (int(g) << 8) + int(b); } constexpr int to_libgd() const { return (((0xff - int(a)) >> 1) << 24) + (int(r) << 16) + (int(g) << 8) + int(b); }
uint8_t r{0}; uint8_t r{0};
uint8_t g{0}; uint8_t g{0};
uint8_t b{0}; uint8_t b{0};
@ -21,12 +21,12 @@ struct Color {
struct ColorEntry { struct ColorEntry {
enum flags { enum flags {
FlagNone = 0x00, FlagNone = 0x00,
FlagIgnore = 0x01, FlagIgnore = 0x01,
FlagAir = 0x02, FlagAir = 0x02,
}; };
ColorEntry() = default; ColorEntry() = default;
ColorEntry(uint8_t r, uint8_t g, uint8_t b, uint8_t a, uint8_t t, uint8_t f): r(r), g(g), b(b), a(a), t(t), f(f) {}; ColorEntry(uint8_t r, uint8_t g, uint8_t b, uint8_t a, uint8_t t, uint8_t f) : r(r), g(g), b(b), a(a), t(t), f(f){};
inline Color to_color() const { return Color(r, g, b, a); } inline Color to_color() const { return Color(r, g, b, a); }
uint8_t r{0}; uint8_t r{0};
uint8_t g{0}; uint8_t g{0};
@ -36,9 +36,8 @@ struct ColorEntry {
uint8_t f{0}; uint8_t f{0};
}; };
struct HeightMapColor struct HeightMapColor {
{ HeightMapColor(int h0, Color c0, int h1, Color c1) : height{h0, h1}, color{c0, c1} {}
HeightMapColor(int h0, Color c0, int h1, Color c1) : height{ h0, h1 }, color{ c0, c1 } {}
int height[2]; int height[2];
Color color[2]; Color color[2];
}; };