Attribute.cs 4.7 KB

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