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