From d0eb35e5c30caae2fb54940f1b81b47079829e32 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 30 May 2020 16:06:59 +0200 Subject: [PATCH] LibGfx: Use a bit of constexpr in Color This avoids a bunch of strlen()'s when we're parsing web colors. --- Libraries/LibGfx/Color.cpp | 11 ++++++++--- Libraries/LibGfx/Color.h | 12 ++++++------ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/Libraries/LibGfx/Color.cpp b/Libraries/LibGfx/Color.cpp index e64c6f4096..6c335b6189 100644 --- a/Libraries/LibGfx/Color.cpp +++ b/Libraries/LibGfx/Color.cpp @@ -204,11 +204,16 @@ Optional 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::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); } diff --git a/Libraries/LibGfx/Color.h b/Libraries/LibGfx/Color.h index 106e6cb3ad..1af7ecfbbd 100644 --- a/Libraries/LibGfx/Color.h +++ b/Libraries/LibGfx/Color.h @@ -72,19 +72,19 @@ public: MidMagenta, }; - Color() {} + constexpr Color() { } Color(NamedColor); - Color(u8 r, u8 g, u8 b) + constexpr Color(u8 r, u8 g, u8 b) : m_value(0xff000000 | (r << 16) | (g << 8) | b) { } - Color(u8 r, u8 g, u8 b, u8 a) + constexpr Color(u8 r, u8 g, u8 b, u8 a) : m_value((a << 24) | (r << 16) | (g << 8) | b) { } - static Color from_rgb(unsigned rgb) { return Color(rgb | 0xff000000); } - static Color from_rgba(unsigned rgba) { return Color(rgba); } + static constexpr Color from_rgb(unsigned rgb) { return Color(rgb | 0xff000000); } + static constexpr Color from_rgba(unsigned rgba) { return Color(rgba); } u8 red() const { return (m_value >> 16) & 0xff; } u8 green() const { return (m_value >> 8) & 0xff; } @@ -267,7 +267,7 @@ public: } private: - explicit Color(RGBA32 rgba) + constexpr explicit Color(RGBA32 rgba) : m_value(rgba) { }