2
0

Attribute.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #nullable enable
  2. using System.Numerics;
  3. using System.Text.Json.Serialization;
  4. namespace Terminal.Gui.Drawing;
  5. /// <summary>
  6. /// Represents the visual styling for a UI element, including foreground and background color and text style.
  7. /// </summary>
  8. /// <remarks>
  9. /// <para>
  10. /// <see cref="Attribute"/> is a lightweight, immutable struct used to define how visual elements are rendered
  11. /// in a terminal UI. It wraps color and style information in a platform-independent way and is used
  12. /// extensively in <see cref="Scheme"/>, <see cref="VisualRole"/>, and theming infrastructure.
  13. /// </para>
  14. /// </remarks>
  15. /// <seealso cref="Color"/>
  16. /// <seealso cref="TextStyle"/>
  17. /// <seealso cref="VisualRole"/>
  18. /// <seealso cref="Scheme"/>
  19. [JsonConverter (typeof (AttributeJsonConverter))]
  20. public readonly record struct Attribute : IEqualityOperators<Attribute, Attribute, bool>
  21. {
  22. /// <summary>Default empty attribute.</summary>
  23. [JsonIgnore]
  24. public static Attribute Default => new (Color.White, Color.Black);
  25. // TODO: Once CursesDriver is dead, remove this property
  26. /// <summary>INTERNAL: The <see cref="IConsoleDriver"/>-specific color value.</summary>
  27. [JsonIgnore (Condition = JsonIgnoreCondition.Always)]
  28. internal int PlatformColor { get; init; }
  29. /// <summary>
  30. /// Gets the foreground <see cref="Color"/> used to render text.
  31. /// </summary>
  32. [JsonConverter (typeof (ColorJsonConverter))]
  33. public Color Foreground { get; init; }
  34. /// <summary>
  35. /// Gets the background <see cref="Color"/> used behind text.
  36. /// </summary>
  37. [JsonConverter (typeof (ColorJsonConverter))]
  38. public Color Background { get; init; }
  39. // TODO: Add constructors which permit including a Style.
  40. /// <summary>
  41. /// Gets the <see cref="TextStyle"/> (e.g., bold, underline, italic) applied to text.
  42. /// </summary>
  43. public TextStyle Style { get; init; } = TextStyle.None;
  44. /// <summary>
  45. /// Initializes a new instance of the <see cref="Attribute"/> struct with default values.
  46. /// </summary>
  47. public Attribute () { this = Default with { PlatformColor = -1 }; }
  48. /// <summary>
  49. /// Initializes a new <see cref="Attribute"/> from an existing instance, preserving explicit state.
  50. /// </summary>
  51. public Attribute (in Attribute attr) { this = attr with { PlatformColor = -1 }; }
  52. /// <summary>INTERNAL: Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  53. /// <param name="platformColor">platform-dependent color value.</param>
  54. /// <param name="foreground">Foreground</param>
  55. /// <param name="background">Background</param>
  56. internal Attribute (in int platformColor, in Color foreground, in Color background)
  57. {
  58. Foreground = foreground;
  59. Background = background;
  60. PlatformColor = platformColor;
  61. Style = TextStyle.None;
  62. }
  63. /// <summary>
  64. /// Initializes an instance using two named colors.
  65. /// </summary>
  66. public Attribute (in Color foreground, in Color background)
  67. {
  68. Foreground = foreground;
  69. Background = background;
  70. // TODO: Once CursesDriver supports true color all the PlatformColor stuff goes away
  71. PlatformColor = Application.Driver?.MakeColor (in foreground, in background).PlatformColor ?? -1;
  72. Style = TextStyle.None;
  73. }
  74. /// <summary>
  75. /// Initializes a new instance with foreground, background, and <see cref="TextStyle"/>.
  76. /// </summary>
  77. public Attribute (in Color foreground, in Color background, in TextStyle style)
  78. {
  79. Foreground = foreground;
  80. Background = background;
  81. Style = style;
  82. // TODO: Once CursesDriver supports true color all the PlatformColor stuff goes away
  83. PlatformColor = Application.Driver?.MakeColor (in foreground, in background).PlatformColor ?? -1;
  84. }
  85. /// <summary>
  86. /// Initializes a new instance of the <see cref="Attribute"/> struct from string representations of colors and style.
  87. /// </summary>
  88. /// <param name="foreground">Foreground color as a string (name, hex, or rgb).</param>
  89. /// <param name="background">Background color as a string (name, hex, or rgb).</param>
  90. /// <param name="style">Optional style as a string (e.g., "Bold,Underline").</param>
  91. /// <exception cref="ArgumentException">Thrown if color parsing fails.</exception>
  92. public Attribute (in string foreground, in string background, in string? style = null)
  93. {
  94. Foreground = Color.Parse (foreground);
  95. Background = Color.Parse (background);
  96. Style = style is { } && Enum.TryParse (style, true, out TextStyle parsedStyle)
  97. ? parsedStyle
  98. : TextStyle.None;
  99. PlatformColor = Application.Driver?.MakeColor (Foreground, Background).PlatformColor ?? -1;
  100. }
  101. /// <summary>
  102. /// Initializes a new instance of the <see cref="Attribute"/> struct from string representations of colors and style.
  103. /// </summary>
  104. /// <param name="foreground">Foreground color as a string (name, hex, or rgb).</param>
  105. /// <param name="background">Background color as a string (name, hex, or rgb).</param>
  106. /// <param name="style">Optional style as a string (e.g., "Bold,Underline").</param>
  107. /// <exception cref="ArgumentException">Thrown if color parsing fails.</exception>
  108. public Attribute (in string foreground, in string background, in TextStyle style)
  109. {
  110. Foreground = Color.Parse (foreground);
  111. Background = Color.Parse (background);
  112. Style = style;
  113. PlatformColor = Application.Driver?.MakeColor (Foreground, Background).PlatformColor ?? -1;
  114. }
  115. /// <summary>
  116. /// INTERNAL: Initializes a new instance with a <see cref="ColorName16"/> value. Both <see cref="Foreground"/> and
  117. /// <see cref="Background"/> will be set to the specified color.
  118. /// </summary>
  119. /// <param name="color16Name">Value.</param>
  120. internal Attribute (in ColorName16 color16Name) : this (in color16Name, in color16Name) { }
  121. /// <summary>
  122. /// Initializes a new instance with foreground and background colors.
  123. /// </summary>
  124. public Attribute (in ColorName16 foreground16Name, in ColorName16 background16Name)
  125. : this (new Color (in foreground16Name), new Color (in background16Name))
  126. { }
  127. /// <summary>
  128. /// Initializes a new instance with foreground and background colors.
  129. /// </summary>
  130. public Attribute (in ColorName16 foreground16Name, in Color background) : this (new Color (in foreground16Name), in background) { }
  131. /// <summary>
  132. /// Initializes a new instance with foreground and background colors.
  133. /// </summary>
  134. public Attribute (in Color foreground, in ColorName16 background16Name) : this (in foreground, new Color (in background16Name)) { }
  135. /// <summary>
  136. /// INTERNAL: Initializes a new instance with a <see cref="StandardColors"/> value. Both <see cref="Foreground"/> and
  137. /// <see cref="Background"/> will be set to the specified color.
  138. /// </summary>
  139. /// <param name="standardColor">Value.</param>
  140. internal Attribute (in StandardColor standardColor) : this (in standardColor, in standardColor) { }
  141. /// <summary>
  142. /// Initializes a new instance with foreground and background colors.
  143. /// </summary>
  144. public Attribute (in StandardColor foreground, in StandardColor background)
  145. : this (new Color (in foreground), new Color (in background))
  146. { }
  147. /// <summary>
  148. /// Initializes a new instance with foreground and background colors.
  149. /// </summary>
  150. public Attribute (in StandardColor foreground, in Color background) : this (new Color (in foreground), in background) { }
  151. /// <summary>
  152. /// Initializes a new instance with foreground and background colors.
  153. /// </summary>
  154. public Attribute (in Color foreground, in StandardColor background) : this (in foreground, new Color (in background)) { }
  155. /// <summary>
  156. /// Initializes an instance using a single color for both foreground and background.
  157. /// </summary>
  158. public Attribute (in Color color) : this (color, color) { }
  159. /// <summary>
  160. /// Initializes a new instance with foreground and background colors and a <see cref="TextStyle"/>.
  161. /// </summary>
  162. public Attribute (in StandardColor foreground, in StandardColor background, in TextStyle style) : this (new (in foreground), new Color (in background), style) { }
  163. /// <inheritdoc/>
  164. public bool Equals (Attribute other)
  165. {
  166. return PlatformColor == other.PlatformColor
  167. && Foreground.Equals (other.Foreground)
  168. && Background.Equals (other.Background)
  169. && Style == other.Style;
  170. }
  171. /// <inheritdoc/>
  172. public override int GetHashCode () { return HashCode.Combine (PlatformColor, Foreground, Background, Style); }
  173. /// <inheritdoc/>
  174. public override string ToString ()
  175. {
  176. // Note: Unit tests are dependent on this format
  177. return $"[{Foreground},{Background},{Style}]";
  178. }
  179. }