diff --git a/Source/Editor/Surface/Archetypes/Constants.cs b/Source/Editor/Surface/Archetypes/Constants.cs index 5987aeb2e..88399fa8d 100644 --- a/Source/Editor/Surface/Archetypes/Constants.cs +++ b/Source/Editor/Surface/Archetypes/Constants.cs @@ -555,24 +555,13 @@ namespace FlaxEditor.Surface.Archetypes }, TryParseText = (string filterText, out object[] data) => { - data = null; - if (filterText.StartsWith("#") && Color.TryParseHex(filterText, out var color)) + if (Color.TryParse(filterText, out var color)) { // Color constant from hex data = new object[] { color }; return true; } - if (filterText.Length > 2) - { - var fieldName = char.ToUpperInvariant(filterText[0]) + filterText.Substring(1).ToLowerInvariant(); - var field = typeof(Color).GetField(fieldName, BindingFlags.Public | BindingFlags.Static); - if (field != null && fieldName != "Zero") - { - // Color constant in-built - data = new object[] { field.GetValue(null) }; - return true; - } - } + data = null; return false; } }, diff --git a/Source/Engine/Core/Math/Color.cs b/Source/Engine/Core/Math/Color.cs index 77de16303..2e0b6bc03 100644 --- a/Source/Engine/Core/Math/Color.cs +++ b/Source/Engine/Core/Math/Color.cs @@ -2,6 +2,7 @@ using System; using System.ComponentModel; +using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -270,6 +271,46 @@ namespace FlaxEngine return new string(result); } + + /// + /// Creates from the text string (hex or color name). + /// + /// The color string (hex or color name). + /// The color. + public static Color Parse(string text) + { + if (TryParse(text, out Color value)) + return value; + throw new FormatException(); + } + + /// + /// Creates from the string (hex or color name). + /// + /// The color string (hex or color name). + /// Output value. + /// True if value has been parsed, otherwise false. + public static bool TryParse(string text, out Color value) + { + // Try hexadecimal + if (text.StartsWith("#") && TryParseHex(text, out value)) + return true; + + // Try named color + if (text.Length > 2) + { + var fieldName = char.ToUpperInvariant(text[0]) + text.Substring(1).ToLowerInvariant(); + var field = typeof(Color).GetField(fieldName, BindingFlags.Public | BindingFlags.Static); + if (field != null && fieldName != "Zero") + { + value = (Color)field.GetValue(null); + return true; + } + } + + value = Black; + return false; + } /// /// Creates from the hexadecimal string.