Attribute.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. [JsonIgnore]
  16. public static Attribute Default => new (Color.White, ColorName.Black);
  17. /// <summary>The <see cref="ConsoleDriver"/>-specific color value.</summary>
  18. [JsonIgnore (Condition = JsonIgnoreCondition.Always)]
  19. internal int PlatformColor { get; init; }
  20. /// <summary>The foreground color.</summary>
  21. [JsonConverter (typeof (ColorJsonConverter))]
  22. public Color Foreground { get; }
  23. /// <summary>The background color.</summary>
  24. [JsonConverter (typeof (ColorJsonConverter))]
  25. public Color Background { get; }
  26. /// <summary>Initializes a new instance with default values.</summary>
  27. public Attribute ()
  28. {
  29. this = Default with { PlatformColor = -1 };
  30. }
  31. /// <summary>Initializes a new instance from an existing instance.</summary>
  32. public Attribute (in Attribute attr)
  33. {
  34. this = attr with { PlatformColor = -1 };
  35. }
  36. /// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  37. /// <param name="platformColor">platform-dependent color value.</param>
  38. /// <param name="foreground">Foreground</param>
  39. /// <param name="background">Background</param>
  40. internal Attribute (int platformColor, Color foreground, Color background)
  41. {
  42. Foreground = foreground;
  43. Background = background;
  44. PlatformColor = platformColor;
  45. }
  46. /// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  47. /// <param name="foreground">Foreground</param>
  48. /// <param name="background">Background</param>
  49. public Attribute (in Color foreground, in Color background)
  50. {
  51. Foreground = foreground;
  52. Background = background;
  53. // TODO: Once CursesDriver supports true color all the PlatformColor stuff goes away
  54. PlatformColor = Application.Driver?.MakeColor(in foreground, in background).PlatformColor ?? -1;
  55. }
  56. /// <summary>
  57. /// Initializes a new instance with a <see cref="ColorName"/> value. Both <see cref="Foreground"/> and
  58. /// <see cref="Background"/> will be set to the specified color.
  59. /// </summary>
  60. /// <param name="colorName">Value.</param>
  61. internal Attribute (in ColorName colorName) : this (in colorName, in colorName) { }
  62. /// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  63. /// <param name="foregroundName">Foreground</param>
  64. /// <param name="backgroundName">Background</param>
  65. public Attribute (in ColorName foregroundName, in ColorName backgroundName)
  66. : this (new Color (in foregroundName), new Color (in backgroundName))
  67. { }
  68. /// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  69. /// <param name="foregroundName">Foreground</param>
  70. /// <param name="background">Background</param>
  71. public Attribute (in ColorName foregroundName, in Color background) : this (new Color (in foregroundName), in background) { }
  72. /// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  73. /// <param name="foreground">Foreground</param>
  74. /// <param name="backgroundName">Background</param>
  75. public Attribute (in Color foreground, in ColorName backgroundName) : this (in foreground, new Color (in backgroundName)) { }
  76. /// <summary>
  77. /// Initializes a new instance of the <see cref="Attribute"/> struct with the same colors for the foreground and
  78. /// background.
  79. /// </summary>
  80. /// <param name="color">The color.</param>
  81. public Attribute (in Color color) : this (color, color) { }
  82. /// <inheritdoc/>
  83. public override int GetHashCode () { return HashCode.Combine (PlatformColor, Foreground, Background); }
  84. /// <inheritdoc/>
  85. public override string ToString ()
  86. {
  87. // Note: Unit tests are dependent on this format
  88. return $"[{Foreground},{Background}]";
  89. }
  90. }