Allow color numbers in markup expressions

Closes #614
This commit is contained in:
Patrik Svensson 2021-11-08 09:06:32 +01:00 committed by Phil Scott
parent 9c9eb04f91
commit e86f9d3c5a
2 changed files with 40 additions and 0 deletions

View File

@ -96,6 +96,21 @@ namespace Spectre.Console
return null;
}
}
else if (int.TryParse(part, out var number))
{
if (number < 0)
{
error = $"Color number must be greater than or equal to 0 (was {number})";
return null;
}
else if (number > 255)
{
error = $"Color number must be less than or equal to 255 (was {number})";
return null;
}
color = number;
}
else
{
error = !foreground

View File

@ -275,6 +275,31 @@ namespace Spectre.Console.Tests.Unit
result.Background.ShouldBe(Color.Blue);
}
[Theory]
[InlineData("12 on 24")]
public void Should_Parse_Colors_Numbers_Correctly(string style)
{
// Given, When
var result = Style.Parse(style);
// Then
result.Foreground.ShouldBe(Color.Blue);
result.Background.ShouldBe(Color.DeepSkyBlue4_1);
}
[Theory]
[InlineData("-12", "Color number must be greater than or equal to 0 (was -12)")]
[InlineData("256", "Color number must be less than or equal to 255 (was 256)")]
public void Should_Return_Error_If_Color_Number_Is_Invalid(string style, string expected)
{
// Given, When
var result = Record.Exception(() => Style.Parse(style));
// Then
result.ShouldNotBeNull();
result.Message.ShouldBe(expected);
}
[Theory]
[InlineData("rgb()", "Invalid RGB color 'rgb()'.")]
[InlineData("rgb(", "Invalid RGB color 'rgb('.")]