Browse Source

Added FromHex and ToHex to color.

Former-commit-id: 60166c603ecbab8d22260406a54e0df01f88fa4d
Former-commit-id: 15b01cc24864cba066426996a85710645f274afd
Former-commit-id: 1221f66d47521eaebe09ff5ad4e0ebd78abc1554
af/merge-core
dirk 10 years ago
parent
commit
43c366a6d0
  1. 23
      src/ImageProcessorCore/Colors/Color.cs
  2. 28
      tests/ImageProcessorCore.Tests/Colors/ColorTests.cs

23
src/ImageProcessorCore/Colors/Color.cs

@ -201,6 +201,27 @@ namespace ImageProcessorCore
return left.packedValue != right.packedValue; return left.packedValue != right.packedValue;
} }
/// <summary>
/// Creates a new instance of the <see cref="Color"/> struct.
/// </summary>
/// <param name="hex">
/// The hexadecimal representation of the combined color components arranged
/// in rgb, rgba, rrggbb, or rrggbbaa format to match web syntax.
/// </param>
public static Color FromHex(string hex)
{
return new Color(hex);
}
/// <summary>
/// Converts the value of this instance to a hexadecimal string.
/// </summary>
/// <returns>A hexadecimal string representation of the value.</returns>
public string ToHex()
{
return this.PackedValue.ToString("X8");
}
/// <inheritdoc/> /// <inheritdoc/>
public void PackFromVector4(Vector4 vector) public void PackFromVector4(Vector4 vector)
{ {
@ -309,4 +330,4 @@ namespace ImageProcessorCore
return red + red + green + green + blue + blue + alpha + alpha; return red + red + green + green + blue + blue + alpha + alpha;
} }
} }
} }

28
tests/ImageProcessorCore.Tests/Colors/ColorTests.cs

@ -91,15 +91,31 @@ namespace ImageProcessorCore.Tests
} }
/// <summary> /// <summary>
/// Tests to see that in the input hex matches that of the output. /// Tests whether FromHex and ToHex work correctly.
/// </summary> /// </summary>
[Fact] [Fact]
public void ConvertHex() public void FromAndToHex()
{ {
const string First = "000000FF"; Color color = Color.FromHex("#AABBCCDD");
Color color = Color.Black; Assert.Equal(170, color.R);
string second = color.PackedValue.ToString("X8"); Assert.Equal(187, color.G);
Assert.Equal(First, second); 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());
} }
} }
} }
Loading…
Cancel
Save