Attribute.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. /// <summary>
  26. /// Gets the foreground <see cref="Color"/> used to render text.
  27. /// </summary>
  28. [JsonConverter (typeof (ColorJsonConverter))]
  29. public Color Foreground { get; init; }
  30. /// <summary>
  31. /// Gets the background <see cref="Color"/> used behind text.
  32. /// </summary>
  33. [JsonConverter (typeof (ColorJsonConverter))]
  34. public Color Background { get; init; }
  35. // TODO: Add constructors which permit including a Style.
  36. /// <summary>
  37. /// Gets the <see cref="TextStyle"/> (e.g., bold, underline, italic) applied to text.
  38. /// </summary>
  39. public TextStyle Style { get; init; } = TextStyle.None;
  40. /// <summary>
  41. /// Initializes a new instance of the <see cref="Attribute"/> struct with default values.
  42. /// </summary>
  43. public Attribute () { this = Default; }
  44. /// <summary>
  45. /// Initializes a new <see cref="Attribute"/> from an existing instance, preserving explicit state.
  46. /// </summary>
  47. public Attribute (in Attribute attr) { this = attr; }
  48. /// <summary>
  49. /// Initializes an instance using two named colors.
  50. /// </summary>
  51. public Attribute (in Color foreground, in Color background)
  52. {
  53. Foreground = foreground;
  54. Background = background;
  55. Style = TextStyle.None;
  56. }
  57. /// <summary>
  58. /// Initializes a new instance with foreground, background, and <see cref="TextStyle"/>.
  59. /// </summary>
  60. public Attribute (in Color foreground, in Color background, in TextStyle style)
  61. {
  62. Foreground = foreground;
  63. Background = background;
  64. Style = style;
  65. }
  66. /// <summary>
  67. /// Initializes a new instance of the <see cref="Attribute"/> struct from string representations of colors and style.
  68. /// </summary>
  69. /// <param name="foreground">Foreground color as a string (name, hex, or rgb).</param>
  70. /// <param name="background">Background color as a string (name, hex, or rgb).</param>
  71. /// <param name="style">Optional style as a string (e.g., "Bold,Underline").</param>
  72. /// <exception cref="ArgumentException">Thrown if color parsing fails.</exception>
  73. public Attribute (in string foreground, in string background, in string? style = null)
  74. {
  75. Foreground = Color.Parse (foreground);
  76. Background = Color.Parse (background);
  77. Style = style is { } && Enum.TryParse (style, true, out TextStyle parsedStyle)
  78. ? parsedStyle
  79. : TextStyle.None;
  80. }
  81. /// <summary>
  82. /// Initializes a new instance of the <see cref="Attribute"/> struct from string representations of colors and style.
  83. /// </summary>
  84. /// <param name="foreground">Foreground color as a string (name, hex, or rgb).</param>
  85. /// <param name="background">Background color as a string (name, hex, or rgb).</param>
  86. /// <param name="style">Optional style as a string (e.g., "Bold,Underline").</param>
  87. /// <exception cref="ArgumentException">Thrown if color parsing fails.</exception>
  88. public Attribute (in string foreground, in string background, in TextStyle style)
  89. {
  90. Foreground = Color.Parse (foreground);
  91. Background = Color.Parse (background);
  92. Style = style;
  93. }
  94. /// <summary>
  95. /// INTERNAL: Initializes a new instance with a <see cref="ColorName16"/> value. Both <see cref="Foreground"/> and
  96. /// <see cref="Background"/> will be set to the specified color.
  97. /// </summary>
  98. /// <param name="color16Name">Value.</param>
  99. internal Attribute (in ColorName16 color16Name) : this (in color16Name, in color16Name) { }
  100. /// <summary>
  101. /// Initializes a new instance with foreground and background colors.
  102. /// </summary>
  103. public Attribute (in ColorName16 foreground16Name, in ColorName16 background16Name)
  104. : this (new Color (in foreground16Name), new Color (in background16Name))
  105. { }
  106. /// <summary>
  107. /// Initializes a new instance with foreground and background colors.
  108. /// </summary>
  109. public Attribute (in ColorName16 foreground16Name, in Color background) : this (new Color (in foreground16Name), in background) { }
  110. /// <summary>
  111. /// Initializes a new instance with foreground and background colors.
  112. /// </summary>
  113. public Attribute (in Color foreground, in ColorName16 background16Name) : this (in foreground, new Color (in background16Name)) { }
  114. /// <summary>
  115. /// INTERNAL: Initializes a new instance with a <see cref="StandardColors"/> value. Both <see cref="Foreground"/> and
  116. /// <see cref="Background"/> will be set to the specified color.
  117. /// </summary>
  118. /// <param name="standardColor">Value.</param>
  119. internal Attribute (in StandardColor standardColor) : this (in standardColor, in standardColor) { }
  120. /// <summary>
  121. /// Initializes a new instance with foreground and background colors.
  122. /// </summary>
  123. public Attribute (in StandardColor foreground, in StandardColor background)
  124. : this (new Color (in foreground), new Color (in background))
  125. { }
  126. /// <summary>
  127. /// Initializes a new instance with foreground and background colors.
  128. /// </summary>
  129. public Attribute (in StandardColor foreground, in Color background) : this (new Color (in foreground), in background) { }
  130. /// <summary>
  131. /// Initializes a new instance with foreground and background colors.
  132. /// </summary>
  133. public Attribute (in Color foreground, in StandardColor background) : this (in foreground, new Color (in background)) { }
  134. /// <summary>
  135. /// Initializes an instance using a single color for both foreground and background.
  136. /// </summary>
  137. public Attribute (in Color color) : this (color, color) { }
  138. /// <summary>
  139. /// Initializes a new instance with foreground and background colors and a <see cref="TextStyle"/>.
  140. /// </summary>
  141. public Attribute (in StandardColor foreground, in StandardColor background, in TextStyle style) : this (
  142. new (in foreground),
  143. new Color (in background),
  144. style)
  145. { }
  146. /// <inheritdoc/>
  147. public bool Equals (Attribute other)
  148. {
  149. return Foreground.Equals (other.Foreground)
  150. && Background.Equals (other.Background)
  151. && Style == other.Style;
  152. }
  153. /// <inheritdoc/>
  154. public override int GetHashCode () { return HashCode.Combine (Foreground, Background, Style); }
  155. /// <inheritdoc/>
  156. public override string ToString ()
  157. {
  158. // Note: Unit tests are dependent on this format
  159. return $"[{Foreground},{Background},{Style}]";
  160. }
  161. }