mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 04:07:35 +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())
|
if (string.is_empty())
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
if (string.equals_ignoring_ascii_case("transparent"sv))
|
if (string[0] == '#') {
|
||||||
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 {};
|
|
||||||
|
|
||||||
auto hex_nibble_to_u8 = [](char nibble) -> Optional<u8> {
|
auto hex_nibble_to_u8 = [](char nibble) -> Optional<u8> {
|
||||||
if (!isxdigit(nibble))
|
if (!isxdigit(nibble))
|
||||||
return {};
|
return {};
|
||||||
|
@ -329,6 +315,21 @@ Optional<Color> Color::from_string(StringView string)
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
return Color(r.value(), g.value(), b.value(), a.value());
|
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
|
Vector<Color> Color::shades(u32 steps, float max) const
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue