Skip to content

Commit

Permalink
Added hex color conversion (#1432)
Browse files Browse the repository at this point in the history
* Added hex color conversion

---------

Co-authored-by: Frank Ray <[email protected]>
Co-authored-by: Nils Andresen <[email protected]>
  • Loading branch information
3 people authored Nov 13, 2024
1 parent a87277e commit 574ead6
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/Spectre.Console/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,50 @@ public static Color FromInt32(int number)
return ColorTable.GetColor(number);
}

/// <summary>
/// Creates a color from a hexadecimal string representation.
/// </summary>
/// <param name="hex">The hexadecimal string representation of the color.</param>
/// <returns>The color created from the hexadecimal string.</returns>
public static Color FromHex(string hex)
{
if (hex is null)
{
throw new ArgumentNullException(nameof(hex));
}

if (hex.StartsWith("#"))
{
hex = hex.Substring(1);
}

var r = byte.Parse(hex.Substring(0, 2), NumberStyles.HexNumber);
var g = byte.Parse(hex.Substring(2, 2), NumberStyles.HexNumber);
var b = byte.Parse(hex.Substring(4, 2), NumberStyles.HexNumber);

return new Color(r, g, b);
}

/// <summary>
/// Tries to convert a hexadecimal color code to a <see cref="Color"/> object.
/// </summary>
/// <param name="hex">The hexadecimal color code.</param>
/// <param name="color">When this method returns, contains the <see cref="Color"/> equivalent of the hexadecimal color code, if the conversion succeeded, or <see cref="Color.Default"/> if the conversion failed.</param>
/// <returns><c>true</c> if the conversion succeeded; otherwise, <c>false</c>.</returns>
public static bool TryFromHex(string hex, out Color color)
{
try
{
color = FromHex(hex);
return true;
}
catch
{
color = Color.Default;
return false;
}
}

/// <summary>
/// Converts a <see cref="ConsoleColor"/> to a <see cref="Color"/>.
/// </summary>
Expand Down
64 changes: 64 additions & 0 deletions src/Tests/Spectre.Console.Tests/Unit/ColorTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,73 @@
using System.Drawing;

namespace Spectre.Console.Tests.Unit;

public sealed class ColorTests
{
public sealed class TheEqualsMethod
{
[Theory]
[InlineData("800080")]
[InlineData("#800080")]
public void Should_Consider_Color_And_Color_From_Hex_Equal(string color)
{
// Given
var color1 = new Color(128, 0, 128);

// When
var color2 = Color.FromHex(color);

// Then
color2.ShouldBe(color1);
}

[Theory]
[InlineData("800080")]
[InlineData("#800080")]
public void Should_Consider_Color_And_Color_Try_From_Hex_Equal(string color)
{
// Given
var color1 = new Color(128, 0, 128);

// When
var result = Color.TryFromHex(color, out var color2);

// Then
result.ShouldBeTrue();
color2.ShouldBe(color1);
}

[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData("#")]
[InlineData("#80")]
[InlineData("FOO")]
public void Should_Not_Parse_Non_Color_From_Hex(string noncolor)
{
// Given, When
var result = Record.Exception(() => Color.FromHex(noncolor));

// Then
result.ShouldBeAssignableTo<Exception>();
}

[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData("#")]
[InlineData("#80")]
[InlineData("FOO")]
public void Should_Not_Parse_Non_Color_Try_From_Hex(string noncolor)
{
// Given, When
var result = Color.TryFromHex(noncolor, out var color);

// Then
result.ShouldBeFalse();
color.ShouldBe(Color.Default);
}

[Fact]
public void Should_Consider_Color_And_Non_Color_Equal()
{
Expand Down

0 comments on commit 574ead6

Please sign in to comment.