From f0e3bf887baf7bda86e99f187ed71b2c7aba59da Mon Sep 17 00:00:00 2001 From: James South Date: Thu, 18 Sep 2014 17:21:59 +0100 Subject: [PATCH] Fixing unit tests Former-commit-id: fc41dd3f921e5fd4a6594b3fe5efb61c20d66515 --- .../Extensions/DoubleExtensionsUnitTests.cs | 8 ++-- .../Common/Extensions/DoubleExtensions.cs | 2 +- .../Imaging/Colors/HSLAColor.cs | 41 +++++++++++++++++-- .../Imaging/Colors/RGBAColor.cs | 28 ++++++------- 4 files changed, 57 insertions(+), 22 deletions(-) diff --git a/src/ImageProcessor.UnitTests/Extensions/DoubleExtensionsUnitTests.cs b/src/ImageProcessor.UnitTests/Extensions/DoubleExtensionsUnitTests.cs index 91b62d74f..e3942c8e9 100644 --- a/src/ImageProcessor.UnitTests/Extensions/DoubleExtensionsUnitTests.cs +++ b/src/ImageProcessor.UnitTests/Extensions/DoubleExtensionsUnitTests.cs @@ -25,10 +25,10 @@ namespace ImageProcessor.UnitTests.Extensions /// Double input /// Expected result [Test] - [TestCase(-10, 0x0)] - [TestCase(1.5, 0x1)] - [TestCase(25.7, 0x19)] - [TestCase(1289047, 0xFF)] + [TestCase(-10, 0)] + [TestCase(1.5, 2)] + [TestCase(25.7, 26)] + [TestCase(1289047, 255)] public void TestDoubleToByte(double input, byte expected) { byte result = input.ToByte(); diff --git a/src/ImageProcessor/Common/Extensions/DoubleExtensions.cs b/src/ImageProcessor/Common/Extensions/DoubleExtensions.cs index 1ce97d2ac..789058179 100644 --- a/src/ImageProcessor/Common/Extensions/DoubleExtensions.cs +++ b/src/ImageProcessor/Common/Extensions/DoubleExtensions.cs @@ -32,7 +32,7 @@ namespace ImageProcessor.Common.Extensions /// public static byte ToByte(this double d) { - return Convert.ToByte(Math.Max(0.0d, Math.Min(255d, d))); // ((d > byte.MaxValue) ? byte.MaxValue : ((d < byte.MinValue) ? byte.MinValue : d)); + return Convert.ToByte(Math.Max(0.0d, Math.Min(255d, d))); } } } diff --git a/src/ImageProcessor/Imaging/Colors/HSLAColor.cs b/src/ImageProcessor/Imaging/Colors/HSLAColor.cs index d989f4ed3..24939766b 100644 --- a/src/ImageProcessor/Imaging/Colors/HSLAColor.cs +++ b/src/ImageProcessor/Imaging/Colors/HSLAColor.cs @@ -201,7 +201,13 @@ namespace ImageProcessor.Imaging.Colors /// public static implicit operator HslaColor(Color color) { - return FromColor(color); + HslaColor hslColor = new HslaColor( + color.GetHue() / 360.0f, + color.GetSaturation(), + color.GetBrightness(), + color.A / 255f); + + return hslColor; } /// @@ -231,7 +237,14 @@ namespace ImageProcessor.Imaging.Colors /// public static implicit operator HslaColor(YCbCrColor ycbcrColor) { - return FromColor(ycbcrColor); + Color color = ycbcrColor; + HslaColor hslColor = new HslaColor( + color.GetHue() / 360.0f, + color.GetSaturation(), + color.GetBrightness(), + color.A / 255f); + + return hslColor; } /// @@ -283,7 +296,29 @@ namespace ImageProcessor.Imaging.Colors /// public static implicit operator RgbaColor(HslaColor hslaColor) { - return RgbaColor.FromColor(hslaColor); + float r = 0, g = 0, b = 0; + if (Math.Abs(hslaColor.l - 0) > .0001) + { + if (Math.Abs(hslaColor.s - 0) <= .0001) + { + r = g = b = hslaColor.l; + } + else + { + float temp2 = GetTemp2(hslaColor); + float temp1 = (2.0f * hslaColor.l) - temp2; + + r = GetColorComponent(temp1, temp2, hslaColor.h + (1.0f / 3.0f)); + g = GetColorComponent(temp1, temp2, hslaColor.h); + b = GetColorComponent(temp1, temp2, hslaColor.h - (1.0f / 3.0f)); + } + } + + return RgbaColor.FromRgba( + Convert.ToByte(255 * r), + Convert.ToByte(255 * g), + Convert.ToByte(255 * b), + Convert.ToByte(255 * hslaColor.a)); } /// diff --git a/src/ImageProcessor/Imaging/Colors/RGBAColor.cs b/src/ImageProcessor/Imaging/Colors/RGBAColor.cs index 790e2ae09..87e8fa9aa 100644 --- a/src/ImageProcessor/Imaging/Colors/RGBAColor.cs +++ b/src/ImageProcessor/Imaging/Colors/RGBAColor.cs @@ -254,20 +254,20 @@ namespace ImageProcessor.Imaging.Colors return HslaColor.FromColor(rgba); } - /// - /// Allows the implicit conversion of an instance of to a - /// . - /// - /// - /// The instance of to convert. - /// - /// - /// An instance of . - /// - public static implicit operator YCbCrColor(RgbaColor rgba) - { - return YCbCrColor.FromColor(rgba); - } + ///// + ///// Allows the implicit conversion of an instance of to a + ///// . + ///// + ///// + ///// The instance of to convert. + ///// + ///// + ///// An instance of . + ///// + //public static implicit operator YCbCrColor(RgbaColor rgba) + //{ + // return YCbCrColor.FromColor(rgba); + //} /// /// Returns a that represents this instance.