Skip to content

Commit 1f21aef

Browse files
Remove KnownColors static constructor (#9661)
* Remove KnownColors static constructor * PR feedback * Add unit test and fix failing new test * Fix IDE0073 + seal test class
1 parent 3e741ef commit 1f21aef

File tree

2 files changed

+45
-17
lines changed

2 files changed

+45
-17
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Knowncolors.cs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
#if PBTCOMPILER
77
namespace MS.Internal.Markup
88
#else
9-
using System.Collections.Generic;
109
using MS.Internal;
10+
using System.Collections.Generic;
11+
using System.Globalization;
1112

1213
namespace System.Windows.Media
1314
#endif
@@ -168,16 +169,6 @@ internal static class KnownColors
168169
{
169170
#if !PBTCOMPILER
170171

171-
static KnownColors()
172-
{
173-
KnownColor[] knownColorValues = Enum.GetValues<KnownColor>();
174-
foreach (KnownColor colorValue in knownColorValues)
175-
{
176-
string aRGBString = String.Format("#{0,8:X8}", (uint)colorValue);
177-
s_knownArgbColors[aRGBString] = colorValue;
178-
}
179-
}
180-
181172
/// Return the solid color brush from a color string. If there's no match, null
182173
public static SolidColorBrush ColorStringToKnownBrush(string s)
183174
{
@@ -818,20 +809,28 @@ internal static KnownColor ColorStringToKnownColor(string colorString)
818809
#if !PBTCOMPILER
819810
internal static KnownColor ArgbStringToKnownColor(string argbString)
820811
{
821-
string argbUpper = argbString.Trim().ToUpper(System.Globalization.CultureInfo.InvariantCulture);
812+
ArgumentNullException.ThrowIfNull(argbString);
813+
814+
ReadOnlySpan<char> argbSpan = argbString.AsSpan().Trim();
815+
816+
// Use NumberStyles.AllowHexSpecifier instead of NumberStyles.HexNumber because NumberStyles.HexNumber
817+
// trims the whitespaces when we already trim it in the code above and we don't consider values
818+
// with whitespaces between the "#" and the hex value to be valid values.
819+
if (argbSpan.StartsWith('#') && uint.TryParse(argbSpan[1..], NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out uint uintValue))
820+
{
821+
KnownColor color = (KnownColor)uintValue;
822822

823-
KnownColor color;
824-
if (s_knownArgbColors.TryGetValue(argbUpper, out color))
825-
return color;
823+
if (Enum.IsDefined(color))
824+
return color;
825+
}
826826

827-
return KnownColor.UnknownColor;
827+
return KnownColor.UnknownColor;
828828
}
829829
#if DEBUG
830830
private static int s_count = 0;
831831
#endif
832832

833833
private static Dictionary<uint, SolidColorBrush> s_solidColorBrushCache = new Dictionary<uint, SolidColorBrush>();
834-
private static Dictionary<string, KnownColor> s_knownArgbColors = new Dictionary<string, KnownColor>();
835834
#endif
836835
}
837836

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace System.Windows.Media;
5+
6+
public sealed class KnownColorsTests
7+
{
8+
[Theory]
9+
// Supported values.
10+
[InlineData(KnownColor.AliceBlue, "#FFF0F8FF")]
11+
[InlineData(KnownColor.AliceBlue, " #FFF0F8FF")]
12+
[InlineData(KnownColor.AliceBlue, " #FFF0F8FF ")]
13+
[InlineData(KnownColor.AliceBlue, "#FFF0F8FF ")]
14+
// Unsupported values.
15+
[InlineData(KnownColor.UnknownColor, "")]
16+
[InlineData(KnownColor.UnknownColor, " ")]
17+
[InlineData(KnownColor.UnknownColor, "#020B37EF")] // Random ARGB that is not a known color.
18+
[InlineData(KnownColor.UnknownColor, "# FFF0F8FF")]
19+
public void ArgbStringToKnownColor_ReturnsExpected(object expected, string? argbString)
20+
{
21+
Assert.Equal((KnownColor)expected, KnownColors.ArgbStringToKnownColor(argbString));
22+
}
23+
24+
[Fact]
25+
public void ArgbStringToKnownColor_NullValue_ThrowsArgumentNullException()
26+
{
27+
Assert.Throws<ArgumentNullException>(() => KnownColors.ArgbStringToKnownColor(argbString: null));
28+
}
29+
}

0 commit comments

Comments
 (0)