ColorStrings.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. // PERFORMANCE: See https://stackoverflow.com/a/15521524/297526 for why GlobalResources.GetString is fast.
  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. return GlobalResources.GetString ($"#{color.R:X2}{color.G:X2}{color.B:X2}", CultureInfo.CurrentUICulture);
  21. }
  22. /// <summary>
  23. /// Returns the list of W3C standard color names.
  24. /// </summary>
  25. /// <returns></returns>
  26. public static IEnumerable<string> GetW3CColorNames ()
  27. {
  28. ResourceSet? resourceSet = GlobalResources.GetResourceSet (CultureInfo.CurrentUICulture, true, true);
  29. if (resourceSet == null)
  30. {
  31. yield break;
  32. }
  33. foreach (DictionaryEntry entry in resourceSet)
  34. {
  35. if (entry is { Value: string colorName, Key: string key } && key.StartsWith ('#'))
  36. {
  37. yield return colorName;
  38. }
  39. }
  40. }
  41. /// <summary>
  42. /// Parses <paramref name="name"/> and returns <paramref name="color"/> if name is a W3C standard named color.
  43. /// </summary>
  44. /// <param name="name">The name to parse.</param>
  45. /// <param name="color">If successful, the color.</param>
  46. /// <returns><see langword="true"/> if <paramref name="name"/> was parsed successfully.</returns>
  47. public static bool TryParseW3CColorName (string name, out Color color)
  48. {
  49. foreach (DictionaryEntry entry in GlobalResources.GetResourceSet (CultureInfo.CurrentUICulture, true, true)!)
  50. {
  51. if (entry.Value is string colorName && colorName.Equals (name, StringComparison.OrdinalIgnoreCase))
  52. {
  53. return TryParseColorKey (entry.Key.ToString (), out color);
  54. }
  55. }
  56. return TryParseColorKey (name, out color);
  57. bool TryParseColorKey (string? key, out Color color)
  58. {
  59. if (key != null && key.StartsWith ('#') && key.Length == 7)
  60. {
  61. if (int.TryParse (key.AsSpan (1, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int r) &&
  62. int.TryParse (key.AsSpan (3, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int g) &&
  63. int.TryParse (key.AsSpan (5, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int b))
  64. {
  65. color = new Color (r, g, b);
  66. return true;
  67. }
  68. }
  69. color = default (Color);
  70. return false;
  71. }
  72. }
  73. }