#nullable enable using System.Text.Json.Serialization; namespace Terminal.Gui; /// Attributes represent how text is styled when displayed in the terminal. /// /// provides a platform independent representation of colors (and someday other forms of /// text styling). They encode both the foreground and the background color and are used in the /// class to define color schemes that can be used in an application. /// [JsonConverter (typeof (AttributeJsonConverter))] public readonly struct Attribute : IEquatable { /// Default empty attribute. public static readonly Attribute Default = new (Color.White, ColorName.Black); /// The -specific color value. [JsonIgnore (Condition = JsonIgnoreCondition.Always)] internal int PlatformColor { get; } /// The foreground color. [JsonConverter (typeof (ColorJsonConverter))] public Color Foreground { get; } /// The background color. [JsonConverter (typeof (ColorJsonConverter))] public Color Background { get; } /// Initializes a new instance with default values. public Attribute () { PlatformColor = -1; Foreground = Default.Foreground; Background = Default.Background; } /// Initializes a new instance from an existing instance. public Attribute (in Attribute attr) { PlatformColor = -1; Foreground = attr.Foreground; Background = attr.Background; } /// Initializes a new instance with platform specific color value. /// Value. internal Attribute (int platformColor) { PlatformColor = platformColor; Foreground = Default.Foreground; Background = Default.Background; } /// Initializes a new instance of the struct. /// platform-dependent color value. /// Foreground /// Background internal Attribute (int platformColor, in Color foreground, in Color background) { Foreground = foreground; Background = background; PlatformColor = platformColor; } /// Initializes a new instance of the struct. /// Foreground /// Background public Attribute (Color foreground, Color background) { Foreground = foreground; Background = background; // TODO: Once CursesDriver supports truecolor all the PlatformColor stuff goes away if (Application.Driver == null) { PlatformColor = -1; return; } Attribute make = Application.Driver.MakeColor (foreground, background); PlatformColor = make.PlatformColor; } /// /// Initializes a new instance with a value. Both and /// will be set to the specified color. /// /// Value. internal Attribute (ColorName colorName) : this (colorName, colorName) { } /// Initializes a new instance of the struct. /// Foreground /// Background public Attribute (in ColorName foregroundName, in ColorName backgroundName) : this ( new Color (foregroundName), new Color (backgroundName) ) { } /// Initializes a new instance of the struct. /// Foreground /// Background public Attribute (ColorName foregroundName, Color background) : this (new Color (foregroundName), background) { } /// Initializes a new instance of the struct. /// Foreground /// Background public Attribute (Color foreground, ColorName backgroundName) : this (foreground, new Color (backgroundName)) { } /// /// Initializes a new instance of the struct with the same colors for the foreground and /// background. /// /// The color. public Attribute (Color color) : this (color, color) { } /// Compares two attributes for equality. /// /// /// public static bool operator == (Attribute left, Attribute right) { return left.Equals (right); } /// Compares two attributes for inequality. /// /// /// public static bool operator != (Attribute left, Attribute right) { return !(left == right); } /// public override bool Equals (object? obj) { return obj is Attribute other && Equals (other); } /// public bool Equals (Attribute other) { return PlatformColor == other.PlatformColor && Foreground == other.Foreground && Background == other.Background; } /// public override int GetHashCode () { return HashCode.Combine (PlatformColor, Foreground, Background); } /// public override string ToString () { // Note: Unit tests are dependent on this format return $"[{Foreground},{Background}]"; } }