Attribute.cs 7.8 KB

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