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
2 changes: 0 additions & 2 deletions Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,8 +1013,6 @@ static int MapColor (Color color)
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");
}
Expand Down
50 changes: 21 additions & 29 deletions Terminal.Gui/Core/ConsoleDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Terminal.Gui {
/// Colors that can be used to set the foreground and background colors in console applications.
/// </summary>
/// <remarks>
/// The <see cref="Color.Invalid"/> value indicates either no-color has been set or the color is invalid.
/// The <see cref="Attribute.HasValidColors"/> value indicates either no-color has been set or the color is invalid.
/// </remarks>
public enum Color {
/// <summary>
Expand Down Expand Up @@ -81,11 +81,7 @@ public enum Color {
/// <summary>
/// The White color.
/// </summary>
White,
/// <summary>
/// Indicates an invalid or un-set color value.
/// </summary>
Invalid = -1
White
}

/// <summary>
Expand Down Expand Up @@ -177,7 +173,7 @@ private float CalculateDistance (TrueColor color1, TrueColor color2)
public struct Attribute {
/// <summary>
/// The <see cref="ConsoleDriver"/>-specific color attribute value. If <see cref="Initialized"/> is <see langword="false"/>
/// 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 <see cref="Make(Color, Color)"/>) before it is used.
/// </summary>
public int Value { get; }
Expand All @@ -199,8 +195,8 @@ public struct Attribute {
/// <param name="value">Value.</param>
public Attribute (int value)
{
Color foreground = Color.Invalid;
Color background = Color.Invalid;
Color foreground = default;
Color background = default;

Initialized = false;
if (Application.Driver != null) {
Expand Down Expand Up @@ -280,9 +276,9 @@ public static Attribute Make (Color foreground, Color background)
{
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);
}
Expand All @@ -299,25 +295,21 @@ public static Attribute Get ()
}

/// <summary>
/// If <see langword="true"/> the attribute has been initialzed by a <see cref="ConsoleDriver"/> and
/// If <see langword="true"/> the attribute has been initialized by a <see cref="ConsoleDriver"/> and
/// thus has <see cref="Value"/> that is valid for that driver. If <see langword="false"/> the <see cref="Foreground"/>
/// and <see cref="Background"/> colors may have been set (see <see cref="Color.Invalid"/>) but
/// the attribute has not been mapped to a <see cref="ConsoleDriver"/> specific color value.
/// and <see cref="Background"/> colors may have been set '-1' but
/// the attribute has not been mapped to a <see cref="ConsoleDriver"/> specific color value.
/// </summary>
/// <remarks>
/// Attributes that have not been initialized must eventually be initialized before being passed to a driver.
/// </remarks>
public bool Initialized { get; internal set; }

/// <summary>
/// Returns <see langword="true"/> if the Atrribute is valid (both foreground and background have valid color values).
/// Returns <see langword="true"/> if the Attribute is valid (both foreground and background have valid color values).
/// </summary>
/// <returns></returns>
public bool HasValidColors {
get {
return Foreground != Color.Invalid && Background != Color.Invalid;
}
}
public bool HasValidColors { get => (int)Foreground > -1 && (int)Background > -1; }
}

/// <summary>
Expand All @@ -329,7 +321,7 @@ public bool HasValidColors {
/// See also: <see cref="Colors.ColorSchemes"/>.
/// </remarks>
public class ColorScheme : IEquatable<ColorScheme> {
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);
Expand Down Expand Up @@ -520,13 +512,13 @@ static Colors ()
/// <summary>
/// Creates a new dictionary of new <see cref="ColorScheme"/> objects.
/// </summary>
public static Dictionary<string, ColorScheme> Create ()
public static Dictionary<string, ColorScheme> 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<string, ColorScheme> (p.Name, new ColorScheme()))
.Select (p => new KeyValuePair<string, ColorScheme> (p.Name, new ColorScheme ()))
.ToDictionary (t => t.Key, t => t.Value, comparer: new SchemeNameComparerIgnoreCase ());
}

Expand Down Expand Up @@ -881,7 +873,7 @@ public bool IsValidContent (int col, int row, Rect clip) =>
/// The current attribute the driver is using.
/// </summary>
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);
Expand Down Expand Up @@ -1463,13 +1455,13 @@ public Rect Clip {
public abstract Attribute MakeColor (Color foreground, Color background);

/// <summary>
/// Ensures all <see cref="Attribute"/>s in <see cref="Colors.ColorSchemes"/> are correclty
/// initalized by the driver.
/// Ensures all <see cref="Attribute"/>s in <see cref="Colors.ColorSchemes"/> are correctly
/// initialized by the driver.
/// </summary>
/// <param name="supportsColors">Flag indicating if colors are supported (not used).</param>
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 ();
}
Expand All @@ -1480,7 +1472,7 @@ public void InitalizeColorSchemes (bool supportsColors = true)


// 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);
Expand Down
5 changes: 3 additions & 2 deletions UICatalog/Scenarios/BasicColors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ public override void Setup ()
var colors = System.Enum.GetValues (typeof (Color));

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,
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 ()) {
Expand All @@ -28,7 +29,7 @@ public override void Setup ()
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++;
Expand Down
6 changes: 3 additions & 3 deletions UnitTests/Drivers/AttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,13 @@ public void IsValid_Tests ()
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);
}
}
Expand Down
29 changes: 29 additions & 0 deletions UnitTests/Drivers/ColorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,34 @@ public void ColorScheme_New ()
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]);
}
}
}