From cd684a405ce38c0173602957b4a81f0129efd324 Mon Sep 17 00:00:00 2001 From: miguel Date: Tue, 9 Apr 2019 22:32:18 -0400 Subject: [PATCH] Add Attribute.Make that delegates the color creation to the platform driver to more easily create attributes that can be used --- Terminal.Gui/Drivers/ConsoleDriver.cs | 26 ++++++++++++++++ Terminal.Gui/Drivers/CursesDriver.cs | 45 +++++++++++++++++++++++++++ Terminal.Gui/Drivers/NetDriver.cs | 5 +++ Terminal.Gui/Drivers/WindowsDriver.cs | 5 +++ 4 files changed, 81 insertions(+) diff --git a/Terminal.Gui/Drivers/ConsoleDriver.cs b/Terminal.Gui/Drivers/ConsoleDriver.cs index 85cfddad8..6285ca971 100644 --- a/Terminal.Gui/Drivers/ConsoleDriver.cs +++ b/Terminal.Gui/Drivers/ConsoleDriver.cs @@ -103,8 +103,32 @@ namespace Terminal.Gui { this.value = value; } + /// + /// Implicit conversion from an attribute to the underlying Int32 representation + /// + /// The integer value stored in the attribute. + /// The attribute to convert public static implicit operator int (Attribute c) => c.value; + + /// + /// Implicitly convert an integer value into an attribute + /// + /// An attribute with the specified integer value. + /// value public static implicit operator Attribute (int v) => new Attribute (v); + + /// + /// Creates an attribute from the specified foreground and background. + /// + /// The make. + /// Foreground color to use. + /// Background color to use. + public static Attribute Make (Color foreground, Color background) + { + if (Application.Driver == null) + throw new InvalidOperationException ("The Application has not been initialized"); + return Application.Driver.MakeAttribute (foreground, background); + } } /// @@ -449,5 +473,7 @@ namespace Terminal.Gui { /// The bottom tee. /// public Rune BottomTee; + + public abstract Attribute MakeAttribute (Color fore, Color back); } } diff --git a/Terminal.Gui/Drivers/CursesDriver.cs b/Terminal.Gui/Drivers/CursesDriver.cs index 56a1c07c3..323dc459a 100644 --- a/Terminal.Gui/Drivers/CursesDriver.cs +++ b/Terminal.Gui/Drivers/CursesDriver.cs @@ -295,6 +295,51 @@ namespace Terminal.Gui { } } + static int MapColor (Color color) + { + switch (color) { + case Color.Black: + return Curses.COLOR_BLACK; + case Color.Blue: + return Curses.COLOR_BLUE; + case Color.Green: + return Curses.COLOR_GREEN; + case Color.Cyan: + return Curses.COLOR_CYAN; + case Color.Red: + return Curses.COLOR_RED; + case Color.Magenta: + return Curses.COLOR_MAGENTA; + case Color.Brown: + return Curses.COLOR_YELLOW; + case Color.Gray: + return Curses.COLOR_WHITE; + case Color.DarkGray: + return Curses.COLOR_BLACK | Curses.A_BOLD; + case Color.BrightBlue: + return Curses.COLOR_BLUE | Curses.A_BOLD; + case Color.BrightGreen: + return Curses.COLOR_GREEN | Curses.A_BOLD; + case Color.BrighCyan: + return Curses.COLOR_CYAN | Curses.A_BOLD; + case Color.BrightRed: + return Curses.COLOR_RED | Curses.A_BOLD; + case Color.BrightMagenta: + return Curses.COLOR_MAGENTA | Curses.A_BOLD; + case Color.BrightYellow: + return Curses.COLOR_YELLOW | Curses.A_BOLD; + case Color.White: + return Curses.COLOR_WHITE | Curses.A_BOLD; + } + throw new ArgumentException ("Invalid color code"); + } + + public override Attribute MakeAttribute (Color fore, Color back) + { + var f = MapColor (fore); + return MakeColor ((short)(f & 0xffff), (short)MapColor (back)) | ((f & Curses.A_BOLD) != 0 ? Curses.A_BOLD : 0); + } + public override void Suspend () { StopReportingMouseMoves (); diff --git a/Terminal.Gui/Drivers/NetDriver.cs b/Terminal.Gui/Drivers/NetDriver.cs index d524774a5..0487d0d7b 100644 --- a/Terminal.Gui/Drivers/NetDriver.cs +++ b/Terminal.Gui/Drivers/NetDriver.cs @@ -157,6 +157,11 @@ namespace Terminal.Gui { Console.Clear (); } + public override Attribute MakeAttribute (Color fore, Color back) + { + return MakeColor ((ConsoleColor)fore, (ConsoleColor)back); + } + int redrawColor = -1; void SetColor (int color) { diff --git a/Terminal.Gui/Drivers/WindowsDriver.cs b/Terminal.Gui/Drivers/WindowsDriver.cs index c365df781..55e52be37 100644 --- a/Terminal.Gui/Drivers/WindowsDriver.cs +++ b/Terminal.Gui/Drivers/WindowsDriver.cs @@ -786,6 +786,11 @@ namespace Terminal.Gui { }; } + public override Attribute MakeAttribute (Color fore, Color back) + { + return MakeColor ((ConsoleColor)fore, (ConsoleColor)back); + } + public override void Refresh () { UpdateScreen ();