File tree 2 files changed +24
-0
lines changed
Tests/Spectre.Console.Tests/Unit
2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -230,6 +230,13 @@ public static Color FromHex(string hex)
230
230
hex = hex . Substring ( 1 ) ;
231
231
}
232
232
233
+ // 3 digit hex codes are expanded to 6 digits
234
+ // by doubling each digit, conform to CSS color codes
235
+ if ( hex . Length == 3 )
236
+ {
237
+ hex = string . Concat ( hex . Select ( c => new string ( c , 2 ) ) ) ;
238
+ }
239
+
233
240
var r = byte . Parse ( hex . Substring ( 0 , 2 ) , NumberStyles . HexNumber ) ;
234
241
var g = byte . Parse ( hex . Substring ( 2 , 2 ) , NumberStyles . HexNumber ) ;
235
242
var b = byte . Parse ( hex . Substring ( 4 , 2 ) , NumberStyles . HexNumber ) ;
Original file line number Diff line number Diff line change @@ -68,6 +68,23 @@ public void Should_Not_Parse_Non_Color_Try_From_Hex(string noncolor)
68
68
color . ShouldBe ( Color . Default ) ;
69
69
}
70
70
71
+ [ Theory ]
72
+ [ InlineData ( "ffffff" ) ]
73
+ [ InlineData ( "#ffffff" ) ]
74
+ [ InlineData ( "fff" ) ]
75
+ [ InlineData ( "#fff" ) ]
76
+ public void Should_Parse_3_Digit_Hex_Colors_From_Hex ( string color )
77
+ {
78
+ // Given
79
+ var expected = new Color ( 255 , 255 , 255 ) ;
80
+
81
+ // When
82
+ var result = Color . FromHex ( color ) ;
83
+
84
+ // Then
85
+ result . ShouldBe ( expected ) ;
86
+ }
87
+
71
88
[ Fact ]
72
89
public void Should_Consider_Color_And_Non_Color_Equal ( )
73
90
{
You can’t perform that action at this time.
0 commit comments