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 <cstring>
#include <iomanip>
#include <map>
#include <sstream>
#include <stdexcept>
#include <string>
#include <unordered_map>
class ColorTable : public std::map<std::string, Color>
{
public:
ColorTable()
{
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);
static const std::unordered_map<std::string, const Color> colorTable{
{"white", Color(0xff, 0xff, 0xff)},
{"black", Color(0, 0, 0)},
{"gray", Color(0x7f, 0x7f, 0x7f)},
{"grey", Color(0x7f, 0x7f, 0x7f)},
table["red"] = Color(0xff, 0, 0);
table["green"] = Color(0, 0xff, 0);
table["blue"] = Color(0, 0, 0xff);
{"red", Color(0xff, 0, 0)},
{"green", Color(0, 0xff, 0)},
{"blue", Color(0, 0, 0xff)},
table["yellow"] = Color(0xff, 0xff, 0);
table["magenta"] = Color(0xff, 0, 0xff);
table["fuchsia"] = Color(0xff, 0, 0xff);
table["cyan"] = Color(0, 0xff, 0xff);
table["aqua"] = Color(0, 0xff, 0xff);
{"yellow", Color(0xff, 0xff, 0)},
{"magenta", Color(0xff, 0, 0xff)},
{"fuchsia", Color(0xff, 0, 0xff)},
{"cyan", Color(0, 0xff, 0xff)},
{"aqua", Color(0, 0xff, 0xff)},
table["orange"] = Color(0xff, 0x7f, 0);
table["chartreuse"] = Color(0x7f, 0xff, 0);
table["pink"] = Color(0xff, 0, 0x7f);
table["violet"] = Color(0x7f, 0, 0xff);
table["springgreen"] = Color(0, 0xff, 0x7f);
table["azure"] = Color(0, 0x7f, 0xff);
{"orange", Color(0xff, 0x7f, 0)},
{"chartreuse", Color(0x7f, 0xff, 0)},
{"pink", Color(0xff, 0, 0x7f)},
{"violet", Color(0x7f, 0, 0xff)},
{"springgreen", Color(0, 0xff, 0x7f)},
{"azure", Color(0, 0x7f, 0xff)},
table["brown"] = Color(0x7f, 0x3f, 0);
}
{"brown", Color(0x7f, 0x3f, 0)},
};
ColorTable colorTable;
// alpha:
// 0: don't expect/allow alpha
@ -57,11 +49,11 @@ Color::Color(const std::string &color, int alpha)
colormod = color.substr(pos);
}
if (basecolor[0] != '#') { // Color name
std::transform(basecolor.begin(), basecolor.end(), basecolor.begin(), ::tolower);
if (colorTable.count(basecolor) > 0) {
*this = colorTable[basecolor];
auto colorTableIter = colorTable.find(basecolor);
if (colorTableIter != colorTable.end()) {
*this = colorTableIter->second;
}
else {
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>
struct Color {
Color() = default;
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) {};
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);
constexpr Color() = default;
constexpr Color(uint32_t c) : r((c >> 16) & 0xff), g((c >> 8) & 0xff), b((c >> 0) & 0xff), a((c >> 24) & 0xff){};
constexpr 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, uint8_t a) : r(r), g(g), b(b), a(a){};
Color(const std::string &color, int alpha = 1);
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 ...
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 g{0};
uint8_t b{0};
@ -21,12 +21,12 @@ struct Color {
struct ColorEntry {
enum flags {
FlagNone = 0x00,
FlagIgnore = 0x01,
FlagAir = 0x02,
FlagNone = 0x00,
FlagIgnore = 0x01,
FlagAir = 0x02,
};
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); }
uint8_t r{0};
uint8_t g{0};
@ -36,9 +36,8 @@ struct ColorEntry {
uint8_t f{0};
};
struct HeightMapColor
{
HeightMapColor(int h0, Color c0, int h1, Color c1) : height{ h0, h1 }, color{ c0, c1 } {}
struct HeightMapColor {
HeightMapColor(int h0, Color c0, int h1, Color c1) : height{h0, h1}, color{c0, c1} {}
int height[2];
Color color[2];
};