Rewrite rgb color parsing
This commit is contained in:
parent
ee5283a9b1
commit
762f60f9fd
25
src/color.cc
25
src/color.cc
|
@ -32,16 +32,23 @@ Color str_to_color(StringView color)
|
||||||
if (it != std::end(color_names))
|
if (it != std::end(color_names))
|
||||||
return static_cast<Colors>(it - color_names);
|
return static_cast<Colors>(it - color_names);
|
||||||
|
|
||||||
static const Regex rgb_regex{"rgb:[0-9a-fA-F]{6}"};
|
auto hval = [&color](char c) -> int
|
||||||
if (regex_match(color.begin(), color.end(), rgb_regex))
|
|
||||||
{
|
{
|
||||||
unsigned l;
|
if (c >= 'A' and c <= 'F')
|
||||||
sscanf(color.zstr() + 4, "%x", &l);
|
return 10 + c - 'A';
|
||||||
return { (unsigned char)((l >> 16) & 0xFF),
|
else if (c >= 'a' and c <= 'f')
|
||||||
(unsigned char)((l >> 8) & 0xFF),
|
return 10 + c - 'a';
|
||||||
(unsigned char)(l & 0xFF) };
|
else if (c >= '0' and c <= '9')
|
||||||
}
|
return c - '0';
|
||||||
throw runtime_error("Unable to parse color '" + color + "'");
|
throw runtime_error(format("invalid digit '{}' in '{}'", c, color));
|
||||||
|
};
|
||||||
|
|
||||||
|
if (color.length() == 10 and color.substr(0_byte, 4_byte) == "rgb:")
|
||||||
|
return { (unsigned char)(hval(color[4]) * 16 + hval(color[5])),
|
||||||
|
(unsigned char)(hval(color[6]) * 16 + hval(color[7])),
|
||||||
|
(unsigned char)(hval(color[8]) * 16 + hval(color[9])) };
|
||||||
|
|
||||||
|
throw runtime_error(format("Unable to parse color '{}'", color));
|
||||||
return Colors::Default;
|
return Colors::Default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user