Skip to content

Commit

Permalink
(MahApps#4131) Implement ColorHelper-Constructor with respect to a gi…
Browse files Browse the repository at this point in the history
…ven CultureInfo

⚠ BREAKING CHANGE ⚠
- Make the `ColorNamesDictionary` an instance member 
- Create parameterless constructor with uses the default translations
- Create constructor which uses a given culture. If the the culture is `null` we use the actual color names from .NET
- Create a constructor which uses a given culture and a given resource to look up the names

- Create a new static instance DefaultInstanceInvariant which provides the invariant color names.
  • Loading branch information
timunie authored and punker76 committed Jun 29, 2021
1 parent 4c13d5c commit a8fd41c
Showing 1 changed file with 60 additions and 11 deletions.
71 changes: 60 additions & 11 deletions src/MahApps.Metro/Controls/ColorPicker/ColorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Windows.Media;

Expand All @@ -24,28 +25,76 @@ public class ColorHelper
/// </summary>
public static readonly ColorHelper DefaultInstance = new();

// Explicit static constructor to tell C# compiler not to mark type as beforefieldinit
static ColorHelper()
/// <summary>
/// Gets a static default instance of <see cref="ColorHelper"/> with the original WPF color names.
/// </summary>
public static readonly ColorHelper DefaultInstanceInvariant = new(CultureInfo.InvariantCulture);


/// <summary>
/// Creates a new Instance of the ColorHelper with the default translations.
/// </summary>
public ColorHelper() : this(CultureInfo.CurrentUICulture, typeof(ColorNames))
{
ColorNamesDictionary = new Dictionary<Color, string>();
// Empty
}

var rm = new ResourceManager(typeof(ColorNames));
var resourceSet = rm.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
/// <summary>
/// Creates a new Instance of the ColorHelper
/// </summary>
/// <param name="culture">The <see cref="CultureInfo"/> used to get the local color names</param>
public ColorHelper(CultureInfo? culture) : this(culture, typeof(ColorNames))
{
// Empty
}

if (resourceSet != null)
/// <summary>
/// Creates a new Instance of the ColorHelper
/// </summary>
/// <param name="culture">The <see cref="CultureInfo"/> used to get the local color names</param>
/// <param name="resourceDictionaryType">A type from which the <see cref="ResourceManager"/> derives all information for finding .resources files.</param>
public ColorHelper(CultureInfo? culture, Type resourceDictionaryType)
{
ColorNamesDictionary = new Dictionary<Color, string>();

if (culture is null)
{
foreach (var entry in resourceSet.OfType<DictionaryEntry>())
foreach (var propertyInfo in typeof(Colors).GetProperties(BindingFlags.Static | BindingFlags.Public))
{
var color = (Color)(propertyInfo.GetValue(null) ?? default(Color));
try
{
if (ColorConverter.ConvertFromString(entry.Key.ToString()) is Color color)
if (!ColorNamesDictionary.ContainsKey(color))
{
ColorNamesDictionary.Add(color, entry.Value!.ToString()!);
ColorNamesDictionary.Add(color, propertyInfo.Name);
}
}
catch (Exception)
{
Trace.TraceError($"{entry.Key} is not a valid color key!");
Trace.TraceError($"{color} could not be added to dictionary!");
}
}
}
else
{
var rm = new ResourceManager(resourceDictionaryType);
var resourceSet = rm.GetResourceSet(culture, true, true);

if (resourceSet != null)
{
foreach (var entry in resourceSet.OfType<DictionaryEntry>())
{
try
{
if (ColorConverter.ConvertFromString(entry.Key.ToString()) is Color color)
{
ColorNamesDictionary.Add(color, entry.Value!.ToString()!);
}
}
catch (Exception)
{
Trace.TraceError($"{entry.Key} is not a valid color key!");
}
}
}
}
Expand Down Expand Up @@ -112,7 +161,7 @@ static ColorHelper()
/// <summary>
/// A Dictionary with localized Color Names
/// </summary>
public static Dictionary<Color, string> ColorNamesDictionary { get; set; }
public Dictionary<Color, string> ColorNamesDictionary { get; }

/// <summary>
/// Searches for the localized name of a given <paramref name="color"/>
Expand Down

0 comments on commit a8fd41c

Please sign in to comment.