mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-16 12:13:22 +08:00
Implement 3 digit hex parsing (#1708)
This commit is contained in:
parent
29ab313bb9
commit
58bf89a56a
@ -230,6 +230,13 @@ public partial struct Color : IEquatable<Color>
|
|||||||
hex = hex.Substring(1);
|
hex = hex.Substring(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 3 digit hex codes are expanded to 6 digits
|
||||||
|
// by doubling each digit, conform to CSS color codes
|
||||||
|
if (hex.Length == 3)
|
||||||
|
{
|
||||||
|
hex = string.Concat(hex.Select(c => new string(c, 2)));
|
||||||
|
}
|
||||||
|
|
||||||
var r = byte.Parse(hex.Substring(0, 2), NumberStyles.HexNumber);
|
var r = byte.Parse(hex.Substring(0, 2), NumberStyles.HexNumber);
|
||||||
var g = byte.Parse(hex.Substring(2, 2), NumberStyles.HexNumber);
|
var g = byte.Parse(hex.Substring(2, 2), NumberStyles.HexNumber);
|
||||||
var b = byte.Parse(hex.Substring(4, 2), NumberStyles.HexNumber);
|
var b = byte.Parse(hex.Substring(4, 2), NumberStyles.HexNumber);
|
||||||
|
@ -68,6 +68,23 @@ public sealed class ColorTests
|
|||||||
color.ShouldBe(Color.Default);
|
color.ShouldBe(Color.Default);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("ffffff")]
|
||||||
|
[InlineData("#ffffff")]
|
||||||
|
[InlineData("fff")]
|
||||||
|
[InlineData("#fff")]
|
||||||
|
public void Should_Parse_3_Digit_Hex_Colors_From_Hex(string color)
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var expected = new Color(255, 255, 255);
|
||||||
|
|
||||||
|
// When
|
||||||
|
var result = Color.FromHex(color);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
result.ShouldBe(expected);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Should_Consider_Color_And_Non_Color_Equal()
|
public void Should_Consider_Color_And_Non_Color_Equal()
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user