Attribute.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 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; init; }
  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. this = attr with { PlatformColor = -1 };
  36. }
  37. /// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  38. /// <param name="platformColor">platform-dependent color value.</param>
  39. /// <param name="foreground">Foreground</param>
  40. /// <param name="background">Background</param>
  41. internal Attribute (int platformColor, Color foreground, Color background)
  42. {
  43. Foreground = foreground;
  44. Background = background;
  45. PlatformColor = platformColor;
  46. }
  47. /// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  48. /// <param name="foreground">Foreground</param>
  49. /// <param name="background">Background</param>
  50. public Attribute (in Color foreground, in Color background)
  51. {
  52. Foreground = foreground;
  53. Background = background;
  54. // TODO: Once CursesDriver supports true color all the PlatformColor stuff goes away
  55. PlatformColor = Application.Driver?.MakeColor(in foreground, in background).PlatformColor ?? -1;
  56. }
  57. /// <summary>
  58. /// Initializes a new instance with a <see cref="ColorName"/> value. Both <see cref="Foreground"/> and
  59. /// <see cref="Background"/> will be set to the specified color.
  60. /// </summary>
  61. /// <param name="colorName">Value.</param>
  62. internal Attribute (in ColorName colorName) : this (in colorName, in colorName) { }
  63. /// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  64. /// <param name="foregroundName">Foreground</param>
  65. /// <param name="backgroundName">Background</param>
  66. public Attribute (in ColorName foregroundName, in ColorName backgroundName)
  67. : this (new Color (in foregroundName), new Color (in backgroundName))
  68. { }
  69. /// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  70. /// <param name="foregroundName">Foreground</param>
  71. /// <param name="background">Background</param>
  72. public Attribute (in ColorName foregroundName, in Color background) : this (new Color (foregroundName), in background) { }
  73. /// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
  74. /// <param name="foreground">Foreground</param>
  75. /// <param name="backgroundName">Background</param>
  76. public Attribute (in Color foreground, in ColorName backgroundName) : this (foreground, new Color (backgroundName)) { }
  77. /// <summary>
  78. /// Initializes a new instance of the <see cref="Attribute"/> struct with the same colors for the foreground and
  79. /// background.
  80. /// </summary>
  81. /// <param name="color">The color.</param>
  82. public Attribute (in Color color) : this (color, color) { }
  83. /// <inheritdoc/>
  84. public bool Equals (Attribute other) { return PlatformColor == other.PlatformColor && Foreground == other.Foreground && Background == other.Background; }
  85. /// <inheritdoc/>
  86. public override int GetHashCode () { return HashCode.Combine (PlatformColor, Foreground, Background); }
  87. /// <inheritdoc/>
  88. public override string ToString ()
  89. {
  90. // Note: Unit tests are dependent on this format
  91. return $"[{Foreground},{Background}]";
  92. }
  93. }