From 276ffe0343ee6cba5783641f85554f34d008f32a Mon Sep 17 00:00:00 2001 From: BDisp Date: Mon, 13 Feb 2023 20:52:00 +0000 Subject: [PATCH 1/3] Fixes #2348. The Basic Colors scenario doesn't output the blue on white properly. --- UICatalog/Scenarios/BasicColors.cs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/UICatalog/Scenarios/BasicColors.cs b/UICatalog/Scenarios/BasicColors.cs index b5110b33d..d88128206 100644 --- a/UICatalog/Scenarios/BasicColors.cs +++ b/UICatalog/Scenarios/BasicColors.cs @@ -1,4 +1,5 @@ -using Terminal.Gui; +using System.Linq; +using Terminal.Gui; namespace UICatalog.Scenarios { [ScenarioMetadata (Name: "Basic Colors", Description: "Show all basic colors.")] @@ -10,16 +11,18 @@ namespace UICatalog.Scenarios { var vx = 30; var x = 30; var y = 14; - var colors = System.Enum.GetValues (typeof (Color)); + var colors = System.Enum.GetValues (typeof (Color)).Cast ().ToArray (); - foreach (Color bg in colors) { + for (int i = 0; i < colors.Length; i++) { + Color bg = colors [i]; + Attribute attr = new Attribute (bg, colors [colors.Length - 1 - i]); var vl = new Label (bg.ToString (), TextDirection.TopBottom_LeftRight) { X = vx, Y = 0, Width = 1, Height = 13, VerticalTextAlignment = VerticalTextAlignment.Bottom, - ColorScheme = new ColorScheme () { Normal = new Attribute (bg, colors.Length - 1 - bg) } + ColorScheme = new ColorScheme () { Normal = attr } }; Win.Add (vl); var hl = new Label (bg.ToString ()) { @@ -28,7 +31,7 @@ namespace UICatalog.Scenarios { Width = 13, Height = 1, TextAlignment = TextAlignment.Right, - ColorScheme = new ColorScheme () { Normal = new Attribute (bg, colors.Length - 1 - bg) } + ColorScheme = new ColorScheme () { Normal = attr } }; Win.Add (hl); vx++; From 721a8873b0fcd634a2b96c1639c396bfe5b5786c Mon Sep 17 00:00:00 2001 From: BDisp Date: Tue, 14 Feb 2023 18:32:06 +0000 Subject: [PATCH 2/3] Removed Color.Invalid and added more unit tests. --- .../CursesDriver/CursesDriver.cs | 2 - Terminal.Gui/Core/ConsoleDriver.cs | 50 ++++++++----------- UICatalog/Scenarios/BasicColors.cs | 10 ++-- UnitTests/Drivers/AttributeTests.cs | 6 +-- UnitTests/Drivers/ColorTests.cs | 29 +++++++++++ 5 files changed, 57 insertions(+), 40 deletions(-) diff --git a/Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs b/Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs index c915db6e4..ec2220519 100644 --- a/Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs +++ b/Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs @@ -1013,8 +1013,6 @@ namespace Terminal.Gui { return Curses.COLOR_YELLOW | Curses.A_BOLD | Curses.COLOR_GRAY; case Color.White: return Curses.COLOR_WHITE | Curses.A_BOLD | Curses.COLOR_GRAY; - case Color.Invalid: - return Curses.COLOR_BLACK; } throw new ArgumentException ("Invalid color code"); } diff --git a/Terminal.Gui/Core/ConsoleDriver.cs b/Terminal.Gui/Core/ConsoleDriver.cs index bc3856289..74cd4f5fa 100644 --- a/Terminal.Gui/Core/ConsoleDriver.cs +++ b/Terminal.Gui/Core/ConsoleDriver.cs @@ -15,7 +15,7 @@ namespace Terminal.Gui { /// Colors that can be used to set the foreground and background colors in console applications. /// /// - /// The value indicates either no-color has been set or the color is invalid. + /// The value indicates either no-color has been set or the color is invalid. /// public enum Color { /// @@ -81,11 +81,7 @@ namespace Terminal.Gui { /// /// The White color. /// - White, - /// - /// Indicates an invalid or un-set color value. - /// - Invalid = -1 + White } /// @@ -177,7 +173,7 @@ namespace Terminal.Gui { public struct Attribute { /// /// The -specific color attribute value. If is - /// the value of this property is invalid (typcially because the Attribute was created before a driver was loaded) + /// the value of this property is invalid (typically because the Attribute was created before a driver was loaded) /// and the attribute should be re-made (see ) before it is used. /// public int Value { get; } @@ -199,8 +195,8 @@ namespace Terminal.Gui { /// Value. public Attribute (int value) { - Color foreground = Color.Invalid; - Color background = Color.Invalid; + Color foreground = default; + Color background = default; Initialized = false; if (Application.Driver != null) { @@ -280,9 +276,9 @@ namespace Terminal.Gui { { if (Application.Driver == null) { // Create the attribute, but show it's not been initialized - var a = new Attribute (-1, foreground, background); - a.Initialized = false; - return a; + return new Attribute (-1, foreground, background) { + Initialized = false + }; } return Application.Driver.MakeAttribute (foreground, background); } @@ -299,10 +295,10 @@ namespace Terminal.Gui { } /// - /// If the attribute has been initialzed by a and + /// If the attribute has been initialized by a and /// thus has that is valid for that driver. If the - /// and colors may have been set (see ) but - /// the attribute has not been mapped to a specific color value. + /// and colors may have been set '-1' but + /// the attribute has not been mapped to a specific color value. /// /// /// Attributes that have not been initialized must eventually be initialized before being passed to a driver. @@ -310,14 +306,10 @@ namespace Terminal.Gui { public bool Initialized { get; internal set; } /// - /// Returns if the Atrribute is valid (both foreground and background have valid color values). + /// Returns if the Attribute is valid (both foreground and background have valid color values). /// /// - public bool HasValidColors { - get { - return Foreground != Color.Invalid && Background != Color.Invalid; - } - } + public bool HasValidColors { get => (int)Foreground > -1 && (int)Background != -1; } } /// @@ -329,7 +321,7 @@ namespace Terminal.Gui { /// See also: . /// public class ColorScheme : IEquatable { - Attribute _normal = new Attribute(Color.White, Color.Black); + Attribute _normal = new Attribute (Color.White, Color.Black); Attribute _focus = new Attribute (Color.White, Color.Black); Attribute _hotNormal = new Attribute (Color.White, Color.Black); Attribute _hotFocus = new Attribute (Color.White, Color.Black); @@ -520,13 +512,13 @@ namespace Terminal.Gui { /// /// Creates a new dictionary of new objects. /// - public static Dictionary Create () + public static Dictionary Create () { // Use reflection to dynamically create the default set of ColorSchemes from the list defined // by the class. return typeof (Colors).GetProperties () .Where (p => p.PropertyType == typeof (ColorScheme)) - .Select (p => new KeyValuePair (p.Name, new ColorScheme())) + .Select (p => new KeyValuePair (p.Name, new ColorScheme ())) .ToDictionary (t => t.Key, t => t.Value, comparer: new SchemeNameComparerIgnoreCase ()); } @@ -881,7 +873,7 @@ namespace Terminal.Gui { /// The current attribute the driver is using. /// public virtual Attribute CurrentAttribute { - get => currentAttribute; + get => currentAttribute; set { if (!value.Initialized && value.HasValidColors && Application.Driver != null) { CurrentAttribute = Application.Driver.MakeAttribute (value.Foreground, value.Background); @@ -1463,13 +1455,13 @@ namespace Terminal.Gui { public abstract Attribute MakeColor (Color foreground, Color background); /// - /// Ensures all s in are correclty - /// initalized by the driver. + /// Ensures all s in are correctly + /// initialized by the driver. /// /// Flag indicating if colors are supported (not used). public void InitalizeColorSchemes (bool supportsColors = true) { - // Ensure all Attributes are initlaized by the driver + // Ensure all Attributes are initialized by the driver foreach (var s in Colors.ColorSchemes) { s.Value.Initialize (); } @@ -1480,7 +1472,7 @@ namespace Terminal.Gui { // Define the default color theme only if the user has not defined one. - + Colors.TopLevel.Normal = MakeColor (Color.BrightGreen, Color.Black); Colors.TopLevel.Focus = MakeColor (Color.White, Color.Cyan); Colors.TopLevel.HotNormal = MakeColor (Color.Brown, Color.Black); diff --git a/UICatalog/Scenarios/BasicColors.cs b/UICatalog/Scenarios/BasicColors.cs index d88128206..a8fe04e4c 100644 --- a/UICatalog/Scenarios/BasicColors.cs +++ b/UICatalog/Scenarios/BasicColors.cs @@ -1,5 +1,4 @@ -using System.Linq; -using Terminal.Gui; +using Terminal.Gui; namespace UICatalog.Scenarios { [ScenarioMetadata (Name: "Basic Colors", Description: "Show all basic colors.")] @@ -11,11 +10,10 @@ namespace UICatalog.Scenarios { var vx = 30; var x = 30; var y = 14; - var colors = System.Enum.GetValues (typeof (Color)).Cast ().ToArray (); + var colors = System.Enum.GetValues (typeof (Color)); - for (int i = 0; i < colors.Length; i++) { - Color bg = colors [i]; - Attribute attr = new Attribute (bg, colors [colors.Length - 1 - i]); + foreach (Color bg in colors) { + Attribute attr = new Attribute (bg, colors.Length - 1 - bg); var vl = new Label (bg.ToString (), TextDirection.TopBottom_LeftRight) { X = vx, Y = 0, diff --git a/UnitTests/Drivers/AttributeTests.cs b/UnitTests/Drivers/AttributeTests.cs index fd563c051..f2130f220 100644 --- a/UnitTests/Drivers/AttributeTests.cs +++ b/UnitTests/Drivers/AttributeTests.cs @@ -218,13 +218,13 @@ namespace Terminal.Gui.DriverTests { attr = new Attribute (Color.Red, Color.Green); Assert.True (attr.HasValidColors); - attr = new Attribute (Color.Red, Color.Invalid); + attr = new Attribute (Color.Red, (Color)(-1)); Assert.False (attr.HasValidColors); - attr = new Attribute (Color.Invalid, Color.Green); + attr = new Attribute ((Color)(-1), Color.Green); Assert.False (attr.HasValidColors); - attr = new Attribute (Color.Invalid, Color.Invalid); + attr = new Attribute ((Color)(-1), (Color)(-1)); Assert.False (attr.HasValidColors); } } diff --git a/UnitTests/Drivers/ColorTests.cs b/UnitTests/Drivers/ColorTests.cs index 3e8266e27..2ac8ad14d 100644 --- a/UnitTests/Drivers/ColorTests.cs +++ b/UnitTests/Drivers/ColorTests.cs @@ -44,5 +44,34 @@ namespace Terminal.Gui.DriverTests { lbl.Redraw (lbl.Bounds); } + [Fact] + public void TestAllColors () + { + var colors = System.Enum.GetValues (typeof (Color)); + Attribute [] attrs = new Attribute [colors.Length]; + + int idx = 0; + foreach (Color bg in colors) { + attrs [idx] = new Attribute (bg, colors.Length - 1 - bg); + idx++; + } + Assert.Equal (16, attrs.Length); + Assert.Equal (new Attribute (Color.Black, Color.White), attrs [0]); + Assert.Equal (new Attribute (Color.Blue, Color.BrightYellow), attrs [1]); + Assert.Equal (new Attribute (Color.Green, Color.BrightMagenta), attrs [2]); + Assert.Equal (new Attribute (Color.Cyan, Color.BrightRed), attrs [3]); + Assert.Equal (new Attribute (Color.Red, Color.BrightCyan), attrs [4]); + Assert.Equal (new Attribute (Color.Magenta, Color.BrightGreen), attrs [5]); + Assert.Equal (new Attribute (Color.Brown, Color.BrightBlue), attrs [6]); + Assert.Equal (new Attribute (Color.Gray, Color.DarkGray), attrs [7]); + Assert.Equal (new Attribute (Color.DarkGray, Color.Gray), attrs [8]); + Assert.Equal (new Attribute (Color.BrightBlue, Color.Brown), attrs [9]); + Assert.Equal (new Attribute (Color.BrightGreen, Color.Magenta), attrs [10]); + Assert.Equal (new Attribute (Color.BrightCyan, Color.Red), attrs [11]); + Assert.Equal (new Attribute (Color.BrightRed, Color.Cyan), attrs [12]); + Assert.Equal (new Attribute (Color.BrightMagenta, Color.Green), attrs [13]); + Assert.Equal (new Attribute (Color.BrightYellow, Color.Blue), attrs [14]); + Assert.Equal (new Attribute (Color.White, Color.Black), attrs [^1]); + } } } \ No newline at end of file From 43efe8a563a50a4715140c302cde68c9f8a0147c Mon Sep 17 00:00:00 2001 From: BDisp Date: Tue, 14 Feb 2023 18:43:58 +0000 Subject: [PATCH 3/3] Ensures working even if a value less than -1 is used. --- Terminal.Gui/Core/ConsoleDriver.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Terminal.Gui/Core/ConsoleDriver.cs b/Terminal.Gui/Core/ConsoleDriver.cs index 74cd4f5fa..5f88b5d86 100644 --- a/Terminal.Gui/Core/ConsoleDriver.cs +++ b/Terminal.Gui/Core/ConsoleDriver.cs @@ -309,7 +309,7 @@ namespace Terminal.Gui { /// Returns if the Attribute is valid (both foreground and background have valid color values). /// /// - public bool HasValidColors { get => (int)Foreground > -1 && (int)Background != -1; } + public bool HasValidColors { get => (int)Foreground > -1 && (int)Background > -1; } } ///