mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 06:37:43 +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:
parent
504aef470a
commit
6c9912c341
1 changed files with 53 additions and 52 deletions
|
@ -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 {};
|
||||
|
@ -331,6 +317,21 @@ Optional<Color> Color::from_string(StringView string)
|
|||
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
|
||||
{
|
||||
float shade = 1.f;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue