diff --git a/Libraries/LibGfx/Color.cpp b/Libraries/LibGfx/Color.cpp index 1e6d888f95..80de68c496 100644 --- a/Libraries/LibGfx/Color.cpp +++ b/Libraries/LibGfx/Color.cpp @@ -34,6 +34,7 @@ #include #include #include +#include namespace Gfx { @@ -159,6 +160,43 @@ static Optional parse_rgb_color(const StringView& string) return Color(r, g, b); } +static Optional parse_rgba_color(const StringView& string) +{ + ASSERT(string.starts_with("rgba(")); + ASSERT(string.ends_with(")")); + + auto substring = string.substring_view(5, string.length() - 6); + auto parts = substring.split_view(','); + + if (parts.size() != 4) + return {}; + + bool ok; + auto r = parts[0].to_int(ok); + if (!ok) + return {}; + auto g = parts[1].to_int(ok); + if (!ok) + return {}; + auto b = parts[2].to_int(ok); + if (!ok) + return {}; + + double alpha = strtod(String(parts[3]).characters(), nullptr); + int a = alpha * 255; + + if (r < 0 || r > 255) + return {}; + if (g < 0 || g > 255) + return {}; + if (b < 0 || b > 255) + return {}; + if (a < 0 || a > 255) + return {}; + + return Color(r, g, b, a); +} + Optional Color::from_string(const StringView& string) { if (string.is_empty()) @@ -334,6 +372,9 @@ Optional Color::from_string(const StringView& string) if (string.starts_with("rgb(") && string.ends_with(")")) return parse_rgb_color(string); + if (string.starts_with("rgba(") && string.ends_with(")")) + return parse_rgba_color(string); + if (string[0] != '#') return {};