Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}