Recognise some symbolic colors (red, green, blue, yellow, black, white, gr[ae]y, and more)
This commit is contained in:
parent
59bae48fc7
commit
1854ef34e6
48
Color.cpp
48
Color.cpp
@ -2,16 +2,60 @@
|
||||
#include <cctype>
|
||||
#include <stdexcept>
|
||||
#include <cstdlib>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <cctype>
|
||||
#include "Color.h"
|
||||
|
||||
class ColorTable : public std::map<std::string, Color>
|
||||
{
|
||||
public:
|
||||
ColorTable(void)
|
||||
{
|
||||
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);
|
||||
table["green"] = Color(0, 0xff, 0);
|
||||
table["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);
|
||||
|
||||
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);
|
||||
|
||||
table["brown"] = Color(0x7f, 0x3f, 0);
|
||||
}
|
||||
};
|
||||
ColorTable colorTable;
|
||||
|
||||
|
||||
// alpha:
|
||||
// 0: don't expect/allow alpha
|
||||
// 1: allow alpha; defaults to 255 (0xff)
|
||||
// -1: allow alpha but ignore - set to to 255 (0xff)
|
||||
Color::Color(const std::string &color, int alpha)
|
||||
Color::Color(std::string color, int alpha)
|
||||
{
|
||||
if (color[0] != '#') {
|
||||
throw std::runtime_error("Color does not begin with #");
|
||||
int l = color.length();
|
||||
for (int i = 0; i < l; i++)
|
||||
color[i] = tolower(color[i]);
|
||||
if (colorTable.count(color) > 0) {
|
||||
*this = colorTable[color];
|
||||
return;
|
||||
}
|
||||
throw std::runtime_error(std::string("Symbolic color '") + color + "' not known, or color does not begin with #");
|
||||
}
|
||||
if (std::string::npos != color.find_first_not_of("0123456789abcdefABCDEF",1)) {
|
||||
throw std::runtime_error("Color value has invalid digits (expected: [0-9a-zA-Z])");
|
||||
|
2
Color.h
2
Color.h
@ -9,7 +9,7 @@ struct Color {
|
||||
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 &s, int alpha = 1);
|
||||
Color(std::string s, int alpha = 1);
|
||||
Color &operator=(const Color &c);
|
||||
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 ...
|
||||
|
@ -108,6 +108,11 @@ void usage()
|
||||
"Color formats:\n"
|
||||
"\t'#000' or '#000000' (RGB)\n"
|
||||
"\t'#0000' or '#0000000' (ARGB - usable if an alpha value is allowed)\n"
|
||||
"\tSome symbolic color names:\n"
|
||||
"\t\twhite, black, gray, grey, red, green, blue,\n"
|
||||
"\t\tyellow, magenta, fuchsia, cyan, aqua,\n"
|
||||
"\t\torange, chartreuse, pink, violet, springgreen, azure\n"
|
||||
"\t\tbrown (= 50% orange)\n"
|
||||
"Geometry formats:\n"
|
||||
"\t<width>x<heigth>[+|-<xoffset>+|-<yoffset>] (dimensions and corner)\n"
|
||||
"\t<xoffset>,<yoffset>+<width>+<height> (corner and dimensions)\n"
|
||||
|
Loading…
x
Reference in New Issue
Block a user