From 43c366a6d0c29ba3f5b02c949d7396f24536dc04 Mon Sep 17 00:00:00 2001 From: dirk Date: Fri, 21 Oct 2016 17:43:45 +0200 Subject: [PATCH] Added FromHex and ToHex to color. Former-commit-id: 60166c603ecbab8d22260406a54e0df01f88fa4d Former-commit-id: 15b01cc24864cba066426996a85710645f274afd Former-commit-id: 1221f66d47521eaebe09ff5ad4e0ebd78abc1554 --- src/ImageProcessorCore/Colors/Color.cs | 23 ++++++++++++++- .../Colors/ColorTests.cs | 28 +++++++++++++++---- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/ImageProcessorCore/Colors/Color.cs b/src/ImageProcessorCore/Colors/Color.cs index e82a1e3a84..da24ea95ea 100644 --- a/src/ImageProcessorCore/Colors/Color.cs +++ b/src/ImageProcessorCore/Colors/Color.cs @@ -201,6 +201,27 @@ namespace ImageProcessorCore return left.packedValue != right.packedValue; } + /// + /// Creates a new instance of the struct. + /// + /// + /// The hexadecimal representation of the combined color components arranged + /// in rgb, rgba, rrggbb, or rrggbbaa format to match web syntax. + /// + public static Color FromHex(string hex) + { + return new Color(hex); + } + + /// + /// Converts the value of this instance to a hexadecimal string. + /// + /// A hexadecimal string representation of the value. + public string ToHex() + { + return this.PackedValue.ToString("X8"); + } + /// public void PackFromVector4(Vector4 vector) { @@ -309,4 +330,4 @@ namespace ImageProcessorCore return red + red + green + green + blue + blue + alpha + alpha; } } -} \ No newline at end of file +} diff --git a/tests/ImageProcessorCore.Tests/Colors/ColorTests.cs b/tests/ImageProcessorCore.Tests/Colors/ColorTests.cs index a2f53694e0..433202e527 100644 --- a/tests/ImageProcessorCore.Tests/Colors/ColorTests.cs +++ b/tests/ImageProcessorCore.Tests/Colors/ColorTests.cs @@ -91,15 +91,31 @@ namespace ImageProcessorCore.Tests } /// - /// Tests to see that in the input hex matches that of the output. + /// Tests whether FromHex and ToHex work correctly. /// [Fact] - public void ConvertHex() + public void FromAndToHex() { - const string First = "000000FF"; - Color color = Color.Black; - string second = color.PackedValue.ToString("X8"); - Assert.Equal(First, second); + Color color = Color.FromHex("#AABBCCDD"); + Assert.Equal(170, color.R); + Assert.Equal(187, color.G); + Assert.Equal(204, color.B); + Assert.Equal(221, color.A); + + color.A = 170; + color.B = 187; + color.G = 204; + color.R = 221; + + Assert.Equal("DDCCBBAA", color.ToHex()); + + color.R = 0; + + Assert.Equal("00CCBBAA", color.ToHex()); + + color.A = 255; + + Assert.Equal("00CCBBFF", color.ToHex()); } } } \ No newline at end of file