ColorStrings.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System.Globalization;
  2. namespace Terminal.Gui.Drawing;
  3. /// <summary>
  4. /// Provides a mapping between <see cref="Color"/> and the W3C standard color name strings.
  5. /// </summary>
  6. public static class ColorStrings
  7. {
  8. private static readonly StandardColorsNameResolver _standard = new();
  9. /// <summary>
  10. /// Gets the color name for <paramref name="color"/>.
  11. /// </summary>
  12. /// <param name="color">The color.</param>
  13. /// <returns>Standard color name for the specified color; otherwise <see langword="null"/>.</returns>
  14. public static string? GetColorName (Color color)
  15. {
  16. if (_standard.TryNameColor (color, out string? name))
  17. {
  18. return name;
  19. }
  20. return null;
  21. }
  22. /// <summary>
  23. /// Returns the list of W3C+ standard color names.
  24. /// </summary>
  25. /// <returns></returns>
  26. public static IEnumerable<string> GetStandardColorNames ()
  27. {
  28. return _standard.GetColorNames ();
  29. }
  30. /// <summary>
  31. /// Parses <paramref name="name"/> and returns <paramref name="color"/> if name is a W3C+ standard named color.
  32. /// </summary>
  33. /// <param name="name">The name to parse.</param>
  34. /// <param name="color">If successful, the color.</param>
  35. /// <returns><see langword="true"/> if <paramref name="name"/> was parsed successfully.</returns>
  36. public static bool TryParseStandardColorName (ReadOnlySpan<char> name, out Color color)
  37. {
  38. if (_standard.TryParseColor (name, out color))
  39. {
  40. return true;
  41. }
  42. // Backwards compatibility: Also parse #RRGGBB.
  43. return TryParseHexColor (name, out color);
  44. }
  45. /// <summary>
  46. /// Parses <paramref name="name"/> and returns <paramref name="color"/> if name is a W3C+ standard named color.
  47. /// </summary>
  48. /// <param name="name">The name to parse.</param>
  49. /// <param name="color">If successful, the color.</param>
  50. /// <returns><see langword="true"/> if <paramref name="name"/> was parsed successfully.</returns>
  51. public static bool TryParseNamedColor (ReadOnlySpan<char> name, out Color color)
  52. {
  53. if (_standard.TryParseColor (name, out color))
  54. {
  55. return true;
  56. }
  57. // Backwards compatibility: Also parse #RRGGBB.
  58. if (TryParseHexColor (name, out color))
  59. {
  60. return true;
  61. }
  62. color = default (Color);
  63. return false;
  64. }
  65. private static bool TryParseHexColor (ReadOnlySpan<char> name, out Color color)
  66. {
  67. if (name.Length == 7 && name [0] == '#')
  68. {
  69. if (int.TryParse (name.Slice (1, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int r) &&
  70. int.TryParse (name.Slice (3, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int g) &&
  71. int.TryParse (name.Slice (5, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int b))
  72. {
  73. color = new Color (r, g, b);
  74. return true;
  75. }
  76. }
  77. color = default (Color);
  78. return false;
  79. }
  80. }