ColorStrings.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. /// <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. // Fetch the color name from the resource file
  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. foreach (DictionaryEntry entry in GlobalResources.GetResourceSet (
  29. CultureInfo.CurrentUICulture,
  30. true,
  31. true,
  32. e =>
  33. {
  34. string keyName = e.Key.ToString () ?? string.Empty;
  35. return e.Value is string && keyName.StartsWith ('#');
  36. })!)
  37. {
  38. yield return (entry.Value as string)!;
  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. // Iterate through all resource entries to find the matching color name
  50. foreach (DictionaryEntry entry in GlobalResources.GetResourceSet (CultureInfo.CurrentUICulture, true, true)!)
  51. {
  52. if (entry.Value is string colorName && colorName.Equals (name, StringComparison.OrdinalIgnoreCase))
  53. {
  54. // Parse the key to extract the color components
  55. string key = entry.Key.ToString () ?? string.Empty;
  56. if (key.StartsWith ("#") && key.Length == 7)
  57. {
  58. if (int.TryParse (key.Substring (1, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int r)
  59. && int.TryParse (key.Substring (3, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int g)
  60. && int.TryParse (key.Substring (5, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int b))
  61. {
  62. color = new (r, g, b);
  63. return true;
  64. }
  65. }
  66. }
  67. }
  68. color = default (Color);
  69. return false;
  70. }
  71. }