1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 06:27:45 +00:00

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.
This commit is contained in:
Andreas Kling 2023-11-30 10:22:40 +01:00
parent 504aef470a
commit 6c9912c341

View file

@ -267,21 +267,7 @@ Optional<Color> Color::from_string(StringView string)
if (string.is_empty())
return {};
if (string.equals_ignoring_ascii_case("transparent"sv))
return Color::from_argb(0x00000000);
if (auto const color = from_named_css_color_string(string); color.has_value())
return color;
if (string.starts_with("rgb("sv, CaseSensitivity::CaseInsensitive) && string.ends_with(')'))
return parse_rgb_color(string);
if (string.starts_with("rgba("sv, CaseSensitivity::CaseInsensitive) && string.ends_with(')'))
return parse_rgba_color(string);
if (string[0] != '#')
return {};
if (string[0] == '#') {
auto hex_nibble_to_u8 = [](char nibble) -> Optional<u8> {
if (!isxdigit(nibble))
return {};
@ -329,6 +315,21 @@ Optional<Color> Color::from_string(StringView string)
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);
if (string.starts_with("rgba("sv, CaseSensitivity::CaseInsensitive) && string.ends_with(')'))
return parse_rgba_color(string);
if (string.equals_ignoring_ascii_case("transparent"sv))
return Color::from_argb(0x00000000);
if (auto const color = from_named_css_color_string(string); color.has_value())
return color;
return {};
}
Vector<Color> Color::shades(u32 steps, float max) const