|
6 | 6 | #if PBTCOMPILER |
7 | 7 | namespace MS.Internal.Markup |
8 | 8 | #else |
9 | | -using System.Collections.Generic; |
10 | 9 | using MS.Internal; |
| 10 | +using System.Collections.Generic; |
| 11 | +using System.Globalization; |
11 | 12 |
|
12 | 13 | namespace System.Windows.Media |
13 | 14 | #endif |
@@ -168,16 +169,6 @@ internal static class KnownColors |
168 | 169 | { |
169 | 170 | #if !PBTCOMPILER |
170 | 171 |
|
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 | | - |
181 | 172 | /// Return the solid color brush from a color string. If there's no match, null |
182 | 173 | public static SolidColorBrush ColorStringToKnownBrush(string s) |
183 | 174 | { |
@@ -818,20 +809,28 @@ internal static KnownColor ColorStringToKnownColor(string colorString) |
818 | 809 | #if !PBTCOMPILER |
819 | 810 | internal static KnownColor ArgbStringToKnownColor(string argbString) |
820 | 811 | { |
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; |
822 | 822 |
|
823 | | - KnownColor color; |
824 | | - if (s_knownArgbColors.TryGetValue(argbUpper, out color)) |
825 | | - return color; |
| 823 | + if (Enum.IsDefined(color)) |
| 824 | + return color; |
| 825 | + } |
826 | 826 |
|
827 | | - return KnownColor.UnknownColor; |
| 827 | + return KnownColor.UnknownColor; |
828 | 828 | } |
829 | 829 | #if DEBUG |
830 | 830 | private static int s_count = 0; |
831 | 831 | #endif |
832 | 832 |
|
833 | 833 | private static Dictionary<uint, SolidColorBrush> s_solidColorBrushCache = new Dictionary<uint, SolidColorBrush>(); |
834 | | - private static Dictionary<string, KnownColor> s_knownArgbColors = new Dictionary<string, KnownColor>(); |
835 | 834 | #endif |
836 | 835 | } |
837 | 836 |
|
|
0 commit comments