1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- #nullable enable
- using System.Collections;
- using System.Globalization;
- using System.Resources;
- using Terminal.Gui.Resources;
- namespace Terminal.Gui;
- /// <summary>
- /// Provides a mapping between <see cref="Color"/> and the W3C standard color name strings.
- /// </summary>
- public static class ColorStrings
- {
- /// <summary>
- /// Gets the W3C standard string for <paramref name="color"/>.
- /// </summary>
- /// <param name="color">The color.</param>
- /// <returns><see langword="null"/> if there is no standard color name for the specified color.</returns>
- public static string? GetW3CColorName (Color color)
- {
- // Fetch the color name from the resource file
- return GlobalResources.GetString ($"#{color.R:X2}{color.G:X2}{color.B:X2}", CultureInfo.CurrentUICulture);
- }
- /// <summary>
- /// Returns the list of W3C standard color names.
- /// </summary>
- /// <returns></returns>
- public static IEnumerable<string> GetW3CColorNames ()
- {
- foreach (DictionaryEntry entry in GlobalResources.GetResourceSet (
- CultureInfo.CurrentUICulture,
- true,
- true,
- e =>
- {
- string keyName = e.Key.ToString () ?? string.Empty;
- return e.Value is string && keyName.StartsWith ('#');
- })!)
- {
- yield return (entry.Value as string)!;
- }
- }
- /// <summary>
- /// Parses <paramref name="name"/> and returns <paramref name="color"/> if name is a W3C standard named color.
- /// </summary>
- /// <param name="name">The name to parse.</param>
- /// <param name="color">If successful, the color.</param>
- /// <returns><see langword="true"/> if <paramref name="name"/> was parsed successfully.</returns>
- public static bool TryParseW3CColorName (string name, out Color color)
- {
- // Iterate through all resource entries to find the matching color name
- foreach (DictionaryEntry entry in GlobalResources.GetResourceSet (CultureInfo.CurrentUICulture, true, true)!)
- {
- if (entry.Value is string colorName && colorName.Equals (name, StringComparison.OrdinalIgnoreCase))
- {
- // Parse the key to extract the color components
- string key = entry.Key.ToString () ?? string.Empty;
- if (key.StartsWith ("#") && key.Length == 7)
- {
- if (int.TryParse (key.Substring (1, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int r)
- && int.TryParse (key.Substring (3, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int g)
- && int.TryParse (key.Substring (5, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int b))
- {
- color = new (r, g, b);
- return true;
- }
- }
- }
- }
- color = default (Color);
- return false;
- }
- }
|