1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 02:57:42 +00:00

LibGfx: Use a bit of constexpr in Color

This avoids a bunch of strlen()'s when we're parsing web colors.
This commit is contained in:
Andreas Kling 2020-05-30 16:06:59 +02:00
parent 4e80f22cc0
commit d0eb35e5c3
2 changed files with 14 additions and 9 deletions

View file

@ -204,11 +204,16 @@ Optional<Color> Color::from_string(const StringView& string)
return {};
struct ColorAndWebName {
constexpr ColorAndWebName(RGBA32 c, const char* n)
: color(c)
, name(n)
{
}
RGBA32 color;
const char* name;
StringView name;
};
const ColorAndWebName web_colors[] = {
constexpr ColorAndWebName web_colors[] = {
// CSS Level 1
{ 0x000000, "black" },
{ 0xc0c0c0, "silver" },
@ -365,7 +370,7 @@ Optional<Color> Color::from_string(const StringView& string)
{ 0x000000, nullptr }
};
for (size_t i = 0; web_colors[i].name; ++i) {
for (size_t i = 0; !web_colors[i].name.is_null(); ++i) {
if (string == web_colors[i].name)
return Color::from_rgb(web_colors[i].color);
}