Browse Source

LibGfx: Factor out #rrggbb Color parsing into its own helper function

Andrew Kaster 1 year ago
parent
commit
cf42bf2bca
1 changed files with 50 additions and 46 deletions
  1. 50 46
      Userland/Libraries/LibGfx/Color.cpp

+ 50 - 46
Userland/Libraries/LibGfx/Color.cpp

@@ -262,60 +262,64 @@ Optional<Color> Color::from_named_css_color_string(StringView string)
     return {};
     return {};
 }
 }
 
 
-Optional<Color> Color::from_string(StringView string)
+static Optional<Color> hex_string_to_color(StringView string)
 {
 {
-    if (string.is_empty())
-        return {};
+    auto hex_nibble_to_u8 = [](char nibble) -> Optional<u8> {
+        if (!isxdigit(nibble))
+            return {};
+        if (nibble >= '0' && nibble <= '9')
+            return nibble - '0';
+        return 10 + (tolower(nibble) - 'a');
+    };
 
 
-    if (string[0] == '#') {
-        auto hex_nibble_to_u8 = [](char nibble) -> Optional<u8> {
-            if (!isxdigit(nibble))
-                return {};
-            if (nibble >= '0' && nibble <= '9')
-                return nibble - '0';
-            return 10 + (tolower(nibble) - 'a');
-        };
-
-        if (string.length() == 4) {
-            Optional<u8> r = hex_nibble_to_u8(string[1]);
-            Optional<u8> g = hex_nibble_to_u8(string[2]);
-            Optional<u8> 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<u8> r = hex_nibble_to_u8(string[1]);
-            Optional<u8> g = hex_nibble_to_u8(string[2]);
-            Optional<u8> b = hex_nibble_to_u8(string[3]);
-            Optional<u8> 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)
+    if (string.length() == 4) {
+        Optional<u8> r = hex_nibble_to_u8(string[1]);
+        Optional<u8> g = hex_nibble_to_u8(string[2]);
+        Optional<u8> b = hex_nibble_to_u8(string[3]);
+        if (!r.has_value() || !g.has_value() || !b.has_value())
             return {};
             return {};
+        return Color(r.value() * 17, g.value() * 17, b.value() * 17);
+    }
 
 
-        auto to_hex = [&](char c1, char c2) -> Optional<u8> {
-            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();
-        };
+    if (string.length() == 5) {
+        Optional<u8> r = hex_nibble_to_u8(string[1]);
+        Optional<u8> g = hex_nibble_to_u8(string[2]);
+        Optional<u8> b = hex_nibble_to_u8(string[3]);
+        Optional<u8> 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);
+    }
 
 
-        Optional<u8> r = to_hex(string[1], string[2]);
-        Optional<u8> g = to_hex(string[3], string[4]);
-        Optional<u8> b = to_hex(string[5], string[6]);
-        Optional<u8> a = string.length() == 9 ? to_hex(string[7], string[8]) : Optional<u8>(255);
+    if (string.length() != 7 && string.length() != 9)
+        return {};
 
 
-        if (!r.has_value() || !g.has_value() || !b.has_value() || !a.has_value())
+    auto to_hex = [&](char c1, char c2) -> Optional<u8> {
+        auto nib1 = hex_nibble_to_u8(c1);
+        auto nib2 = hex_nibble_to_u8(c2);
+        if (!nib1.has_value() || !nib2.has_value())
             return {};
             return {};
+        return nib1.value() << 4 | nib2.value();
+    };
 
 
-        return Color(r.value(), g.value(), b.value(), a.value());
-    }
+    Optional<u8> r = to_hex(string[1], string[2]);
+    Optional<u8> g = to_hex(string[3], string[4]);
+    Optional<u8> b = to_hex(string[5], string[6]);
+    Optional<u8> a = string.length() == 9 ? to_hex(string[7], string[8]) : Optional<u8>(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());
+}
+
+Optional<Color> Color::from_string(StringView string)
+{
+    if (string.is_empty())
+        return {};
+
+    if (string[0] == '#')
+        return hex_string_to_color(string);
 
 
     if (string.starts_with("rgb("sv, CaseSensitivity::CaseInsensitive) && string.ends_with(')'))
     if (string.starts_with("rgb("sv, CaseSensitivity::CaseInsensitive) && string.ends_with(')'))
         return parse_rgb_color(string);
         return parse_rgb_color(string);