Browse Source

Simplify html_is_valid and allow it to work with 3 and 4 hex digits

Aaron Franke 5 years ago
parent
commit
a6ff389a55
2 changed files with 16 additions and 57 deletions
  1. 8 27
      core/color.cpp
  2. 8 30
      modules/mono/glue/GodotSharp/GodotSharp/Core/Color.cs

+ 8 - 27
core/color.cpp

@@ -296,7 +296,7 @@ Color Color::html(const String &p_rgba) {
 		return Color();
 	}
 	if (color[0] == '#') {
-		color = color.substr(1, color.length() - 1);
+		color = color.substr(1);
 	}
 
 	// If enabled, use 1 hex digit per channel instead of 2.
@@ -347,41 +347,22 @@ bool Color::html_is_valid(const String &p_color) {
 		return false;
 	}
 	if (color[0] == '#') {
-		color = color.substr(1, color.length() - 1);
+		color = color.substr(1);
 	}
 
-	bool alpha = false;
-
-	if (color.length() == 8) {
-		alpha = true;
-	} else if (color.length() == 6) {
-		alpha = false;
-	} else {
+	// Check if the amount of hex digits is valid.
+	int len = color.length();
+	if (!(len == 3 || len == 4 || len == 6 || len == 8)) {
 		return false;
 	}
 
-	if (alpha) {
-		int a = _parse_col8(color, 0);
-		if (a < 0) {
+	// Check if each hex digit is valid.
+	for (int i = 0; i < len; i++) {
+		if (_parse_col4(color, i) == -1) {
 			return false;
 		}
 	}
 
-	int from = alpha ? 2 : 0;
-
-	int r = _parse_col8(color, from + 0);
-	if (r < 0) {
-		return false;
-	}
-	int g = _parse_col8(color, from + 2);
-	if (g < 0) {
-		return false;
-	}
-	int b = _parse_col8(color, from + 4);
-	if (b < 0) {
-		return false;
-	}
-
 	return true;
 }
 

+ 8 - 30
modules/mono/glue/GodotSharp/GodotSharp/Core/Color.cs

@@ -827,46 +827,24 @@ namespace Godot
 
             if (color[0] == '#')
             {
-                color = color.Substring(1, color.Length - 1);
+                color = color.Substring(1);
             }
 
-            bool alpha;
-
-            switch (color.Length)
+            // Check if the amount of hex digits is valid.
+            int len = color.Length;
+            if (!(len == 3 || len == 4 || len == 6 || len == 8))
             {
-                case 8:
-                    alpha = true;
-                    break;
-                case 6:
-                    alpha = false;
-                    break;
-                default:
-                    return false;
+                return false;
             }
 
-            if (alpha)
-            {
-                if (ParseCol8(color, 0) < 0)
+            // Check if each hex digit is valid.
+            for (int i = 0; i < len; i++) {
+                if (ParseCol4(color, i) == -1)
                 {
                     return false;
                 }
             }
 
-            int from = alpha ? 2 : 0;
-
-            if (ParseCol8(color, from + 0) < 0)
-            {
-                return false;
-            }
-            if (ParseCol8(color, from + 2) < 0)
-            {
-                return false;
-            }
-            if (ParseCol8(color, from + 4) < 0)
-            {
-                return false;
-            }
-
             return true;
         }