diff --git a/src/ImageSharp/Color/Color.cs b/src/ImageSharp/Color/Color.cs index 4a50ae073..ad271dbf9 100644 --- a/src/ImageSharp/Color/Color.cs +++ b/src/ImageSharp/Color/Color.cs @@ -174,6 +174,13 @@ namespace SixLabors.ImageSharp /// public static bool TryParse(string input, out Color result) { + result = default; + + if (string.IsNullOrWhiteSpace(input)) + { + return false; + } + if (NamedColorsLookupLazy.Value.TryGetValue(input, out result)) { return true; diff --git a/tests/ImageSharp.Tests/Color/ColorTests.cs b/tests/ImageSharp.Tests/Color/ColorTests.cs index 953127f66..c689431f3 100644 --- a/tests/ImageSharp.Tests/Color/ColorTests.cs +++ b/tests/ImageSharp.Tests/Color/ColorTests.cs @@ -67,7 +67,7 @@ namespace SixLabors.ImageSharp.Tests for (int i = 0; i < ReferencePalette.WebSafeColors.Length; i++) { - Assert.Equal(ReferencePalette.WebSafeColors[i], actualPalette[i]); + Assert.Equal((Rgba32)ReferencePalette.WebSafeColors[i], actualPalette[i]); } } @@ -78,7 +78,7 @@ namespace SixLabors.ImageSharp.Tests for (int i = 0; i < ReferencePalette.WernerColors.Length; i++) { - Assert.Equal(ReferencePalette.WernerColors[i], actualPalette[i]); + Assert.Equal((Rgba32)ReferencePalette.WernerColors[i], actualPalette[i]); } } @@ -118,11 +118,35 @@ namespace SixLabors.ImageSharp.Tests Assert.Throws(() => Color.ParseHex(string.Empty)); } + [Fact] + public void ThrowsOnInvalid() + { + Assert.Throws(() => Color.ParseHex("!")); + } + [Fact] public void ThrowsOnNull() { Assert.Throws(() => Color.ParseHex(null)); } + + [Fact] + public void FalseOnEmpty() + { + Assert.False(Color.TryParseHex(string.Empty, out Color _)); + } + + [Fact] + public void FalseOnInvalid() + { + Assert.False(Color.TryParseHex("!", out Color _)); + } + + [Fact] + public void FalseOnNull() + { + Assert.False(Color.TryParseHex(null, out Color _)); + } } public class FromString @@ -156,6 +180,42 @@ namespace SixLabors.ImageSharp.Tests Assert.Equal(expected, (Rgba32)actual); } } + + [Fact] + public void ThrowsOnEmpty() + { + Assert.Throws(() => Color.Parse(string.Empty)); + } + + [Fact] + public void ThrowsOnInvalid() + { + Assert.Throws(() => Color.Parse("!")); + } + + [Fact] + public void ThrowsOnNull() + { + Assert.Throws(() => Color.Parse(null)); + } + + [Fact] + public void FalseOnEmpty() + { + Assert.False(Color.TryParse(string.Empty, out Color _)); + } + + [Fact] + public void FalseOnInvalid() + { + Assert.False(Color.TryParse("!", out Color _)); + } + + [Fact] + public void FalseOnNull() + { + Assert.False(Color.TryParse(null, out Color _)); + } } } }