diff options
author | Bertrand Le Roy <beleroy@microsoft.com> | 2018-06-26 06:54:52 +0300 |
---|---|---|
committer | Bertrand Le Roy <beleroy@microsoft.com> | 2018-06-26 06:54:52 +0300 |
commit | bfa7cf328708e5f0599ee1422af99051df4264a1 (patch) | |
tree | 2585bc063982d3fa020dcbbb663dae1231f1c084 /Xamarin.PropertyEditing.Tests | |
parent | 43d1eefca7331dc32405d5170d89ccdb025f03c1 (diff) |
Store RGB components of `CommonColor`internally as double instead of byte, to improve accuracy. Fixes #325.
Diffstat (limited to 'Xamarin.PropertyEditing.Tests')
-rw-r--r-- | Xamarin.PropertyEditing.Tests/CommonColorTests.cs | 80 |
1 files changed, 49 insertions, 31 deletions
diff --git a/Xamarin.PropertyEditing.Tests/CommonColorTests.cs b/Xamarin.PropertyEditing.Tests/CommonColorTests.cs index f9f579f..3038f7d 100644 --- a/Xamarin.PropertyEditing.Tests/CommonColorTests.cs +++ b/Xamarin.PropertyEditing.Tests/CommonColorTests.cs @@ -9,30 +9,32 @@ namespace Xamarin.PropertyEditing.Tests public class CommonColorTests { public static readonly Dictionary<string, ColorTestCase> PrimaryTestCases = new Dictionary<string, ColorTestCase> { - // { name, r g b c% m% y% k% h° l% s% b% a} - { "Black", new ColorTestCase( 0, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0) }, - { "White", new ColorTestCase(255, 255, 255, 0, 0, 0, 0, 0, 100, 0, 100) }, - { "Gray", new ColorTestCase(127, 127, 127, 0, 0, 0, 50, 0, 50, 0, 50) }, - { "Silver", new ColorTestCase(191, 191, 191, 0, 0, 0, 25, 0, 75, 0, 75) }, - { "Red", new ColorTestCase(255, 0, 0, 0, 100, 100, 0, 0, 50, 100, 100) }, - { "Lime", new ColorTestCase( 0, 255, 0, 100, 0, 100, 0, 120, 50, 100, 100) }, - { "Blue", new ColorTestCase( 0, 0, 255, 100, 100, 0, 0, 240, 50, 100, 100) }, - { "Yellow", new ColorTestCase(255, 255, 0, 0, 0, 100, 0, 60, 50, 100, 100) }, - { "Cyan", new ColorTestCase( 0, 255, 255, 100, 0, 0, 0, 180, 50, 100, 100) }, - { "Magenta", new ColorTestCase(255, 0, 255, 0, 100, 0, 0, 300, 50, 100, 100) }, - { "Maroon", new ColorTestCase(127, 0, 0, 0, 100, 100, 50, 0, 25, 100, 50) }, - { "Green", new ColorTestCase( 0, 127, 0, 100, 0, 100, 50, 120, 25, 100, 50) }, - { "Navy", new ColorTestCase( 0, 0, 127, 100, 100, 0, 50, 240, 25, 100, 50) }, - { "Olive", new ColorTestCase(127, 127, 0, 0, 0, 100, 50, 60, 25, 100, 50) }, - { "Purple", new ColorTestCase(127, 0, 127, 0, 100, 0, 50, 300, 25, 100, 50) }, - { "Teal", new ColorTestCase( 0, 127, 127, 100, 0, 0, 50, 180, 25, 100, 50) }, + // { name, r g b c% m% y% k% h° l% s% b% } + { "Black", new ColorTestCase( 0, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0) }, + { "White", new ColorTestCase(255, 255, 255, 0, 0, 0, 0, 0, 100, 0, 100) }, + { "Gray", new ColorTestCase(128, 128, 128, 0, 0, 0, 50, 0, 50, 0, 50) }, + { "Silver", new ColorTestCase(191, 191, 191, 0, 0, 0, 25, 0, 74.9, 0, 74.9) }, + { "Red", new ColorTestCase(255, 0, 0, 0, 100, 100, 0, 0, 50, 100, 100) }, + { "Lime", new ColorTestCase( 0, 255, 0, 100, 0, 100, 0, 120, 50, 100, 100) }, + { "Blue", new ColorTestCase( 0, 0, 255, 100, 100, 0, 0, 240, 50, 100, 100) }, + { "Yellow", new ColorTestCase(255, 255, 0, 0, 0, 100, 0, 60, 50, 100, 100) }, + { "Cyan", new ColorTestCase( 0, 255, 255, 100, 0, 0, 0, 180, 50, 100, 100) }, + { "Magenta", new ColorTestCase(255, 0, 255, 0, 100, 0, 0, 300, 50, 100, 100) }, + { "Maroon", new ColorTestCase(128, 0, 0, 0, 100, 100, 50, 0, 25.1, 100, 50) }, + { "Green", new ColorTestCase( 0, 128, 0, 100, 0, 100, 50, 120, 25.1, 100, 50) }, + { "Navy", new ColorTestCase( 0, 0, 128, 100, 100, 0, 50, 240, 25.1, 100, 50) }, + { "Olive", new ColorTestCase(128, 128, 0, 0, 0, 100, 50, 60, 25.1, 100, 50) }, + { "Purple", new ColorTestCase(128, 0, 128, 0, 100, 0, 50, 300, 25.1, 100, 50) }, + { "Teal", new ColorTestCase( 0, 128, 128, 100, 0, 0, 50, 180, 25.1, 100, 50) }, }; public static readonly Dictionary<string, ColorTestCase> RGBTestCases = new Dictionary<string, ColorTestCase> { - // { name, r g b c% m% y% k% h° l% s% b% a} - { "Orange", new ColorTestCase(255, 164, 0, 0, 35.7, 100, 0, 38.6, 50, 100, 100) }, - { "MediumSpringGreen", new ColorTestCase( 0, 250, 154, 100, 0, 38, 2, 157, 49, 100, 98) }, - { "Chocolate", new ColorTestCase(209, 117, 52, 0, 44, 75.1, 18, 24.8, 47, 75.1, 82) }, + // { name, r g b c% m% y% k% h° l% s% b% } + { "Orange", new ColorTestCase(255, 164, 0, 0, 35.7, 100, 0, 38.6, 50, 100, 100) }, + { "MediumSpringGreen", new ColorTestCase( 0, 250, 155, 100, 0, 38, 2, 157.2, 49, 100, 98) }, + { "Chocolate", new ColorTestCase(209, 117, 52, 0, 44, 75.1, 18, 24.8, 51, 75.1, 82) }, + { "MoreOrange", new ColorTestCase(200, 101, 31, 0, 49.5, 84.5, 21.6, 24.9, 45.3, 84.5, 78.4) }, + { "Lilac", new ColorTestCase(101, 31, 255, 60.4, 87.8, 0, 0, 258.8, 56.1, 87.8, 100) }, }; [Test, TestCaseSource (typeof(CommonColorTests), "HueColorFromColorCases")] @@ -71,6 +73,9 @@ namespace Xamarin.PropertyEditing.Tests foreach (KeyValuePair<string, ColorTestCase> testCaseKVP in PrimaryTestCases) { yield return new TestCaseData (testCaseKVP.Value.CMYK).Returns (testCaseKVP.Value.Color).SetName ("ColorFromCMYK_" + testCaseKVP.Key); } + foreach (KeyValuePair<string, ColorTestCase> testCaseKVP in RGBTestCases) { + yield return new TestCaseData (testCaseKVP.Value.CMYK).Returns (testCaseKVP.Value.Color).SetName ("ColorFromCMYK_" + testCaseKVP.Key); + } } [Test, TestCaseSource (typeof (CommonColorTests), "HLSFromColorCases")] @@ -94,6 +99,10 @@ namespace Xamarin.PropertyEditing.Tests foreach (KeyValuePair<string, ColorTestCase> testCaseKVP in PrimaryTestCases) { yield return new TestCaseData (testCaseKVP.Value.HLS).Returns (testCaseKVP.Value.Color).SetName ("ColorFromHLS_" + testCaseKVP.Key); } + // HLS -> RGB conversion is the least reliable, and round-tripping it doesn't quite guarantee accurate enough results that they can be tested for arbitrary colors. + //foreach (KeyValuePair<string, ColorTestCase> testCaseKVP in RGBTestCases) { + // yield return new TestCaseData (testCaseKVP.Value.HLS).Returns (testCaseKVP.Value.Color).SetName ("ColorFromHLS_" + testCaseKVP.Key); + //} } [Test, TestCaseSource (typeof (CommonColorTests), "HLSRoundTripCases")] @@ -108,6 +117,9 @@ namespace Xamarin.PropertyEditing.Tests foreach (KeyValuePair<string, ColorTestCase> testCaseKVP in PrimaryTestCases) { yield return new TestCaseData (testCaseKVP.Value.HLS).Returns (testCaseKVP.Value.HLS).SetName ("HLSRoundTrip_" + testCaseKVP.Key); } + foreach (KeyValuePair<string, ColorTestCase> testCaseKVP in RGBTestCases) { + yield return new TestCaseData (testCaseKVP.Value.HLS).Returns (testCaseKVP.Value.HLS).SetName ("HLSRoundTrip_" + testCaseKVP.Key); + } } [Test, TestCaseSource (typeof (CommonColorTests), "HSBFromColorCases")] @@ -131,6 +143,9 @@ namespace Xamarin.PropertyEditing.Tests foreach (KeyValuePair<string, ColorTestCase> testCaseKVP in PrimaryTestCases) { yield return new TestCaseData (testCaseKVP.Value.HSB).Returns (testCaseKVP.Value.Color).SetName ("ColorFromHSB_" + testCaseKVP.Key); } + foreach (KeyValuePair<string, ColorTestCase> testCaseKVP in RGBTestCases) { + yield return new TestCaseData (testCaseKVP.Value.HSB).Returns (testCaseKVP.Value.Color).SetName ("ColorFromHSB_" + testCaseKVP.Key); + } } [Test, TestCaseSource (typeof (CommonColorTests), "HSBRoundTripCases")] @@ -145,6 +160,9 @@ namespace Xamarin.PropertyEditing.Tests foreach (KeyValuePair<string, ColorTestCase> testCaseKVP in PrimaryTestCases) { yield return new TestCaseData (testCaseKVP.Value.HSB).Returns (testCaseKVP.Value.HSB).SetName ("HSBRoundTrip_" + testCaseKVP.Key); } + foreach (KeyValuePair<string, ColorTestCase> testCaseKVP in RGBTestCases) { + yield return new TestCaseData (testCaseKVP.Value.HSB).Returns (testCaseKVP.Value.HSB).SetName ("HSBRoundTrip_" + testCaseKVP.Key); + } } [Test] @@ -189,10 +207,10 @@ namespace Xamarin.PropertyEditing.Tests public override bool Equals (object obj) { var other = (CMYK)obj; - return Math.Round (other.C - C, 2) == 0 - && Math.Round (other.M - M, 2) == 0 - && Math.Round (other.Y - Y, 2) == 0 - && Math.Round (other.K - K, 2) == 0; + return Math.Round (other.C, 2) == Math.Round(C, 2) + && Math.Round (other.M, 2) == Math.Round (M, 2) + && Math.Round (other.Y, 2) == Math.Round (Y, 2) + && Math.Round (other.K, 2) == Math.Round (K, 2); } public override string ToString () => $"{{ C:{C:P1}, M:{M:P1}, Y:{Y:P1}, K:{K:P1} }}"; @@ -214,9 +232,9 @@ namespace Xamarin.PropertyEditing.Tests public override bool Equals (object obj) { var other = (HLS)obj; - return Math.Round (other.Hue - Hue, 1) == 0 - && Math.Round (other.Lightness - Lightness, 1) == 0 - && Math.Round (other.Saturation - Saturation, 1) == 0; + return Math.Round (other.Hue, 1) == Math.Round(Hue, 1) + && Math.Round (other.Lightness, 1) == Math.Round(Lightness, 1) + && Math.Round (other.Saturation, 1) == Math.Round(Saturation, 1); } public override string ToString () => $"{{ H:{Hue:F1}°, S:{Saturation:P1}, L:{Lightness:P1} }}"; @@ -238,9 +256,9 @@ namespace Xamarin.PropertyEditing.Tests public override bool Equals (object obj) { var other = (HSB)obj; - return Math.Round (other.Hue - Hue, 1) == 0 - && Math.Round (other.Saturation - Saturation, 1) == 0 - && Math.Round (other.Brightness - Brightness, 1) == 0; + return Math.Round (other.Hue, 1) == Math.Round(Hue, 1) + && Math.Round (other.Saturation, 1) == Math.Round(Saturation, 1) + && Math.Round (other.Brightness, 1) == Math.Round(Brightness, 1); } public override string ToString () => $"{{ H:{Hue:F1}°, S:{Saturation:P1}, B:{Brightness:P1} }}"; |