StandardColors.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System.Collections.Frozen;
  2. using System.Collections.Immutable;
  3. using System.Diagnostics.CodeAnalysis;
  4. namespace Terminal.Gui.Drawing;
  5. /// <summary>
  6. /// Helper class for transforming to and from <see cref="StandardColor"/> enum.
  7. /// </summary>
  8. internal static class StandardColors
  9. {
  10. private static readonly ImmutableArray<string> _names;
  11. private static readonly FrozenDictionary<uint, string> _argbNameMap;
  12. static StandardColors ()
  13. {
  14. // Populate based on names because enums with same numerical value
  15. // are not otherwise distinguishable from each other.
  16. string [] standardNames = Enum.GetNames<StandardColor> ().Order ().ToArray ();
  17. Dictionary<uint, string> map = new (standardNames.Length);
  18. foreach (string name in standardNames)
  19. {
  20. StandardColor standardColor = Enum.Parse<StandardColor> (name);
  21. uint argb = GetArgb (standardColor);
  22. // TODO: Collect aliases?
  23. _ = map.TryAdd (argb, name);
  24. }
  25. _names = ImmutableArray.Create (standardNames);
  26. _argbNameMap = map.ToFrozenDictionary ();
  27. }
  28. /// <summary>
  29. /// Gets read-only list of the W3C colors in alphabetical order.
  30. /// </summary>
  31. public static IReadOnlyList<string> GetColorNames ()
  32. {
  33. return _names;
  34. }
  35. /// <summary>
  36. /// Converts the given Standard (W3C+) color name to equivalent color value.
  37. /// </summary>
  38. /// <param name="name">Standard (W3C+) color name.</param>
  39. /// <param name="color">The successfully converted Standard (W3C+) color value.</param>
  40. /// <returns>True if the conversion succeeded; otherwise false.</returns>
  41. public static bool TryParseColor (ReadOnlySpan<char> name, out Color color)
  42. {
  43. if (!Enum.TryParse (name, ignoreCase: true, out StandardColor standardColor) ||
  44. // Any numerical value converts to undefined enum value.
  45. !Enum.IsDefined (standardColor))
  46. {
  47. color = default;
  48. return false;
  49. }
  50. uint argb = GetArgb (standardColor);
  51. color = new Color (argb);
  52. return true;
  53. }
  54. /// <summary>
  55. /// Converts the given color value to a Standard (W3C+) color name.
  56. /// </summary>
  57. /// <param name="color">Color value to match Standard (W3C+)color.</param>
  58. /// <param name="name">The successfully converted Standard (W3C+) color name.</param>
  59. /// <returns>True if conversion succeeded; otherwise false.</returns>
  60. public static bool TryNameColor (Color color, [NotNullWhen (true)] out string? name)
  61. {
  62. if (_argbNameMap.TryGetValue (color.Argb, out name))
  63. {
  64. return true;
  65. }
  66. name = null;
  67. return false;
  68. }
  69. internal static uint GetArgb (StandardColor standardColor)
  70. {
  71. const int ALPHA_SHIFT = 24;
  72. const uint ALPHA_MASK = 0xFFU << ALPHA_SHIFT;
  73. int rgb = (int)standardColor;
  74. uint argb = (uint)rgb | ALPHA_MASK;
  75. return argb;
  76. }
  77. }