Attribute.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 record struct 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, Color foreground, 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 true color all the PlatformColor stuff goes away
  65. PlatformColor = Application.Driver?.MakeColor(in foreground, in background).PlatformColor ?? -1;
  66. }
  67. /// <summary>
  68. /// Initializes a new instance with a <see cref="ColorName"/> value. Both <see cref="Foreground"/> and
  69. /// <see cref="Background"/> will be set to the specified color.
  70. /// </summary>
  71. /// <param name="colorName">Value.</param>
  72. internal Attribute (in ColorName colorName) : this (in colorName, in colorName) { }
  73. /// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  74. /// <param name="foregroundName">Foreground</param>
  75. /// <param name="backgroundName">Background</param>
  76. public Attribute (in ColorName foregroundName, in ColorName backgroundName)
  77. : this (new Color (in foregroundName), new Color (in backgroundName))
  78. { }
  79. /// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  80. /// <param name="foregroundName">Foreground</param>
  81. /// <param name="background">Background</param>
  82. public Attribute (in ColorName foregroundName, in Color background) : this (new Color (foregroundName), in background) { }
  83. /// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  84. /// <param name="foreground">Foreground</param>
  85. /// <param name="backgroundName">Background</param>
  86. public Attribute (in Color foreground, in ColorName backgroundName) : this (foreground, new Color (backgroundName)) { }
  87. /// <summary>
  88. /// Initializes a new instance of the <see cref="Attribute"/> struct with the same colors for the foreground and
  89. /// background.
  90. /// </summary>
  91. /// <param name="color">The color.</param>
  92. public Attribute (in Color color) : this (color, color) { }
  93. /// <inheritdoc/>
  94. public bool Equals (Attribute other) { return PlatformColor == other.PlatformColor && Foreground == other.Foreground && Background == other.Background; }
  95. /// <inheritdoc/>
  96. public override int GetHashCode () { return HashCode.Combine (PlatformColor, Foreground, Background); }
  97. /// <inheritdoc/>
  98. public override string ToString ()
  99. {
  100. // Note: Unit tests are dependent on this format
  101. return $"[{Foreground},{Background}]";
  102. }
  103. }