ColorStrings.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #nullable enable
  2. using System.Collections;
  3. using System.Globalization;
  4. using System.Resources;
  5. using Terminal.Gui.Resources;
  6. namespace Terminal.Gui;
  7. /// <summary>
  8. /// Provides a mapping between <see cref="Color"/> and the W3C standard color name strings.
  9. /// </summary>
  10. public static class ColorStrings
  11. {
  12. private static readonly ResourceManager _resourceManager = new (typeof (Strings));
  13. /// <summary>
  14. /// Gets the W3C standard string for <paramref name="color"/>.
  15. /// </summary>
  16. /// <param name="color">The color.</param>
  17. /// <returns><see langword="null"/> if there is no standard color name for the specified color.</returns>
  18. public static string? GetW3CColorName (Color color)
  19. {
  20. // Fetch the color name from the resource file
  21. return _resourceManager.GetString ($"#{color.R:X2}{color.G:X2}{color.B:X2}", CultureInfo.CurrentUICulture);
  22. }
  23. /// <summary>
  24. /// Returns the list of W3C standard color names.
  25. /// </summary>
  26. /// <returns></returns>
  27. public static IEnumerable<string> GetW3CColorNames ()
  28. {
  29. foreach (DictionaryEntry entry in _resourceManager.GetResourceSet (CultureInfo.CurrentUICulture, true, true)!)
  30. {
  31. string keyName = entry.Key.ToString () ?? string.Empty;
  32. if (entry.Value is string colorName && keyName.StartsWith ('#'))
  33. {
  34. yield return colorName;
  35. }
  36. }
  37. }
  38. /// <summary>
  39. /// Parses <paramref name="name"/> and returns <paramref name="color"/> if name is a W3C standard named color.
  40. /// </summary>
  41. /// <param name="name">The name to parse.</param>
  42. /// <param name="color">If successful, the color.</param>
  43. /// <returns><see langword="true"/> if <paramref name="name"/> was parsed successfully.</returns>
  44. public static bool TryParseW3CColorName (string name, out Color color)
  45. {
  46. // Iterate through all resource entries to find the matching color name
  47. foreach (DictionaryEntry entry in _resourceManager.GetResourceSet (CultureInfo.CurrentUICulture, true, true)!)
  48. {
  49. if (entry.Value is string colorName && colorName.Equals (name, StringComparison.OrdinalIgnoreCase))
  50. {
  51. // Parse the key to extract the color components
  52. string key = entry.Key.ToString () ?? string.Empty;
  53. if (key.StartsWith ("#") && key.Length == 7)
  54. {
  55. if (int.TryParse (key.Substring (1, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int r)
  56. && int.TryParse (key.Substring (3, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int g)
  57. && int.TryParse (key.Substring (5, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int b))
  58. {
  59. color = new (r, g, b);
  60. return true;
  61. }
  62. }
  63. }
  64. }
  65. color = default (Color);
  66. return false;
  67. }
  68. }