Skip to content

Remove KnownColors static constructor #9661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
#if PBTCOMPILER
namespace MS.Internal.Markup
#else
using System.Collections.Generic;
using MS.Internal;
using System.Collections.Generic;
using System.Globalization;

namespace System.Windows.Media
#endif
Expand Down Expand Up @@ -169,16 +170,6 @@ internal static class KnownColors
{
#if !PBTCOMPILER

static KnownColors()
{
KnownColor[] knownColorValues = Enum.GetValues<KnownColor>();
foreach (KnownColor colorValue in knownColorValues)
{
string aRGBString = String.Format("#{0,8:X8}", (uint)colorValue);
s_knownArgbColors[aRGBString] = colorValue;
}
}

/// Return the solid color brush from a color string. If there's no match, null
public static SolidColorBrush ColorStringToKnownBrush(string s)
{
Expand Down Expand Up @@ -819,20 +810,28 @@ internal static KnownColor ColorStringToKnownColor(string colorString)
#if !PBTCOMPILER
internal static KnownColor ArgbStringToKnownColor(string argbString)
{
string argbUpper = argbString.Trim().ToUpper(System.Globalization.CultureInfo.InvariantCulture);
ArgumentNullException.ThrowIfNull(argbString);

ReadOnlySpan<char> argbSpan = argbString.AsSpan().Trim();

// Use NumberStyles.AllowHexSpecifier instead of NumberStyles.HexNumber because NumberStyles.HexNumber
// trims the whitespaces when we already trim it in the code above and we don't consider values
// with whitespaces between the "#" and the hex value to be valid values.
if (argbSpan.StartsWith('#') && uint.TryParse(argbSpan[1..], NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out uint uintValue))
{
KnownColor color = (KnownColor)uintValue;

KnownColor color;
if (s_knownArgbColors.TryGetValue(argbUpper, out color))
return color;
if (Enum.IsDefined(color))
return color;
}

return KnownColor.UnknownColor;
return KnownColor.UnknownColor;
}
#if DEBUG
private static int s_count = 0;
#endif

private static Dictionary<uint, SolidColorBrush> s_solidColorBrushCache = new Dictionary<uint, SolidColorBrush>();
private static Dictionary<string, KnownColor> s_knownArgbColors = new Dictionary<string, KnownColor>();
#endif
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Windows.Media;

public sealed class KnownColorsTests
{
[Theory]
// Supported values.
[InlineData(KnownColor.AliceBlue, "#FFF0F8FF")]
[InlineData(KnownColor.AliceBlue, " #FFF0F8FF")]
[InlineData(KnownColor.AliceBlue, " #FFF0F8FF ")]
[InlineData(KnownColor.AliceBlue, "#FFF0F8FF ")]
// Unsupported values.
[InlineData(KnownColor.UnknownColor, "")]
[InlineData(KnownColor.UnknownColor, " ")]
[InlineData(KnownColor.UnknownColor, "#020B37EF")] // Random ARGB that is not a known color.
[InlineData(KnownColor.UnknownColor, "# FFF0F8FF")]
public void ArgbStringToKnownColor_ReturnsExpected(object expected, string? argbString)
{
Assert.Equal((KnownColor)expected, KnownColors.ArgbStringToKnownColor(argbString));
}

[Fact]
public void ArgbStringToKnownColor_NullValue_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => KnownColors.ArgbStringToKnownColor(argbString: null));
}
}