ColorStrings.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #nullable enable
  2. using System.Globalization;
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// Provides a mapping between <see cref="Color"/> and the W3C standard color name strings.
  6. /// </summary>
  7. public static class ColorStrings
  8. {
  9. private static readonly AnsiColorNameResolver Ansi = new();
  10. private static readonly W3cColorNameResolver W3c = new();
  11. private static readonly MultiStandardColorNameResolver Multi = new();
  12. /// <summary>
  13. /// Gets the W3C standard string for <paramref name="color"/>.
  14. /// </summary>
  15. /// <param name="color">The color.</param>
  16. /// <returns><see langword="null"/> if there is no standard color name for the specified color.</returns>
  17. public static string? GetW3CColorName (Color color)
  18. {
  19. if (W3c.TryNameColor (color, out string? name))
  20. {
  21. return name;
  22. }
  23. return null;
  24. }
  25. /// <summary>
  26. /// Gets the ANSI 4-bit (16) color name for <paramref name="color"/>.
  27. /// </summary>
  28. /// <param name="color">The color.</param>
  29. /// <returns><see langword="null"/> if there is no standard color name for the specified color.</returns>
  30. public static string? GetANSIColor16Name (Color color)
  31. {
  32. if (Ansi.TryNameColor (color, out string? name))
  33. {
  34. return name;
  35. }
  36. return null;
  37. }
  38. /// <summary>
  39. /// Gets backwards compatible color name for <paramref name="color"/>.
  40. /// </summary>
  41. /// <param name="color">The color.</param>
  42. /// <returns>Standard color name for the specified color; otherwise <see langword="null"/>.</returns>
  43. public static string? GetColorName (Color color)
  44. {
  45. if (Multi.TryNameColor (color, out string? name))
  46. {
  47. return name;
  48. }
  49. return null;
  50. }
  51. /// <summary>
  52. /// Returns the list of W3C standard color names.
  53. /// </summary>
  54. /// <returns></returns>
  55. public static IEnumerable<string> GetW3CColorNames ()
  56. {
  57. return W3c.GetColorNames ();
  58. }
  59. /// <summary>
  60. /// Parses <paramref name="name"/> and returns <paramref name="color"/> if name is a W3C standard named color.
  61. /// </summary>
  62. /// <param name="name">The name to parse.</param>
  63. /// <param name="color">If successful, the color.</param>
  64. /// <returns><see langword="true"/> if <paramref name="name"/> was parsed successfully.</returns>
  65. public static bool TryParseW3CColorName (ReadOnlySpan<char> name, out Color color)
  66. {
  67. if (W3c.TryParseColor (name, out color))
  68. {
  69. return true;
  70. }
  71. // Backwards compatibility: Also parse #RRGGBB.
  72. return TryParseHexColor (name, out color);
  73. }
  74. /// <summary>
  75. /// Parses <paramref name="name"/> and returns <paramref name="color"/> if name is a ANSI 4-bit standard named color.
  76. /// </summary>
  77. /// <param name="name">The name to parse.</param>
  78. /// <param name="color">If successful, the color.</param>
  79. /// <returns><see langword="true"/> if <paramref name="name"/> was parsed successfully.</returns>
  80. public static bool TryParseColor16 (ReadOnlySpan<char> name, out Color color)
  81. {
  82. if (Ansi.TryParseColor (name, out color))
  83. {
  84. return true;
  85. }
  86. color = default;
  87. return false;
  88. }
  89. /// <summary>
  90. /// Parses <paramref name="name"/> and returns <paramref name="color"/> if name is either ANSI 4-bit or W3C standard named color.
  91. /// </summary>
  92. /// <param name="name">The name to parse.</param>
  93. /// <param name="color">If successful, the color.</param>
  94. /// <returns><see langword="true"/> if <paramref name="name"/> was parsed successfully.</returns>
  95. public static bool TryParseNamedColor (ReadOnlySpan<char> name, out Color color)
  96. {
  97. if (Multi.TryParseColor (name, out color))
  98. {
  99. return true;
  100. }
  101. // Backwards compatibility: Also parse #RRGGBB.
  102. if (TryParseHexColor (name, out color))
  103. {
  104. return true;
  105. }
  106. color = default;
  107. return false;
  108. }
  109. private static bool TryParseHexColor (ReadOnlySpan<char> name, out Color color)
  110. {
  111. if (name.Length == 7 && name [0] == '#')
  112. {
  113. if (int.TryParse (name.Slice (1, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int r) &&
  114. int.TryParse (name.Slice (3, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int g) &&
  115. int.TryParse (name.Slice (5, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int b))
  116. {
  117. color = new Color (r, g, b);
  118. return true;
  119. }
  120. }
  121. color = default;
  122. return false;
  123. }
  124. }