diff options
author | Larry Ewing <lewing@xamarin.com> | 2018-05-16 06:24:44 +0300 |
---|---|---|
committer | Larry Ewing <lewing@microsoft.com> | 2018-07-16 22:05:49 +0300 |
commit | f5f245d7bbf156e87d117a158698533036e73331 (patch) | |
tree | f510494915a6e4ba6b9cb20c0e5149a501633b45 /Xamarin.PropertyEditing | |
parent | a7fb38a78a5af820c8f41c8e6db676b30df43cfb (diff) |
Add color parsing logic
Diffstat (limited to 'Xamarin.PropertyEditing')
-rw-r--r-- | Xamarin.PropertyEditing/Drawing/CommonColor.cs | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/Xamarin.PropertyEditing/Drawing/CommonColor.cs b/Xamarin.PropertyEditing/Drawing/CommonColor.cs index 08578d4..a4bdaa8 100644 --- a/Xamarin.PropertyEditing/Drawing/CommonColor.cs +++ b/Xamarin.PropertyEditing/Drawing/CommonColor.cs @@ -1,5 +1,7 @@ using System; using System.ComponentModel; +using System.Linq; +using System.Text.RegularExpressions; namespace Xamarin.PropertyEditing.Drawing { @@ -522,6 +524,35 @@ namespace Xamarin.PropertyEditing.Drawing + (left.g - right.g) * (left.g - right.g) + (left.b - right.b) * (left.b - right.b); + public static bool TryParse (string value, out CommonColor color) + { + if (Regex.IsMatch(value, @"^#(([A-Fa-f0-9]{2}){3}|[A-Fa-f0-9]{3}|[A-Fa-f0-9]{4}|([A-Fa-f0-9]{2}){4})$")) { + var hex = value.Substring (1); + switch (hex.Length) { + case 3: + hex = hex.Select (c => $"{c}{c}").Aggregate ((a, b) => (a + b)); + goto case 6; + case 6: + hex = hex + "ff"; + goto case 8; + case 4: + hex = hex.Select (c => $"{c}{c}").Aggregate ((a, b) => (a + b)); + goto case 8; + case 8: + var v = Convert.ToInt32 (hex, 16); + color = new CommonColor ( + r: (byte)(v >> 24), + g: (byte)(v >> 16), + b: (byte)(v >> 8), + a: (byte) v); + + return true; + } + } + color = CommonColor.Black; + return false; + } + public override int GetHashCode () { var hashCode = 466501756; @@ -536,7 +567,7 @@ namespace Xamarin.PropertyEditing.Drawing public override string ToString () { - return $"#{A:X2}{R:X2}{G:X2}{B:X2}"; + return $"#{R:X2}{G:X2}{B:X2}{A:X2}"; } } } |