Attribute.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #nullable enable
  2. using System.Numerics;
  3. using System.Text.Json.Serialization;
  4. namespace Terminal.Gui;
  5. /// <summary>Attributes represent how text is styled when displayed in the terminal.</summary>
  6. /// <remarks>
  7. /// <see cref="Attribute"/> provides a platform independent representation of colors (and someday other forms of
  8. /// text styling). They encode both the foreground and the background color and are used in the
  9. /// <see cref="ColorScheme"/> class to define color schemes that can be used in an application.
  10. /// </remarks>
  11. [JsonConverter (typeof (AttributeJsonConverter))]
  12. public readonly struct Attribute : IEquatable<Attribute>, IEqualityOperators<Attribute, Attribute, bool>
  13. {
  14. /// <summary>Default empty attribute.</summary>
  15. public static readonly Attribute Default = new (Color.White, ColorName.Black);
  16. /// <summary>The <see cref="ConsoleDriver"/>-specific color value.</summary>
  17. [JsonIgnore (Condition = JsonIgnoreCondition.Always)]
  18. internal int PlatformColor { get; }
  19. /// <summary>The foreground color.</summary>
  20. [JsonConverter (typeof (ColorJsonConverter))]
  21. public Color Foreground { get; }
  22. /// <summary>The background color.</summary>
  23. [JsonConverter (typeof (ColorJsonConverter))]
  24. public Color Background { get; }
  25. /// <summary>Initializes a new instance with default values.</summary>
  26. public Attribute ()
  27. {
  28. PlatformColor = -1;
  29. Foreground = Default.Foreground;
  30. Background = Default.Background;
  31. }
  32. /// <summary>Initializes a new instance from an existing instance.</summary>
  33. public Attribute (in Attribute attr)
  34. {
  35. PlatformColor = -1;
  36. Foreground = attr.Foreground;
  37. Background = attr.Background;
  38. }
  39. /// <summary>Initializes a new instance with platform specific color value.</summary>
  40. /// <param name="platformColor">Value.</param>
  41. internal Attribute (int platformColor)
  42. {
  43. PlatformColor = platformColor;
  44. Foreground = Default.Foreground;
  45. Background = Default.Background;
  46. }
  47. /// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  48. /// <param name="platformColor">platform-dependent color value.</param>
  49. /// <param name="foreground">Foreground</param>
  50. /// <param name="background">Background</param>
  51. internal Attribute (int platformColor, in Color foreground, in Color background)
  52. {
  53. Foreground = foreground;
  54. Background = background;
  55. PlatformColor = platformColor;
  56. }
  57. /// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  58. /// <param name="foreground">Foreground</param>
  59. /// <param name="background">Background</param>
  60. public Attribute (in Color foreground, in Color background)
  61. {
  62. Foreground = foreground;
  63. Background = background;
  64. // TODO: Once CursesDriver supports truecolor all the PlatformColor stuff goes away
  65. if (Application.Driver is null)
  66. {
  67. PlatformColor = -1;
  68. return;
  69. }
  70. Attribute make = Application.Driver.MakeColor (foreground, background);
  71. PlatformColor = make.PlatformColor;
  72. }
  73. /// <summary>
  74. /// Initializes a new instance with a <see cref="ColorName"/> value. Both <see cref="Foreground"/> and
  75. /// <see cref="Background"/> will be set to the specified color.
  76. /// </summary>
  77. /// <param name="colorName">Value.</param>
  78. internal Attribute (ColorName colorName) : this (colorName, colorName) { }
  79. /// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  80. /// <param name="foregroundName">Foreground</param>
  81. /// <param name="backgroundName">Background</param>
  82. public Attribute (in ColorName foregroundName, in ColorName backgroundName)
  83. : this (new Color (in foregroundName), new Color (in backgroundName))
  84. { }
  85. /// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  86. /// <param name="foregroundName">Foreground</param>
  87. /// <param name="background">Background</param>
  88. public Attribute (ColorName foregroundName, Color background) : this (new Color (foregroundName), background) { }
  89. /// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  90. /// <param name="foreground">Foreground</param>
  91. /// <param name="backgroundName">Background</param>
  92. public Attribute (Color foreground, ColorName backgroundName) : this (foreground, new Color (backgroundName)) { }
  93. /// <summary>
  94. /// Initializes a new instance of the <see cref="Attribute"/> struct with the same colors for the foreground and
  95. /// background.
  96. /// </summary>
  97. /// <param name="color">The color.</param>
  98. public Attribute (Color color) : this (color, color) { }
  99. /// <summary>Compares two attributes for equality.</summary>
  100. /// <param name="left"></param>
  101. /// <param name="right"></param>
  102. /// <returns></returns>
  103. public static bool operator == (Attribute left, Attribute right) { return left.Equals (right); }
  104. /// <summary>Compares two attributes for inequality.</summary>
  105. /// <param name="left"></param>
  106. /// <param name="right"></param>
  107. /// <returns></returns>
  108. public static bool operator != (Attribute left, Attribute right) { return !(left == right); }
  109. /// <inheritdoc/>
  110. public override bool Equals (object? obj) { return obj is Attribute other && Equals (other); }
  111. /// <inheritdoc/>
  112. public bool Equals (Attribute other) { return PlatformColor == other.PlatformColor && Foreground == other.Foreground && Background == other.Background; }
  113. /// <inheritdoc/>
  114. public override int GetHashCode () { return HashCode.Combine (PlatformColor, Foreground, Background); }
  115. /// <inheritdoc/>
  116. public override string ToString ()
  117. {
  118. // Note: Unit tests are dependent on this format
  119. return $"[{Foreground},{Background}]";
  120. }
  121. }