Attribute.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #nullable enable
  2. using System.Numerics;
  3. using System.Text.Json.Serialization;
  4. namespace Terminal.Gui;
  5. // TODO: Add support for other attributes (bold, underline, etc.) once the platform drivers support them.
  6. // TODO: See https://github.com/gui-cs/Terminal.Gui/issues/457
  7. /// <summary>Attributes represent how text is styled when displayed in the terminal.</summary>
  8. /// <remarks>
  9. /// <see cref="Attribute"/> provides a platform independent representation of colors (and someday other forms of
  10. /// text styling). They encode both the foreground and the background color and are used in the
  11. /// <see cref="ColorScheme"/> class to define color schemes that can be used in an application.
  12. /// </remarks>
  13. [JsonConverter (typeof (AttributeJsonConverter))]
  14. public readonly record struct Attribute : IEqualityOperators<Attribute, Attribute, bool>
  15. {
  16. /// <summary>Default empty attribute.</summary>
  17. [JsonIgnore]
  18. public static Attribute Default => new (Color.White, Color.Black);
  19. /// <summary>The <see cref="IConsoleDriver"/>-specific color value.</summary>
  20. [JsonIgnore (Condition = JsonIgnoreCondition.Always)]
  21. internal int PlatformColor { get; init; }
  22. /// <summary>The foreground color.</summary>
  23. [JsonConverter (typeof (ColorJsonConverter))]
  24. public Color Foreground { get; }
  25. /// <summary>The background color.</summary>
  26. [JsonConverter (typeof (ColorJsonConverter))]
  27. public Color Background { get; }
  28. /// <summary>Initializes a new instance with default values.</summary>
  29. public Attribute ()
  30. {
  31. this = Default with { PlatformColor = -1 };
  32. }
  33. /// <summary>Initializes a new instance from an existing instance.</summary>
  34. public Attribute (in Attribute attr)
  35. {
  36. this = attr with { PlatformColor = -1 };
  37. }
  38. /// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  39. /// <param name="platformColor">platform-dependent color value.</param>
  40. /// <param name="foreground">Foreground</param>
  41. /// <param name="background">Background</param>
  42. internal Attribute (int platformColor, Color foreground, Color background)
  43. {
  44. Foreground = foreground;
  45. Background = background;
  46. PlatformColor = platformColor;
  47. }
  48. /// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  49. /// <param name="foreground">Foreground</param>
  50. /// <param name="background">Background</param>
  51. public Attribute (in Color foreground, in Color background)
  52. {
  53. Foreground = foreground;
  54. Background = background;
  55. // TODO: Once CursesDriver supports true color all the PlatformColor stuff goes away
  56. PlatformColor = Application.Driver?.MakeColor(in foreground, in background).PlatformColor ?? -1;
  57. }
  58. /// <summary>
  59. /// Initializes a new instance with a <see cref="ColorName16"/> value. Both <see cref="Foreground"/> and
  60. /// <see cref="Background"/> will be set to the specified color.
  61. /// </summary>
  62. /// <param name="colorName">Value.</param>
  63. internal Attribute (in ColorName16 colorName) : this (in colorName, in colorName) { }
  64. /// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  65. /// <param name="foregroundName">Foreground</param>
  66. /// <param name="backgroundName">Background</param>
  67. public Attribute (in ColorName16 foregroundName, in ColorName16 backgroundName)
  68. : this (new Color (in foregroundName), new Color (in backgroundName))
  69. { }
  70. /// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  71. /// <param name="foregroundName">Foreground</param>
  72. /// <param name="background">Background</param>
  73. public Attribute (in ColorName16 foregroundName, in Color background) : this (new Color (in foregroundName), in background) { }
  74. /// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  75. /// <param name="foreground">Foreground</param>
  76. /// <param name="backgroundName">Background</param>
  77. public Attribute (in Color foreground, in ColorName16 backgroundName) : this (in foreground, new Color (in backgroundName)) { }
  78. /// <summary>
  79. /// Initializes a new instance of the <see cref="Attribute"/> struct with the same colors for the foreground and
  80. /// background.
  81. /// </summary>
  82. /// <param name="color">The color.</param>
  83. public Attribute (in Color color) : this (color, color) { }
  84. /// <inheritdoc/>
  85. public override int GetHashCode () { return HashCode.Combine (PlatformColor, Foreground, Background); }
  86. /// <inheritdoc/>
  87. public override string ToString ()
  88. {
  89. // Note: Unit tests are dependent on this format
  90. return $"[{Foreground},{Background}]";
  91. }
  92. }