ColorStrings.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #nullable enable
  2. using System.Collections;
  3. using System.Globalization;
  4. using Terminal.Gui.Resources;
  5. namespace Terminal.Gui;
  6. /// <summary>
  7. /// Provides a mapping between <see cref="Color"/> and the W3C standard color name strings.
  8. /// </summary>
  9. public static class ColorStrings
  10. {
  11. /// <summary>
  12. /// Gets the W3C standard string for <paramref name="color"/>.
  13. /// </summary>
  14. /// <param name="color">The color.</param>
  15. /// <returns><see langword="null"/> if there is no standard color name for the specified color.</returns>
  16. public static string? GetW3CColorName (Color color)
  17. {
  18. // Fetch the color name from the resource file
  19. return GlobalResources.GetString ($"#{color.R:X2}{color.G:X2}{color.B:X2}", CultureInfo.CurrentUICulture);
  20. }
  21. /// <summary>
  22. /// Returns the list of W3C standard color names.
  23. /// </summary>
  24. /// <returns></returns>
  25. public static IEnumerable<string> GetW3CColorNames ()
  26. {
  27. foreach (DictionaryEntry entry in GlobalResources.GetResourceSet (
  28. CultureInfo.CurrentUICulture,
  29. true,
  30. true,
  31. e =>
  32. {
  33. string keyName = e.Key.ToString () ?? string.Empty;
  34. return e.Value is string && keyName.StartsWith ('#');
  35. })!)
  36. {
  37. yield return (entry.Value as string)!;
  38. }
  39. }
  40. /// <summary>
  41. /// Parses <paramref name="name"/> and returns <paramref name="color"/> if name is a W3C standard named color.
  42. /// </summary>
  43. /// <param name="name">The name to parse.</param>
  44. /// <param name="color">If successful, the color.</param>
  45. /// <returns><see langword="true"/> if <paramref name="name"/> was parsed successfully.</returns>
  46. public static bool TryParseW3CColorName (string name, out Color color)
  47. {
  48. // Iterate through all resource entries to find the matching color name
  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. // Parse the key to extract the color components
  54. string key = entry.Key.ToString () ?? string.Empty;
  55. if (key.StartsWith ("#") && key.Length == 7)
  56. {
  57. if (int.TryParse (key.Substring (1, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int r)
  58. && int.TryParse (key.Substring (3, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int g)
  59. && int.TryParse (key.Substring (5, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int b))
  60. {
  61. color = new (r, g, b);
  62. return true;
  63. }
  64. }
  65. }
  66. }
  67. color = default (Color);
  68. return false;
  69. }
  70. }