Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Sources/ModelingEvolution.Drawing.Tests/ColorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ public void TryParse_ReturnsExpectedResult(string input, bool expectedResult)
Assert.Equal(expectedResult, result);
}

[Theory]
[InlineData("#FF0000", 255, 0, 0, 255)]
[InlineData("#00FF00", 0, 255, 0, 255)]
[InlineData("#0000FF", 0, 0, 255, 255)]
[InlineData("#80808080", 128, 128, 128, 128)]
[InlineData("0xFF0000", 255, 0, 0, 255)]
[InlineData("rgba(255,128,64,0.5)", 255, 128, 64, 127)]
public void TryParse_ValidInputs_ReturnsExpectedColor(string input, byte r, byte g, byte b, byte a)
{
Color.TryParse(input, out var color).Should().BeTrue();

color.R.Should().Be(r);
color.G.Should().Be(g);
color.B.Should().Be(b);
color.A.Should().Be(a);
}

[Fact]
public void JsonSerialization_RoundTrip_PreservesValues()
{
Expand Down
7 changes: 5 additions & 2 deletions Sources/ModelingEvolution.Drawing/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,11 @@ public static bool TryParse([NotNullWhen(true)] string s, IFormatProvider? provi
s = s.StartsWith("#") ? s[1..] : s;

if (!uint.TryParse(s, NumberStyles.HexNumber, provider, out var value)) return false;

result = new Color(value);

uint alpha = 255;
if (s.Length == 8)
alpha = 0;
result = new Color(value | alpha << ARGBAlphaShift);
return true;

}
Expand Down
Loading