Skip to content

Commit

Permalink
Merge pull request #1359 from BDisp/cursesdriver-attribute-fix
Browse files Browse the repository at this point in the history
Fixes #1358. Attribute.Foreground / Attribute.Background now working with CursesDriver
  • Loading branch information
tig authored Jul 21, 2021
2 parents 1026f21 + fb49513 commit 481bac9
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 4 deletions.
49 changes: 45 additions & 4 deletions Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ public static Attribute MakeColor (short foreground, short background)
return new Attribute (
//value: Curses.ColorPair (last_color_pair),
value: Curses.ColorPair (v),
foreground: (Color)foreground,
background: (Color)background);

//foreground: (Color)foreground,
foreground: MapCursesColor (foreground),
//background: (Color)background);
background: MapCursesColor (background));
}

int [,] colorPairs = new int [16, 16];
Expand Down Expand Up @@ -897,10 +898,50 @@ static int MapColor (Color color)
throw new ArgumentException ("Invalid color code");
}

static Color MapCursesColor (int color)
{
switch (color) {
case Curses.COLOR_BLACK:
return Color.Black;
case Curses.COLOR_BLUE:
return Color.Blue;
case Curses.COLOR_GREEN:
return Color.Green;
case Curses.COLOR_CYAN:
return Color.Cyan;
case Curses.COLOR_RED:
return Color.Red;
case Curses.COLOR_MAGENTA:
return Color.Magenta;
case Curses.COLOR_YELLOW:
return Color.Brown;
case Curses.COLOR_WHITE:
return Color.Gray;
case Curses.COLOR_GRAY:
return Color.DarkGray;
case Curses.COLOR_BLUE | Curses.COLOR_GRAY:
return Color.BrightBlue;
case Curses.COLOR_GREEN | Curses.COLOR_GRAY:
return Color.BrightGreen;
case Curses.COLOR_CYAN | Curses.COLOR_GRAY:
return Color.BrightCyan;
case Curses.COLOR_RED | Curses.COLOR_GRAY:
return Color.BrightRed;
case Curses.COLOR_MAGENTA | Curses.COLOR_GRAY:
return Color.BrightMagenta;
case Curses.COLOR_YELLOW | Curses.COLOR_GRAY:
return Color.BrightYellow;
case Curses.COLOR_WHITE | Curses.COLOR_GRAY:
return Color.White;
}
throw new ArgumentException ("Invalid curses 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);
//return MakeColor ((short)(f & 0xffff), (short)MapColor (back)) | ((f & Curses.A_BOLD) != 0 ? Curses.A_BOLD : 0);
return MakeColor ((short)(f & 0xffff), (short)MapColor (back));
}

public override void Suspend ()
Expand Down
50 changes: 50 additions & 0 deletions UICatalog/Scenarios/InvertColors.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Terminal.Gui;

namespace UICatalog {
[ScenarioMetadata (Name: "Invert Colors", Description: "Invert the foreground and the background colors.")]
[ScenarioCategory ("Colors")]
class InvertColors : Scenario {
public override void Setup ()
{
Win.ColorScheme = Colors.TopLevel;

List<Label> labels = new List<Label> ();
var foreColors = Enum.GetValues (typeof (Color)).Cast<Color> ().ToArray ();
for (int y = 0; y < foreColors.Length; y++) {

var fore = foreColors [y];
var back = foreColors [(y + 1) % foreColors.Length];
var color = Application.Driver.MakeAttribute (fore, back);

var label = new Label ($"{fore} on {back}") {
ColorScheme = new ColorScheme (),
Y = y
};
label.ColorScheme.Normal = color;
Win.Add (label);
labels.Add (label);
}

var button = new Button ("Invert color!") {
X = Pos.Center (),
Y = foreColors.Length + 1,
};
button.Clicked += () => {
foreach (var label in labels) {
var color = label.ColorScheme.Normal;
color = Application.Driver.MakeAttribute (color.Background, color.Foreground);
label.ColorScheme.Normal = color;
label.Text = $"{color.Foreground} on {color.Background}";
label.SetNeedsDisplay ();
}
};
Win.Add (button);
}
}
}

0 comments on commit 481bac9

Please sign in to comment.