From 05ae84382d64757d78ef93d64ec001b6418052f7 Mon Sep 17 00:00:00 2001 From: Tig Date: Mon, 23 Sep 2024 09:53:52 -0600 Subject: [PATCH] Addressed review feedback. Renamed ColorName to ColorName16 to make it clear it's purpose. Added related TODOs. --- .../Application/Application.Driver.cs | 2 +- Terminal.Gui/Application/Application.Mouse.cs | 2 - .../Configuration/ColorJsonConverter.cs | 2 +- .../Configuration/SourceGenerationContext.cs | 2 +- .../CursesDriver/CursesDriver.cs | 78 ++++++------ .../ConsoleDrivers/FakeDriver/FakeDriver.cs | 4 +- Terminal.Gui/ConsoleDrivers/NetDriver.cs | 4 +- Terminal.Gui/ConsoleDrivers/WindowsDriver.cs | 2 +- Terminal.Gui/Drawing/Attribute.cs | 12 +- Terminal.Gui/Drawing/Color.ColorExtensions.cs | 61 ++++----- Terminal.Gui/Drawing/Color.ColorName.cs | 4 +- Terminal.Gui/Drawing/Color.Formatting.cs | 14 +-- Terminal.Gui/Drawing/Color.Operators.cs | 6 +- Terminal.Gui/Drawing/Color.cs | 78 ++++++------ Terminal.Gui/Views/ColorPicker16.cs | 10 +- UICatalog/Scenarios/AdornmentEditor.cs | 4 +- UICatalog/Scenarios/BasicColors.cs | 116 ------------------ UICatalog/Scenarios/ColorPicker.cs | 4 +- UICatalog/Scenarios/GraphViewExample.cs | 4 +- UICatalog/Scenarios/InvertColors.cs | 6 +- UICatalog/Scenarios/Sliders.cs | 2 +- UICatalog/Scenarios/TextEffectsScenario.cs | 12 +- UnitTests/Configuration/JsonConverterTests.cs | 46 +++---- UnitTests/Drawing/AttributeTests.cs | 14 +-- UnitTests/Drawing/ColorTests.Constructors.cs | 38 +++--- UnitTests/Drawing/ColorTests.Operators.cs | 38 +++--- .../ColorTests.ParsingAndFormatting.cs | 2 +- UnitTests/Drawing/ColorTests.TypeChecks.cs | 40 +++--- UnitTests/Drawing/ColorTests.cs | 32 ++--- UnitTests/Text/TextFormatterTests.cs | 20 +-- UnitTests/View/Adornment/BorderTests.cs | 8 +- UnitTests/View/Adornment/MarginTests.cs | 4 +- UnitTests/View/Adornment/PaddingTests.cs | 2 +- UnitTests/View/Mouse/MouseTests.cs | 2 +- UnitTests/Views/ColorPicker16Tests.cs | 26 ++-- 35 files changed, 292 insertions(+), 409 deletions(-) delete mode 100644 UICatalog/Scenarios/BasicColors.cs diff --git a/Terminal.Gui/Application/Application.Driver.cs b/Terminal.Gui/Application/Application.Driver.cs index f15bd8053..2abeb1337 100644 --- a/Terminal.Gui/Application/Application.Driver.cs +++ b/Terminal.Gui/Application/Application.Driver.cs @@ -10,7 +10,7 @@ public static partial class Application // Driver abstractions /// /// Gets or sets whether will be forced to output only the 16 colors defined in - /// . The default is , meaning 24-bit (TrueColor) colors will be output + /// . The default is , meaning 24-bit (TrueColor) colors will be output /// as long as the selected supports TrueColor. /// [SerializableConfigurationProperty (Scope = typeof (SettingsScope))] diff --git a/Terminal.Gui/Application/Application.Mouse.cs b/Terminal.Gui/Application/Application.Mouse.cs index c7927ada1..bbb60c1a6 100644 --- a/Terminal.Gui/Application/Application.Mouse.cs +++ b/Terminal.Gui/Application/Application.Mouse.cs @@ -1,8 +1,6 @@ #nullable enable using System.ComponentModel; using System.Diagnostics; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.VisualBasic.Syntax; namespace Terminal.Gui; diff --git a/Terminal.Gui/Configuration/ColorJsonConverter.cs b/Terminal.Gui/Configuration/ColorJsonConverter.cs index fe3622f7d..93296d4ed 100644 --- a/Terminal.Gui/Configuration/ColorJsonConverter.cs +++ b/Terminal.Gui/Configuration/ColorJsonConverter.cs @@ -37,7 +37,7 @@ internal class ColorJsonConverter : JsonConverter return new (color1); } - if (Enum.TryParse (colorString, true, out ColorName color)) + if (Enum.TryParse (colorString, true, out ColorName16 color)) { // Return the parsed color return new (in color); diff --git a/Terminal.Gui/Configuration/SourceGenerationContext.cs b/Terminal.Gui/Configuration/SourceGenerationContext.cs index be7ff5113..fd815b3fa 100644 --- a/Terminal.Gui/Configuration/SourceGenerationContext.cs +++ b/Terminal.Gui/Configuration/SourceGenerationContext.cs @@ -17,7 +17,7 @@ namespace Terminal.Gui; [JsonSerializable (typeof (ShadowStyle))] [JsonSerializable (typeof (HighlightStyle))] [JsonSerializable (typeof (bool?))] -[JsonSerializable (typeof (Dictionary))] +[JsonSerializable (typeof (Dictionary))] [JsonSerializable (typeof (Dictionary))] [JsonSerializable (typeof (Dictionary))] internal partial class SourceGenerationContext : JsonSerializerContext diff --git a/Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs b/Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs index 378e4b608..99e560044 100644 --- a/Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs +++ b/Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs @@ -374,7 +374,7 @@ internal class CursesDriver : ConsoleDriver ); } - CurrentAttribute = new Attribute (ColorName.White, ColorName.Black); + CurrentAttribute = new Attribute (ColorName16.White, ColorName16.Black); if (Environment.OSVersion.Platform == PlatformID.Win32NT) { @@ -859,8 +859,8 @@ internal class CursesDriver : ConsoleDriver return new Attribute ( Curses.ColorPair (v), - CursesColorNumberToColorName (foreground), - CursesColorNumberToColorName (background) + CursesColorNumberToColorName16 (foreground), + CursesColorNumberToColorName16 (background) ); } @@ -875,8 +875,8 @@ internal class CursesDriver : ConsoleDriver if (!RunningUnitTests) { return MakeColor ( - ColorNameToCursesColorNumber (foreground.GetClosestNamedColor ()), - ColorNameToCursesColorNumber (background.GetClosestNamedColor ()) + ColorNameToCursesColorNumber (foreground.GetClosestNamedColor16 ()), + ColorNameToCursesColorNumber (background.GetClosestNamedColor16 ()) ); } @@ -887,83 +887,83 @@ internal class CursesDriver : ConsoleDriver ); } - private static short ColorNameToCursesColorNumber (ColorName color) + private static short ColorNameToCursesColorNumber (ColorName16 color) { switch (color) { - case ColorName.Black: + case ColorName16.Black: return Curses.COLOR_BLACK; - case ColorName.Blue: + case ColorName16.Blue: return Curses.COLOR_BLUE; - case ColorName.Green: + case ColorName16.Green: return Curses.COLOR_GREEN; - case ColorName.Cyan: + case ColorName16.Cyan: return Curses.COLOR_CYAN; - case ColorName.Red: + case ColorName16.Red: return Curses.COLOR_RED; - case ColorName.Magenta: + case ColorName16.Magenta: return Curses.COLOR_MAGENTA; - case ColorName.Yellow: + case ColorName16.Yellow: return Curses.COLOR_YELLOW; - case ColorName.Gray: + case ColorName16.Gray: return Curses.COLOR_WHITE; - case ColorName.DarkGray: + case ColorName16.DarkGray: return Curses.COLOR_GRAY; - case ColorName.BrightBlue: + case ColorName16.BrightBlue: return Curses.COLOR_BLUE | Curses.COLOR_GRAY; - case ColorName.BrightGreen: + case ColorName16.BrightGreen: return Curses.COLOR_GREEN | Curses.COLOR_GRAY; - case ColorName.BrightCyan: + case ColorName16.BrightCyan: return Curses.COLOR_CYAN | Curses.COLOR_GRAY; - case ColorName.BrightRed: + case ColorName16.BrightRed: return Curses.COLOR_RED | Curses.COLOR_GRAY; - case ColorName.BrightMagenta: + case ColorName16.BrightMagenta: return Curses.COLOR_MAGENTA | Curses.COLOR_GRAY; - case ColorName.BrightYellow: + case ColorName16.BrightYellow: return Curses.COLOR_YELLOW | Curses.COLOR_GRAY; - case ColorName.White: + case ColorName16.White: return Curses.COLOR_WHITE | Curses.COLOR_GRAY; } throw new ArgumentException ("Invalid color code"); } - private static ColorName CursesColorNumberToColorName (short color) + private static ColorName16 CursesColorNumberToColorName16 (short color) { switch (color) { case Curses.COLOR_BLACK: - return ColorName.Black; + return ColorName16.Black; case Curses.COLOR_BLUE: - return ColorName.Blue; + return ColorName16.Blue; case Curses.COLOR_GREEN: - return ColorName.Green; + return ColorName16.Green; case Curses.COLOR_CYAN: - return ColorName.Cyan; + return ColorName16.Cyan; case Curses.COLOR_RED: - return ColorName.Red; + return ColorName16.Red; case Curses.COLOR_MAGENTA: - return ColorName.Magenta; + return ColorName16.Magenta; case Curses.COLOR_YELLOW: - return ColorName.Yellow; + return ColorName16.Yellow; case Curses.COLOR_WHITE: - return ColorName.Gray; + return ColorName16.Gray; case Curses.COLOR_GRAY: - return ColorName.DarkGray; + return ColorName16.DarkGray; case Curses.COLOR_BLUE | Curses.COLOR_GRAY: - return ColorName.BrightBlue; + return ColorName16.BrightBlue; case Curses.COLOR_GREEN | Curses.COLOR_GRAY: - return ColorName.BrightGreen; + return ColorName16.BrightGreen; case Curses.COLOR_CYAN | Curses.COLOR_GRAY: - return ColorName.BrightCyan; + return ColorName16.BrightCyan; case Curses.COLOR_RED | Curses.COLOR_GRAY: - return ColorName.BrightRed; + return ColorName16.BrightRed; case Curses.COLOR_MAGENTA | Curses.COLOR_GRAY: - return ColorName.BrightMagenta; + return ColorName16.BrightMagenta; case Curses.COLOR_YELLOW | Curses.COLOR_GRAY: - return ColorName.BrightYellow; + return ColorName16.BrightYellow; case Curses.COLOR_WHITE | Curses.COLOR_GRAY: - return ColorName.White; + return ColorName16.White; } throw new ArgumentException ("Invalid curses color code"); diff --git a/Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs b/Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs index 6ec810c8a..78215b9c8 100644 --- a/Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs +++ b/Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs @@ -165,8 +165,8 @@ public class FakeDriver : ConsoleDriver if (attr != redrawAttr) { redrawAttr = attr; - FakeConsole.ForegroundColor = (ConsoleColor)attr.Foreground.GetClosestNamedColor (); - FakeConsole.BackgroundColor = (ConsoleColor)attr.Background.GetClosestNamedColor (); + FakeConsole.ForegroundColor = (ConsoleColor)attr.Foreground.GetClosestNamedColor16 (); + FakeConsole.BackgroundColor = (ConsoleColor)attr.Background.GetClosestNamedColor16 (); } outputWidth++; diff --git a/Terminal.Gui/ConsoleDrivers/NetDriver.cs b/Terminal.Gui/ConsoleDrivers/NetDriver.cs index fcd7316e2..1298c1c35 100644 --- a/Terminal.Gui/ConsoleDrivers/NetDriver.cs +++ b/Terminal.Gui/ConsoleDrivers/NetDriver.cs @@ -961,10 +961,10 @@ internal class NetDriver : ConsoleDriver output.Append ( EscSeqUtils.CSI_SetGraphicsRendition ( MapColors ( - (ConsoleColor)attr.Background.GetClosestNamedColor (), + (ConsoleColor)attr.Background.GetClosestNamedColor16 (), false ), - MapColors ((ConsoleColor)attr.Foreground.GetClosestNamedColor ()) + MapColors ((ConsoleColor)attr.Foreground.GetClosestNamedColor16 ()) ) ); } diff --git a/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs b/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs index c0034d6c2..01a8fcd8e 100644 --- a/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs +++ b/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs @@ -72,7 +72,7 @@ internal class WindowsConsole { Char = new CharUnion { UnicodeChar = info.Char }, Attributes = - (ushort)((int)info.Attribute.Foreground.GetClosestNamedColor () | ((int)info.Attribute.Background.GetClosestNamedColor () << 4)) + (ushort)((int)info.Attribute.Foreground.GetClosestNamedColor16 () | ((int)info.Attribute.Background.GetClosestNamedColor16 () << 4)) }; } diff --git a/Terminal.Gui/Drawing/Attribute.cs b/Terminal.Gui/Drawing/Attribute.cs index 00b64f9a3..32c948d2e 100644 --- a/Terminal.Gui/Drawing/Attribute.cs +++ b/Terminal.Gui/Drawing/Attribute.cs @@ -15,7 +15,7 @@ public readonly record struct Attribute : IEqualityOperatorsDefault empty attribute. [JsonIgnore] - public static Attribute Default => new (Color.White, ColorName.Black); + public static Attribute Default => new (Color.White, Color.Black); /// The -specific color value. [JsonIgnore (Condition = JsonIgnoreCondition.Always)] @@ -65,28 +65,28 @@ public readonly record struct Attribute : IEqualityOperators - /// Initializes a new instance with a value. Both and + /// Initializes a new instance with a value. Both and /// will be set to the specified color. /// /// Value. - internal Attribute (in ColorName colorName) : this (in colorName, in colorName) { } + internal Attribute (in ColorName16 colorName) : this (in colorName, in colorName) { } /// Initializes a new instance of the struct. /// Foreground /// Background - public Attribute (in ColorName foregroundName, in ColorName backgroundName) + public Attribute (in ColorName16 foregroundName, in ColorName16 backgroundName) : this (new Color (in foregroundName), new Color (in backgroundName)) { } /// Initializes a new instance of the struct. /// Foreground /// Background - public Attribute (in ColorName foregroundName, in Color background) : this (new Color (in foregroundName), in background) { } + public Attribute (in ColorName16 foregroundName, in Color background) : this (new Color (in foregroundName), in background) { } /// Initializes a new instance of the struct. /// Foreground /// Background - public Attribute (in Color foreground, in ColorName backgroundName) : this (in foreground, new Color (in backgroundName)) { } + public Attribute (in Color foreground, in ColorName16 backgroundName) : this (in foreground, new Color (in backgroundName)) { } /// /// Initializes a new instance of the struct with the same colors for the foreground and diff --git a/Terminal.Gui/Drawing/Color.ColorExtensions.cs b/Terminal.Gui/Drawing/Color.ColorExtensions.cs index 1e0eb660b..b1a618e33 100644 --- a/Terminal.Gui/Drawing/Color.ColorExtensions.cs +++ b/Terminal.Gui/Drawing/Color.ColorExtensions.cs @@ -4,59 +4,62 @@ namespace Terminal.Gui; internal static class ColorExtensions { - private static FrozenDictionary colorToNameMap; + // TODO: This should be refactored to support all W3CColors (`ColorStrings` and this should be merged). + // TODO: ColorName and AnsiColorCode are only needed when a driver is in Force16Color mode and we + // TODO: should be able to remove these from any non-Driver-specific usages. + private static FrozenDictionary colorToNameMap; static ColorExtensions () { - Dictionary nameToCodeMap = new () + Dictionary nameToCodeMap = new () { - { ColorName.Black, AnsiColorCode.BLACK }, - { ColorName.Blue, AnsiColorCode.BLUE }, - { ColorName.Green, AnsiColorCode.GREEN }, - { ColorName.Cyan, AnsiColorCode.CYAN }, - { ColorName.Red, AnsiColorCode.RED }, - { ColorName.Magenta, AnsiColorCode.MAGENTA }, - { ColorName.Yellow, AnsiColorCode.YELLOW }, - { ColorName.Gray, AnsiColorCode.WHITE }, - { ColorName.DarkGray, AnsiColorCode.BRIGHT_BLACK }, - { ColorName.BrightBlue, AnsiColorCode.BRIGHT_BLUE }, - { ColorName.BrightGreen, AnsiColorCode.BRIGHT_GREEN }, - { ColorName.BrightCyan, AnsiColorCode.BRIGHT_CYAN }, - { ColorName.BrightRed, AnsiColorCode.BRIGHT_RED }, - { ColorName.BrightMagenta, AnsiColorCode.BRIGHT_MAGENTA }, - { ColorName.BrightYellow, AnsiColorCode.BRIGHT_YELLOW }, - { ColorName.White, AnsiColorCode.BRIGHT_WHITE } + { ColorName16.Black, AnsiColorCode.BLACK }, + { ColorName16.Blue, AnsiColorCode.BLUE }, + { ColorName16.Green, AnsiColorCode.GREEN }, + { ColorName16.Cyan, AnsiColorCode.CYAN }, + { ColorName16.Red, AnsiColorCode.RED }, + { ColorName16.Magenta, AnsiColorCode.MAGENTA }, + { ColorName16.Yellow, AnsiColorCode.YELLOW }, + { ColorName16.Gray, AnsiColorCode.WHITE }, + { ColorName16.DarkGray, AnsiColorCode.BRIGHT_BLACK }, + { ColorName16.BrightBlue, AnsiColorCode.BRIGHT_BLUE }, + { ColorName16.BrightGreen, AnsiColorCode.BRIGHT_GREEN }, + { ColorName16.BrightCyan, AnsiColorCode.BRIGHT_CYAN }, + { ColorName16.BrightRed, AnsiColorCode.BRIGHT_RED }, + { ColorName16.BrightMagenta, AnsiColorCode.BRIGHT_MAGENTA }, + { ColorName16.BrightYellow, AnsiColorCode.BRIGHT_YELLOW }, + { ColorName16.White, AnsiColorCode.BRIGHT_WHITE } }; - ColorNameToAnsiColorMap = nameToCodeMap.ToFrozenDictionary (); + ColorName16ToAnsiColorMap = nameToCodeMap.ToFrozenDictionary (); - var colorToNameDict = new Dictionary (); + var colorToNameDict = new Dictionary (); - foreach (ColorName colorName in Enum.GetValues ()) + foreach (ColorName16 colorName in Enum.GetValues ()) { - if (ColorStrings.TryParseW3CColorName (Enum.GetName (colorName), out Color color)) + if (ColorStrings.TryParseW3CColorName (Enum.GetName (colorName), out Color color)) { colorToNameDict [color] = colorName; } } - ColorToNameMap = colorToNameDict.ToFrozenDictionary (); + ColorToName16Map = colorToNameDict.ToFrozenDictionary (); } /// Defines the 16 legacy color names and their corresponding ANSI color codes. - internal static FrozenDictionary ColorNameToAnsiColorMap { get; } + internal static FrozenDictionary ColorName16ToAnsiColorMap { get; } - /// Reverse mapping for . - internal static FrozenDictionary ColorNameToColorMap { get; private set; } + /// Reverse mapping for . + internal static FrozenDictionary ColorName16ToColorMap { get; private set; } /// /// Gets or sets a that maps legacy 16-color values to the - /// corresponding . + /// corresponding . /// /// /// Setter should be called as infrequently as possible, as is /// expensive to create. /// - internal static FrozenDictionary ColorToNameMap + internal static FrozenDictionary ColorToName16Map { get => colorToNameMap; set @@ -64,7 +67,7 @@ internal static class ColorExtensions colorToNameMap = value; //Also be sure to set the reverse mapping - ColorNameToColorMap = value.ToFrozenDictionary (static kvp => kvp.Value, static kvp => kvp.Key); + ColorName16ToColorMap = value.ToFrozenDictionary (static kvp => kvp.Value, static kvp => kvp.Key); } } } diff --git a/Terminal.Gui/Drawing/Color.ColorName.cs b/Terminal.Gui/Drawing/Color.ColorName.cs index 71717639e..45bc0577c 100644 --- a/Terminal.Gui/Drawing/Color.ColorName.cs +++ b/Terminal.Gui/Drawing/Color.ColorName.cs @@ -8,10 +8,10 @@ namespace Terminal.Gui; /// These colors match the 16 colors defined for ANSI escape sequences for 4-bit (16) colors. /// /// For terminals that support 24-bit color (TrueColor), the RGB values for each of these colors can be -/// configured using the property. +/// configured using the property. /// /// -public enum ColorName +public enum ColorName16 { /// The black color. ANSI escape sequence: \u001b[30m. Black, diff --git a/Terminal.Gui/Drawing/Color.Formatting.cs b/Terminal.Gui/Drawing/Color.Formatting.cs index 794e2c9c4..65cad5626 100644 --- a/Terminal.Gui/Drawing/Color.Formatting.cs +++ b/Terminal.Gui/Drawing/Color.Formatting.cs @@ -205,7 +205,7 @@ public readonly partial record struct Color /// Converts the provided to a new value. /// /// The text to analyze. Formats supported are "#RGB", "#RRGGBB", "#ARGB", "#AARRGGBB", "rgb(r,g,b)", - /// "rgb(r,g,b,a)", "rgba(r,g,b)", "rgba(r,g,b,a)", and any of the string values. + /// "rgb(r,g,b,a)", "rgba(r,g,b)", "rgba(r,g,b,a)", and any of the string values. /// /// /// If specified and not , will be passed to @@ -246,7 +246,7 @@ public readonly partial record struct Color /// /// /// The text to analyze. Formats supported are "#RGB", "#RRGGBB", "#RGBA", "#AARRGGBB", "rgb(r,g,b)", - /// "rgb(r,g,b,a)", "rgba(r,g,b)", "rgba(r,g,b,a)", and any of the string values. + /// "rgb(r,g,b,a)", "rgba(r,g,b)", "rgba(r,g,b,a)", and any of the string values. /// /// /// Optional to provide parsing services for the input text. @@ -351,10 +351,6 @@ public readonly partial record struct Color { } when char.IsLetter (text [0]) && ColorStrings.TryParseW3CColorName (text.ToString (), out Color color) => new Color (color), - // Attempt to parse as a named color from the ColorName enum - { } when char.IsLetter (text [0]) && Enum.TryParse (text, true, out ColorName colorName) => - new Color (colorName), - // Any other input _ => throw new ColorParseException (in text, "Text did not match any expected format.", in text, []) }; @@ -475,7 +471,7 @@ public readonly partial record struct Color /// Converts the provided to a new value. /// /// The text to analyze. Formats supported are "#RGB", "#RRGGBB", "#ARGB", "#AARRGGBB", "rgb(r,g,b)", - /// "rgb(r,g,b,a)", "rgba(r,g,b)", "rgba(r,g,b,a)", and any of the string + /// "rgb(r,g,b,a)", "rgba(r,g,b)", "rgba(r,g,b,a)", and any of the string /// values. /// /// @@ -505,7 +501,7 @@ public readonly partial record struct Color /// /// /// The text to analyze. Formats supported are "#RGB", "#RRGGBB", "#ARGB", "#AARRGGBB", "rgb(r,g,b)", - /// "rgb(r,g,b,a)", "rgba(r,g,b)", "rgba(r,g,b,a)", and any of the string + /// "rgb(r,g,b,a)", "rgba(r,g,b)", "rgba(r,g,b,a)", and any of the string /// values. /// /// @@ -602,7 +598,7 @@ public readonly partial record struct Color /// Converts the provided string to a new instance. /// /// The text to analyze. Formats supported are "#RGB", "#RRGGBB", "#ARGB", "#AARRGGBB", "rgb(r,g,b)", - /// "rgb(r,g,b,a)", "rgba(r,g,b)", "rgba(r,g,b,a)", and any of the string values. + /// "rgb(r,g,b,a)", "rgba(r,g,b)", "rgba(r,g,b,a)", and any of the string values. /// /// The parsed value. /// A boolean value indicating whether parsing was successful. diff --git a/Terminal.Gui/Drawing/Color.Operators.cs b/Terminal.Gui/Drawing/Color.Operators.cs index 2884e042a..832c53543 100644 --- a/Terminal.Gui/Drawing/Color.Operators.cs +++ b/Terminal.Gui/Drawing/Color.Operators.cs @@ -53,11 +53,11 @@ public readonly partial record struct Color public static implicit operator Color (uint u) { return new Color (u); } /// - /// Implicit conversion from to via lookup from - /// . + /// Implicit conversion from to via lookup from + /// . /// [Pure] - public static implicit operator Color (ColorName colorName) { return ColorExtensions.ColorNameToColorMap [colorName]; } + public static implicit operator Color (ColorName16 colorName) { return ColorExtensions.ColorName16ToColorMap [colorName]; } /// /// Implicit conversion from to , where (, diff --git a/Terminal.Gui/Drawing/Color.cs b/Terminal.Gui/Drawing/Color.cs index 8b0e9625f..5fed5de88 100644 --- a/Terminal.Gui/Drawing/Color.cs +++ b/Terminal.Gui/Drawing/Color.cs @@ -17,7 +17,7 @@ namespace Terminal.Gui; /// /// /// -/// +/// [JsonConverter (typeof (ColorJsonConverter))] [StructLayout (LayoutKind.Explicit)] public readonly partial record struct Color : ISpanParsable, IUtf8SpanParsable, ISpanFormattable, @@ -109,9 +109,9 @@ public readonly partial record struct Color : ISpanParsable, IUtf8SpanPar /// Initializes a new instance of the color from a legacy 16-color named value. /// The 16-color value. - public Color (in ColorName colorName) + public Color (in ColorName16 colorName) { - string? name = Enum.GetName (colorName); + string? name = Enum.GetName (colorName); if (name is null) { @@ -144,26 +144,28 @@ public readonly partial record struct Color : ISpanParsable, IUtf8SpanPar /// Initializes a new instance of the with all channels set to 0. public Color () { Argb = 0u; } + // TODO: ColorName and AnsiColorCode are only needed when a driver is in Force16Color mode and we + // TODO: should be able to remove these from any non-Driver-specific usages. /// Gets or sets the 3-byte/6-character hexadecimal value for each of the legacy 16-color values. [SerializableConfigurationProperty (Scope = typeof (SettingsScope), OmitClassName = true)] - public static Dictionary Colors + public static Dictionary Colors16 { get => // Transform _colorToNameMap into a Dictionary - ColorExtensions.ColorToNameMap.ToDictionary (static kvp => kvp.Value, static kvp => kvp.Key.ToString ("g")); + ColorExtensions.ColorToName16Map.ToDictionary (static kvp => kvp.Value, static kvp => kvp.Key.ToString ("g")); set { // Transform Dictionary into _colorToNameMap - ColorExtensions.ColorToNameMap = value.ToFrozenDictionary (GetColorToNameMapKey, GetColorToNameMapValue); + ColorExtensions.ColorToName16Map = value.ToFrozenDictionary (GetColorToNameMapKey, GetColorToNameMapValue); return; - static Color GetColorToNameMapKey (KeyValuePair kvp) { return new Color (kvp.Value); } + static Color GetColorToNameMapKey (KeyValuePair kvp) { return new Color (kvp.Value); } - static ColorName GetColorToNameMapValue (KeyValuePair kvp) + static ColorName16 GetColorToNameMapValue (KeyValuePair kvp) { - return Enum.TryParse (kvp.Key.ToString (), true, out ColorName colorName) + return Enum.TryParse (kvp.Key.ToString (), true, out ColorName16 colorName) ? colorName : throw new ArgumentException ($"Invalid color name: {kvp.Key}"); } @@ -171,31 +173,31 @@ public readonly partial record struct Color : ISpanParsable, IUtf8SpanPar } /// - /// Gets the using a legacy 16-color value. will + /// Gets the using a legacy 16-color value. will /// return the closest 16 color match to the true color when no exact value is found. /// /// - /// Get returns the of the closest 24-bit color value. Set sets the RGB + /// Get returns the of the closest 24-bit color value. Set sets the RGB /// value using a hard-coded map. /// - public AnsiColorCode GetAnsiColorCode () { return ColorExtensions.ColorNameToAnsiColorMap [GetClosestNamedColor ()]; } + public AnsiColorCode GetAnsiColorCode () { return ColorExtensions.ColorName16ToAnsiColorMap [GetClosestNamedColor16 ()]; } /// - /// Gets the using a legacy 16-color value. + /// Gets the using a legacy 16-color value. /// will return the closest 16 color match to the true color when no exact value is found. /// /// - /// Get returns the of the closest 24-bit color value. Set sets the RGB + /// Get returns the of the closest 24-bit color value. Set sets the RGB /// value using a hard-coded map. /// - public ColorName GetClosestNamedColor () { return GetClosestNamedColor (this); } + public ColorName16 GetClosestNamedColor16 () { return GetClosestNamedColor16 (this); } /// /// Determines if the closest named to is the provided /// . /// /// - /// The to check if this is closer + /// The to check if this is closer /// to than any other configured named color. /// /// @@ -208,18 +210,18 @@ public readonly partial record struct Color : ISpanParsable, IUtf8SpanPar /// [Pure] [MethodImpl (MethodImplOptions.AggressiveInlining)] - public bool IsClosestToNamedColor (in ColorName namedColor) { return GetClosestNamedColor () == namedColor; } + public bool IsClosestToNamedColor16 (in ColorName16 namedColor) { return GetClosestNamedColor16 () == namedColor; } /// /// Determines if the closest named to /> is the provided /// . /// /// - /// The color to test against the value in + /// The color to test against the value in /// . /// /// - /// The to check if this is closer + /// The to check if this is closer /// to than any other configured named color. /// /// @@ -233,7 +235,7 @@ public readonly partial record struct Color : ISpanParsable, IUtf8SpanPar /// [Pure] [MethodImpl (MethodImplOptions.AggressiveInlining)] - public static bool IsColorClosestToNamedColor (in Color color, in ColorName namedColor) { return color.IsClosestToNamedColor (in namedColor); } + public static bool IsColorClosestToNamedColor16 (in Color color, in ColorName16 namedColor) { return color.IsClosestToNamedColor16 (in namedColor); } /// Gets the "closest" named color to this value. /// @@ -244,9 +246,9 @@ public readonly partial record struct Color : ISpanParsable, IUtf8SpanPar /// /// [SkipLocalsInit] - internal static ColorName GetClosestNamedColor (Color inputColor) + internal static ColorName16 GetClosestNamedColor16 (Color inputColor) { - return ColorExtensions.ColorToNameMap.MinBy (pair => CalculateColorDistance (inputColor, pair.Key)).Value; + return ColorExtensions.ColorToName16Map.MinBy (pair => CalculateColorDistance (inputColor, pair.Key)).Value; } [SkipLocalsInit] @@ -297,52 +299,52 @@ public readonly partial record struct Color : ISpanParsable, IUtf8SpanPar #region Legacy Color Names /// The black color. - public const ColorName Black = ColorName.Black; + public const ColorName16 Black = ColorName16.Black; /// The blue color. - public const ColorName Blue = ColorName.Blue; + public const ColorName16 Blue = ColorName16.Blue; /// The green color. - public const ColorName Green = ColorName.Green; + public const ColorName16 Green = ColorName16.Green; /// The cyan color. - public const ColorName Cyan = ColorName.Cyan; + public const ColorName16 Cyan = ColorName16.Cyan; /// The red color. - public const ColorName Red = ColorName.Red; + public const ColorName16 Red = ColorName16.Red; /// The magenta color. - public const ColorName Magenta = ColorName.Magenta; + public const ColorName16 Magenta = ColorName16.Magenta; /// The yellow color. - public const ColorName Yellow = ColorName.Yellow; + public const ColorName16 Yellow = ColorName16.Yellow; /// The gray color. - public const ColorName Gray = ColorName.Gray; + public const ColorName16 Gray = ColorName16.Gray; /// The dark gray color. - public const ColorName DarkGray = ColorName.DarkGray; + public const ColorName16 DarkGray = ColorName16.DarkGray; /// The bright bBlue color. - public const ColorName BrightBlue = ColorName.BrightBlue; + public const ColorName16 BrightBlue = ColorName16.BrightBlue; /// The bright green color. - public const ColorName BrightGreen = ColorName.BrightGreen; + public const ColorName16 BrightGreen = ColorName16.BrightGreen; /// The bright cyan color. - public const ColorName BrightCyan = ColorName.BrightCyan; + public const ColorName16 BrightCyan = ColorName16.BrightCyan; /// The bright red color. - public const ColorName BrightRed = ColorName.BrightRed; + public const ColorName16 BrightRed = ColorName16.BrightRed; /// The bright magenta color. - public const ColorName BrightMagenta = ColorName.BrightMagenta; + public const ColorName16 BrightMagenta = ColorName16.BrightMagenta; /// The bright yellow color. - public const ColorName BrightYellow = ColorName.BrightYellow; + public const ColorName16 BrightYellow = ColorName16.BrightYellow; /// The White color. - public const ColorName White = ColorName.White; + public const ColorName16 White = ColorName16.White; #endregion } \ No newline at end of file diff --git a/Terminal.Gui/Views/ColorPicker16.cs b/Terminal.Gui/Views/ColorPicker16.cs index 7312f113b..4296eb708 100644 --- a/Terminal.Gui/Views/ColorPicker16.cs +++ b/Terminal.Gui/Views/ColorPicker16.cs @@ -61,7 +61,7 @@ public class ColorPicker16 : View set { int colorIndex = value.Y * _cols + value.X; - SelectedColor = (ColorName)colorIndex; + SelectedColor = (ColorName16)colorIndex; } } @@ -132,7 +132,7 @@ public class ColorPicker16 : View continue; } - Driver.SetAttribute (new ((ColorName)foregroundColorIndex, (ColorName)colorIndex)); + Driver.SetAttribute (new ((ColorName16)foregroundColorIndex, (ColorName16)colorIndex)); bool selected = x == Cursor.X && y == Cursor.Y; DrawColorBox (x, y, selected); colorIndex++; @@ -141,12 +141,12 @@ public class ColorPicker16 : View } /// Selected color. - public ColorName SelectedColor + public ColorName16 SelectedColor { - get => (ColorName)_selectColorIndex; + get => (ColorName16)_selectColorIndex; set { - if (value == (ColorName)_selectColorIndex) + if (value == (ColorName16)_selectColorIndex) { return; } diff --git a/UICatalog/Scenarios/AdornmentEditor.cs b/UICatalog/Scenarios/AdornmentEditor.cs index 4a5a7a38d..01fb05ccb 100644 --- a/UICatalog/Scenarios/AdornmentEditor.cs +++ b/UICatalog/Scenarios/AdornmentEditor.cs @@ -61,8 +61,8 @@ public class AdornmentEditor : View _adornment.Initialized += (sender, args) => { var cs = _adornment.ColorScheme; - _foregroundColorPicker.SelectedColor = cs.Normal.Foreground.GetClosestNamedColor (); - _backgroundColorPicker.SelectedColor = cs.Normal.Background.GetClosestNamedColor (); + _foregroundColorPicker.SelectedColor = cs.Normal.Foreground.GetClosestNamedColor16 (); + _backgroundColorPicker.SelectedColor = cs.Normal.Background.GetClosestNamedColor16 (); }; } diff --git a/UICatalog/Scenarios/BasicColors.cs b/UICatalog/Scenarios/BasicColors.cs deleted file mode 100644 index 0fc284621..000000000 --- a/UICatalog/Scenarios/BasicColors.cs +++ /dev/null @@ -1,116 +0,0 @@ -using System; -using Terminal.Gui; - -namespace UICatalog.Scenarios; - -[ScenarioMetadata ("Basic Colors", "Show all basic colors.")] -[ScenarioCategory ("Colors")] -[ScenarioCategory ("Text and Formatting")] -public class BasicColors : Scenario -{ - public override void Main () - { - Application.Init (); - - Window app = new () - { - Title = GetQuitKeyAndName (), - }; - - var vx = 30; - var x = 30; - var y = 14; - Array colors = Enum.GetValues (typeof (ColorName)); - - foreach (ColorName bg in colors) - { - var attr = new Attribute (bg, colors.Length - 1 - bg); - - var vl = new Label - { - X = vx, - Y = 0, - Width = 1, - Height = 13, - VerticalTextAlignment = Alignment.End, - ColorScheme = new ColorScheme { Normal = attr }, - Text = bg.ToString (), - TextDirection = TextDirection.TopBottom_LeftRight - }; - app.Add (vl); - - var hl = new Label - { - X = 15, - Y = y, - Width = 13, - Height = 1, - TextAlignment = Alignment.End, - ColorScheme = new ColorScheme { Normal = attr }, - Text = bg.ToString () - }; - app.Add (hl); - vx++; - - foreach (ColorName fg in colors) - { - var c = new Attribute (fg, bg); - var t = x.ToString (); - - var l = new Label - { - ColorScheme = new ColorScheme { Normal = c }, X = x, Y = y, Text = t [^1].ToString () - }; - app.Add (l); - x++; - } - - x = 30; - y++; - } - - app.Add ( - new Label { X = Pos.AnchorEnd (36), Text = "Mouse over to get the Attribute:" } - ); - app.Add (new Label { X = Pos.AnchorEnd (35), Y = 2, Text = "Foreground:" }); - - var lblForeground = new Label { X = Pos.AnchorEnd (23), Y = 2 }; - app.Add (lblForeground); - - var viewForeground = new View { X = Pos.AnchorEnd (2), Y = 2, ColorScheme = new ColorScheme (), Text = " " }; - app.Add (viewForeground); - - app.Add (new Label { X = Pos.AnchorEnd (35), Y = 4, Text = "Background:" }); - - var lblBackground = new Label { X = Pos.AnchorEnd (23), Y = 4 }; - app.Add (lblBackground); - - var viewBackground = new View { X = Pos.AnchorEnd (2), Y = 4, ColorScheme = new ColorScheme (), Text = " " }; - app.Add (viewBackground); - - Application.MouseEvent += (s, e) => - { - if (e.View != null) - { - Color fore = e.View.GetNormalColor ().Foreground; - Color back = e.View.GetNormalColor ().Background; - - lblForeground.Text = - $"#{fore.R:X2}{fore.G:X2}{fore.B:X2} {fore.GetClosestNamedColor ()} "; - - viewForeground.ColorScheme = - new ColorScheme (viewForeground.ColorScheme) { Normal = new Attribute (fore, fore) }; - - lblBackground.Text = - $"#{back.R:X2}{back.G:X2}{back.B:X2} {back.GetClosestNamedColor ()} "; - - viewBackground.ColorScheme = - new ColorScheme (viewBackground.ColorScheme) { Normal = new Attribute (back, back) }; - } - }; - - Application.Run (app); - app.Dispose (); - Application.Shutdown (); - } -} diff --git a/UICatalog/Scenarios/ColorPicker.cs b/UICatalog/Scenarios/ColorPicker.cs index b1c94720f..c05931912 100644 --- a/UICatalog/Scenarios/ColorPicker.cs +++ b/UICatalog/Scenarios/ColorPicker.cs @@ -216,8 +216,8 @@ public class ColorPickers : Scenario app.Add (cbShowName); // Set default colors. - foregroundColorPicker.SelectedColor = _demoView.SuperView.ColorScheme.Normal.Foreground.GetClosestNamedColor (); - backgroundColorPicker.SelectedColor = _demoView.SuperView.ColorScheme.Normal.Background.GetClosestNamedColor (); + foregroundColorPicker.SelectedColor = _demoView.SuperView.ColorScheme.Normal.Foreground.GetClosestNamedColor16 (); + backgroundColorPicker.SelectedColor = _demoView.SuperView.ColorScheme.Normal.Background.GetClosestNamedColor16 (); app.Initialized += (s, e) => app.LayoutSubviews (); Application.Run (app); diff --git a/UICatalog/Scenarios/GraphViewExample.cs b/UICatalog/Scenarios/GraphViewExample.cs index 8cb528528..1b670ee44 100644 --- a/UICatalog/Scenarios/GraphViewExample.cs +++ b/UICatalog/Scenarios/GraphViewExample.cs @@ -239,8 +239,8 @@ public class GraphViewExample : Scenario _about.Text = "Housing Expenditures by income thirds 1996-2003"; - Color fore = _graphView.ColorScheme.Normal.Foreground == new Color (ColorName.Black) - ? new (ColorName.White) + Color fore = _graphView.ColorScheme.Normal.Foreground == Color.Black + ? Color.White : _graphView.ColorScheme.Normal.Foreground; var black = new Attribute (fore, Color.Black); var cyan = new Attribute (Color.BrightCyan, Color.Black); diff --git a/UICatalog/Scenarios/InvertColors.cs b/UICatalog/Scenarios/InvertColors.cs index a98aaf720..d88e627d4 100644 --- a/UICatalog/Scenarios/InvertColors.cs +++ b/UICatalog/Scenarios/InvertColors.cs @@ -20,12 +20,12 @@ public class InvertColors : Scenario }; List