diff options
author | Eric Maupin <me@ermau.com> | 2018-09-24 17:27:32 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-24 17:27:32 +0300 |
commit | 770449b1c6253585a4e8237c43f9011da5b42a09 (patch) | |
tree | 44e01f931202d21ca1ddf2d380fe27c7775f1559 | |
parent | 3a99673a4082de39bedc5018857d5f52fea66e1a (diff) | |
parent | 51e755eded6517f1f2423b02ebeb0d3bfa1d7809 (diff) |
Merge pull request #411 from xamarin/bleroy-common-color-toargb
Add `CommonColor.ToArgbHex`
-rw-r--r-- | Xamarin.PropertyEditing.Tests/CommonColorTests.cs | 38 | ||||
-rw-r--r-- | Xamarin.PropertyEditing/Drawing/CommonColor.cs | 16 |
2 files changed, 41 insertions, 13 deletions
diff --git a/Xamarin.PropertyEditing.Tests/CommonColorTests.cs b/Xamarin.PropertyEditing.Tests/CommonColorTests.cs index 3038f7d..5ef524a 100644 --- a/Xamarin.PropertyEditing.Tests/CommonColorTests.cs +++ b/Xamarin.PropertyEditing.Tests/CommonColorTests.cs @@ -182,10 +182,42 @@ namespace Xamarin.PropertyEditing.Tests } [Test] - public void ColorToString() + public void ColorToRgba() { - var color = new CommonColor (0x34, 0x56, 0x78, 0x12); - Assert.AreEqual ("#12345678", color.ToString ()); + Assert.AreEqual ("#12345678", new CommonColor (0x12, 0x34, 0x56, 0x78).ToRgbaHex ()); + } + + [Test] + public void ColorToArgb () + { + Assert.AreEqual ("#12345678", new CommonColor (0x34, 0x56, 0x78, 0x12).ToArgbHex ()); + Assert.AreEqual ("#123456", new CommonColor (0x12, 0x34, 0x56).ToArgbHex ()); + } + + [Test] + public void ColorToString () + { + Assert.AreEqual ("#12345678", new CommonColor (0x34, 0x56, 0x78, 0x12).ToString ()); + } + + [Test] + public void ColorParseArgb () + { + CommonColor parsed; + Assert.IsTrue (CommonColor.TryParseArgbHex ("#123", out parsed)); + Assert.AreEqual (new CommonColor (0x11, 0x22, 0x33), parsed); + + Assert.IsTrue (CommonColor.TryParseArgbHex ("#123456", out parsed)); + Assert.AreEqual (new CommonColor (0x12, 0x34, 0x56), parsed); + + Assert.IsTrue (CommonColor.TryParseArgbHex ("#1234", out parsed)); + Assert.AreEqual (new CommonColor (0x22, 0x33, 0x44, 0x11), parsed); + + Assert.IsTrue (CommonColor.TryParseArgbHex ("#12345678", out parsed)); + Assert.AreEqual (new CommonColor (0x34, 0x56, 0x78, 0x12), parsed); + + Assert.IsFalse (CommonColor.TryParseArgbHex ("not a color", out parsed)); + Assert.AreEqual (CommonColor.Black, parsed); } #pragma warning disable CS0659 // Type overrides Object.Equals(object o) but does not override Object.GetHashCode() diff --git a/Xamarin.PropertyEditing/Drawing/CommonColor.cs b/Xamarin.PropertyEditing/Drawing/CommonColor.cs index 915dddd..485c2bf 100644 --- a/Xamarin.PropertyEditing/Drawing/CommonColor.cs +++ b/Xamarin.PropertyEditing/Drawing/CommonColor.cs @@ -366,10 +366,9 @@ namespace Xamarin.PropertyEditing.Drawing return delta == 0 ? highest : (component - lowest) * 255 / delta; } - public string ToRgbaHex () - { - return $"#{R:X2}{G:X2}{B:X2}{A:X2}"; - } + public string ToRgbaHex () => $"#{R:X2}{G:X2}{B:X2}{A:X2}"; + + public string ToArgbHex () => (A == 255) ? $"#{R:X2}{G:X2}{B:X2}" : $"#{A:X2}{R:X2}{G:X2}{B:X2}"; public static bool TryParseArgbHex (string value, out CommonColor color) { @@ -377,8 +376,8 @@ namespace Xamarin.PropertyEditing.Drawing var hex = value.Substring (1); switch (hex.Length) { case 3: - hex = $"{hex[0]}{hex[0]}{hex[1]}{hex[1]}{hex[2]}{hex[2]}"; - goto case 6; + hex = $"FF{hex[0]}{hex[0]}{hex[1]}{hex[1]}{hex[2]}{hex[2]}"; + goto case 8; case 6: hex = "FF" + hex; goto case 8; @@ -576,9 +575,6 @@ namespace Xamarin.PropertyEditing.Drawing return hashCode; } - public override string ToString () - { - return (A == 255) ? $"#{R:X2}{G:X2}{B:X2}" : $"#{A:X2}{R:X2}{G:X2}{B:X2}"; - } + public override string ToString () => ToArgbHex (); } } |