Attribute.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #nullable enable
  2. using System.Numerics;
  3. using System.Text.Json.Serialization;
  4. namespace Terminal.Gui;
  5. // TODO: Add support for other attributes (bold, underline, etc.) once the platform drivers support them.
  6. // TODO: See https://github.com/gui-cs/Terminal.Gui/issues/457
  7. /// <summary>Attributes represent how text is styled when displayed in the terminal.</summary>
  8. /// <remarks>
  9. /// <see cref="Attribute"/> provides a platform independent representation of colors (and someday other forms of
  10. /// text styling). They encode both the foreground and the background color and are used in the
  11. /// <see cref="ColorScheme"/> class to define color schemes that can be used in an application.
  12. /// </remarks>
  13. [JsonConverter (typeof (AttributeJsonConverter))]
  14. public readonly record struct Attribute : IEqualityOperators<Attribute, Attribute, bool>
  15. {
  16. /// <summary>Default empty attribute.</summary>
  17. [JsonIgnore]
  18. public static Attribute Default => new (Color.White, Color.Black);
  19. /// <summary>The <see cref="IConsoleDriver"/>-specific color value.</summary>
  20. [JsonIgnore (Condition = JsonIgnoreCondition.Always)]
  21. internal int PlatformColor { get; init; }
  22. /// <summary>The foreground color.</summary>
  23. [JsonConverter (typeof (ColorJsonConverter))]
  24. public Color Foreground { get; }
  25. /// <summary>The background color.</summary>
  26. [JsonConverter (typeof (ColorJsonConverter))]
  27. public Color Background { get; }
  28. // TODO: Add constructors which permit including a TextStyle.
  29. /// <summary>The text style (bold, italic, underlined, etc.).</summary>
  30. public TextStyle TextStyle { get; init; } = TextStyle.None;
  31. /// <summary>Initializes a new instance with default values.</summary>
  32. public Attribute ()
  33. {
  34. this = Default with { PlatformColor = -1 };
  35. }
  36. /// <summary>Initializes a new instance from an existing instance.</summary>
  37. public Attribute (in Attribute attr)
  38. {
  39. this = attr with { PlatformColor = -1 };
  40. }
  41. /// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  42. /// <param name="platformColor">platform-dependent color value.</param>
  43. /// <param name="foreground">Foreground</param>
  44. /// <param name="background">Background</param>
  45. internal Attribute (int platformColor, Color foreground, Color background)
  46. {
  47. Foreground = foreground;
  48. Background = background;
  49. PlatformColor = platformColor;
  50. }
  51. /// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  52. /// <param name="foreground">Foreground</param>
  53. /// <param name="background">Background</param>
  54. public Attribute (in Color foreground, in Color background)
  55. {
  56. Foreground = foreground;
  57. Background = background;
  58. // TODO: Once CursesDriver supports true color all the PlatformColor stuff goes away
  59. PlatformColor = Application.Driver?.MakeColor(in foreground, in background).PlatformColor ?? -1;
  60. }
  61. /// <summary>
  62. /// Initializes a new instance with a <see cref="ColorName16"/> value. Both <see cref="Foreground"/> and
  63. /// <see cref="Background"/> will be set to the specified color.
  64. /// </summary>
  65. /// <param name="colorName">Value.</param>
  66. internal Attribute (in ColorName16 colorName) : this (in colorName, in colorName) { }
  67. /// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  68. /// <param name="foregroundName">Foreground</param>
  69. /// <param name="backgroundName">Background</param>
  70. public Attribute (in ColorName16 foregroundName, in ColorName16 backgroundName)
  71. : this (new Color (in foregroundName), new Color (in backgroundName))
  72. { }
  73. /// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  74. /// <param name="foregroundName">Foreground</param>
  75. /// <param name="background">Background</param>
  76. public Attribute (in ColorName16 foregroundName, in Color background) : this (new Color (in foregroundName), in background) { }
  77. /// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  78. /// <param name="foreground">Foreground</param>
  79. /// <param name="backgroundName">Background</param>
  80. public Attribute (in Color foreground, in ColorName16 backgroundName) : this (in foreground, new Color (in backgroundName)) { }
  81. /// <summary>
  82. /// Initializes a new instance of the <see cref="Attribute"/> struct with the same colors for the foreground and
  83. /// background.
  84. /// </summary>
  85. /// <param name="color">The color.</param>
  86. public Attribute (in Color color) : this (color, color) { }
  87. /// <inheritdoc/>
  88. public override int GetHashCode () { return HashCode.Combine (PlatformColor, Foreground, Background); }
  89. // TODO: Add TextStyle to Attribute.ToString(), modify unit tests to account
  90. /// <inheritdoc/>
  91. public override string ToString ()
  92. {
  93. // Note: Unit tests are dependent on this format
  94. return $"[{Foreground},{Background}]";
  95. }
  96. }