Răsfoiți Sursa

Add Attribute.Make that delegates the color creation to the platform driver to more easily create attributes that can be used

miguel 6 ani în urmă
părinte
comite
cd684a405c

+ 26 - 0
Terminal.Gui/Drivers/ConsoleDriver.cs

@@ -103,8 +103,32 @@ namespace Terminal.Gui {
 			this.value = value;
 		}
 
+		/// <summary>
+		/// Implicit conversion from an attribute to the underlying Int32 representation
+		/// </summary>
+		/// <returns>The integer value stored in the attribute.</returns>
+		/// <param name="c">The attribute to convert</param>
 		public static implicit operator int (Attribute c) => c.value;
+
+		/// <summary>
+		/// Implicitly convert an integer value into an attribute
+		/// </summary>
+		/// <returns>An attribute with the specified integer value.</returns>
+		/// <param name="v">value</param>
 		public static implicit operator Attribute (int v) => new Attribute (v);
+
+		/// <summary>
+		/// Creates an attribute from the specified foreground and background.
+		/// </summary>
+		/// <returns>The make.</returns>
+		/// <param name="foreground">Foreground color to use.</param>
+		/// <param name="background">Background color to use.</param>
+		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);
+		}
 	}
 
 	/// <summary>
@@ -449,5 +473,7 @@ namespace Terminal.Gui {
 		/// The bottom tee.
 		/// </summary>
 		public Rune BottomTee;
+
+		public abstract Attribute MakeAttribute (Color fore, Color back);
 	}
 }

+ 45 - 0
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 ();

+ 5 - 0
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)
 		{

+ 5 - 0
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 ();