From 6c9912c341d058dfffcdc3498f7ec352cf001bec Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 30 Nov 2023 10:22:40 +0100 Subject: [PATCH] LibGfx: Parse hex, rgb() and rgba() colors before named colors This avoids doing a bunch of unnecessary string comparison work in case we have something that can't be a named color anyway. --- Userland/Libraries/LibGfx/Color.cpp | 105 ++++++++++++++-------------- 1 file changed, 53 insertions(+), 52 deletions(-) diff --git a/Userland/Libraries/LibGfx/Color.cpp b/Userland/Libraries/LibGfx/Color.cpp index a2c70389b3..61c8ba37a8 100644 --- a/Userland/Libraries/LibGfx/Color.cpp +++ b/Userland/Libraries/LibGfx/Color.cpp @@ -267,11 +267,55 @@ Optional Color::from_string(StringView string) if (string.is_empty()) return {}; - if (string.equals_ignoring_ascii_case("transparent"sv)) - return Color::from_argb(0x00000000); + if (string[0] == '#') { + auto hex_nibble_to_u8 = [](char nibble) -> Optional { + if (!isxdigit(nibble)) + return {}; + if (nibble >= '0' && nibble <= '9') + return nibble - '0'; + return 10 + (tolower(nibble) - 'a'); + }; - if (auto const color = from_named_css_color_string(string); color.has_value()) - return color; + if (string.length() == 4) { + Optional r = hex_nibble_to_u8(string[1]); + Optional g = hex_nibble_to_u8(string[2]); + Optional b = hex_nibble_to_u8(string[3]); + if (!r.has_value() || !g.has_value() || !b.has_value()) + return {}; + return Color(r.value() * 17, g.value() * 17, b.value() * 17); + } + + if (string.length() == 5) { + Optional r = hex_nibble_to_u8(string[1]); + Optional g = hex_nibble_to_u8(string[2]); + Optional b = hex_nibble_to_u8(string[3]); + Optional a = hex_nibble_to_u8(string[4]); + if (!r.has_value() || !g.has_value() || !b.has_value() || !a.has_value()) + return {}; + return Color(r.value() * 17, g.value() * 17, b.value() * 17, a.value() * 17); + } + + if (string.length() != 7 && string.length() != 9) + return {}; + + auto to_hex = [&](char c1, char c2) -> Optional { + auto nib1 = hex_nibble_to_u8(c1); + auto nib2 = hex_nibble_to_u8(c2); + if (!nib1.has_value() || !nib2.has_value()) + return {}; + return nib1.value() << 4 | nib2.value(); + }; + + Optional r = to_hex(string[1], string[2]); + Optional g = to_hex(string[3], string[4]); + Optional b = to_hex(string[5], string[6]); + Optional a = string.length() == 9 ? to_hex(string[7], string[8]) : Optional(255); + + if (!r.has_value() || !g.has_value() || !b.has_value() || !a.has_value()) + return {}; + + return Color(r.value(), g.value(), b.value(), a.value()); + } if (string.starts_with("rgb("sv, CaseSensitivity::CaseInsensitive) && string.ends_with(')')) return parse_rgb_color(string); @@ -279,56 +323,13 @@ Optional Color::from_string(StringView string) if (string.starts_with("rgba("sv, CaseSensitivity::CaseInsensitive) && string.ends_with(')')) return parse_rgba_color(string); - if (string[0] != '#') - return {}; + if (string.equals_ignoring_ascii_case("transparent"sv)) + return Color::from_argb(0x00000000); - auto hex_nibble_to_u8 = [](char nibble) -> Optional { - if (!isxdigit(nibble)) - return {}; - if (nibble >= '0' && nibble <= '9') - return nibble - '0'; - return 10 + (tolower(nibble) - 'a'); - }; + if (auto const color = from_named_css_color_string(string); color.has_value()) + return color; - if (string.length() == 4) { - Optional r = hex_nibble_to_u8(string[1]); - Optional g = hex_nibble_to_u8(string[2]); - Optional b = hex_nibble_to_u8(string[3]); - if (!r.has_value() || !g.has_value() || !b.has_value()) - return {}; - return Color(r.value() * 17, g.value() * 17, b.value() * 17); - } - - if (string.length() == 5) { - Optional r = hex_nibble_to_u8(string[1]); - Optional g = hex_nibble_to_u8(string[2]); - Optional b = hex_nibble_to_u8(string[3]); - Optional a = hex_nibble_to_u8(string[4]); - if (!r.has_value() || !g.has_value() || !b.has_value() || !a.has_value()) - return {}; - return Color(r.value() * 17, g.value() * 17, b.value() * 17, a.value() * 17); - } - - if (string.length() != 7 && string.length() != 9) - return {}; - - auto to_hex = [&](char c1, char c2) -> Optional { - auto nib1 = hex_nibble_to_u8(c1); - auto nib2 = hex_nibble_to_u8(c2); - if (!nib1.has_value() || !nib2.has_value()) - return {}; - return nib1.value() << 4 | nib2.value(); - }; - - Optional r = to_hex(string[1], string[2]); - Optional g = to_hex(string[3], string[4]); - Optional b = to_hex(string[5], string[6]); - Optional a = string.length() == 9 ? to_hex(string[7], string[8]) : Optional(255); - - if (!r.has_value() || !g.has_value() || !b.has_value() || !a.has_value()) - return {}; - - return Color(r.value(), g.value(), b.value(), a.value()); + return {}; } Vector Color::shades(u32 steps, float max) const