69 std::vector<double> &fallback)
const
71 std::istringstream ss(input);
72 return _parse(ss, type, cms, values, fallback);
87 std::vector<double> &fallback)
const
94 for (
auto &parser : iter->second) {
95 auto pos = ss.tellg();
99 name = parser->parseColor(ss, values, more);
102 if (more && ptype ==
"#") {
103 std::vector<double> icc_values;
104 if (
_parse(ss, type,
name, icc_values, fallback)) {
106 fallback = std::move(values);
107 values = std::move(icc_values);
113 if (!values.empty()) {
114 type = parser->getType();
130 auto const [it, inserted] =
_parsers.emplace(parser->
getPrefix(), std::vector<std::shared_ptr<Parser>>{});
131 it->second.emplace_back(parser);
145 if (!
parse(ss, output, more)) {
162bool HexParser::parse(std::istringstream &ss, std::vector<double> &output,
bool &more)
const
165 unsigned int size = 0;
168 ss >> std::hex >> hex;
170 size = (ss.tellg() == -1 ? ss.str().size() : (int)ss.tellg()) -
size;
173 for (
int p = (4 * (
size - 1)); p >= 0; p -= 4) {
174 auto val = ((hex & (0xf << p)) >> p);
175 output.emplace_back((val + (val << 4)) / 255.0);
177 }
else if (
size == 6 ||
size == 8) {
187 more = (ss.peek() ==
'i');
188 return !output.empty();
217 if (ss.peek() ==
'#') {
218 return {(char)ss.
get()};
220 auto pos = ss.tellg();
221 if (!std::getline(ss, token,
'(') || ss.eof()) {
226 if (token ==
"color") {
246 ss.imbue(std::locale(
"C"));
264 if (
c ==
'-' &&
result.empty()) {
266 }
else if (
c ==
'.') {
271 }
else if (
c >=
'0' &&
c <=
'9') {
279 value = std::stod(
result);
282 bool parsed = (bool)(ss >> value);
291 if (
c ==
'.' || (
c >=
'0' &&
c <=
'9')) {
294 while (ss && (
c = ss.get())) {
298 }
else if (
c == sep) {
303 if (p !=
' ' && p != sep && p !=
')') {
332 }
else if (unit ==
"deg") {
334 }
else if (unit ==
"turn") {
336 }
else if (!unit.empty()) {
337 std::cerr <<
"Unknown unit in css color parsing '" << unit.c_str() <<
"'\n";
342 output.emplace_back(value);
constexpr Coord get() const
bool parse(std::istringstream &ss, std::vector< double > &output) const override
Prase the given string stream as a CSS Color Module Level 4/5 string.
bool parse(std::istringstream &input, std::vector< double > &output, bool &more) const override
Parse either a hex code or an rgb() css string.
bool parse(std::istringstream &ss, std::vector< double > &output) const override
static bool css_number(std::istringstream &ss, double &value, std::string &unit, bool &end, char const sep=0x0)
Parse a CSS color number after the function name.
static bool append_css_value(std::istringstream &ss, std::vector< double > &output, bool &end, char const sep=0x0, double scale=1.0)
Parse a CSS color number and format it according to it's unit.
static std::string getCssPrefix(std::istringstream &ss)
Parse CSS color numbers after the function name.
std::string const getPrefix() const
virtual std::string parseColor(std::istringstream &ss, std::vector< double > &output, bool &more) const
Parse this specific color format into output values.
virtual bool parse(std::istringstream &ss, std::vector< double > &output) const
bool parse(std::string const &input, Space::Type &type, std::string &cms, std::vector< double > &values, std::vector< double > &fallback) const
Turn a string into a color data, used in Color object creation.
std::map< std::string, std::vector< std::shared_ptr< Parser > > > _parsers
void addParser(Parser *parser)
Add a prser to the list of parser objects used when parsing color strings.
bool _parse(std::istringstream &ss, Space::Type &type, std::string &cms, std::vector< double > &values, std::vector< double > &fallback) const
Internal recursive parser that scans through a string stream.
constexpr double SP_RGBA32_G_F(uint32_t v)
constexpr double SP_RGBA32_R_F(uint32_t v)
constexpr double SP_RGBA32_A_F(uint32_t v)
constexpr double SP_RGBA32_B_F(uint32_t v)
A set of useful color modifying functions which do not fit as generic methods on the color class itse...
void dot(Cairo::RefPtr< Cairo::Context > &cr, double x, double y)