diff --git a/Examples/UICatalog/Scenarios/ColorPicker.cs b/Examples/UICatalog/Scenarios/ColorPicker.cs
index 69ae48660..a058af4af 100644
--- a/Examples/UICatalog/Scenarios/ColorPicker.cs
+++ b/Examples/UICatalog/Scenarios/ColorPicker.cs
@@ -1,7 +1,4 @@
-using System;
-using System.Collections.Generic;
-
-namespace UICatalog.Scenarios;
+namespace UICatalog.Scenarios;
[ScenarioMetadata ("ColorPicker", "Color Picker and TrueColor demonstration.")]
[ScenarioCategory ("Colors")]
@@ -18,25 +15,25 @@ public class ColorPickers : Scenario
private Label _foregroundColorLabel;
/// Background ColorPicker.
- private ColorPicker backgroundColorPicker;
+ private ColorPicker _backgroundColorPicker;
/// Foreground ColorPicker.
- private ColorPicker foregroundColorPicker;
+ private ColorPicker _foregroundColorPicker;
/// Background ColorPicker.
- private ColorPicker16 backgroundColorPicker16;
+ private ColorPicker16 _backgroundColorPicker16;
/// Foreground ColorPicker.
- private ColorPicker16 foregroundColorPicker16;
+ private ColorPicker16 _foregroundColorPicker16;
- /// Setup the scenario.
+ /// Set up the scenario.
public override void Main ()
{
Application.Init ();
Window app = new ()
{
- Title = GetQuitKeyAndName (),
+ Title = GetQuitKeyAndName ()
};
///////////////////////////////////////
@@ -44,23 +41,23 @@ public class ColorPickers : Scenario
///////////////////////////////////////
// Foreground ColorPicker.
- foregroundColorPicker = new ColorPicker
+ _foregroundColorPicker = new ()
{
Title = "_Foreground Color",
BorderStyle = LineStyle.Single,
Width = Dim.Percent (50)
};
- foregroundColorPicker.ColorChanged += ForegroundColor_ColorChanged;
- app.Add (foregroundColorPicker);
+ _foregroundColorPicker.ColorChanged += ForegroundColor_ColorChanged;
+ app.Add (_foregroundColorPicker);
- _foregroundColorLabel = new Label
+ _foregroundColorLabel = new ()
{
- X = Pos.Left (foregroundColorPicker), Y = Pos.Bottom (foregroundColorPicker) + 1
+ X = Pos.Left (_foregroundColorPicker), Y = Pos.Bottom (_foregroundColorPicker) + 1
};
app.Add (_foregroundColorLabel);
// Background ColorPicker.
- backgroundColorPicker = new ColorPicker
+ _backgroundColorPicker = new ()
{
Title = "_Background Color",
X = Pos.AnchorEnd (),
@@ -68,49 +65,47 @@ public class ColorPickers : Scenario
BorderStyle = LineStyle.Single
};
- backgroundColorPicker.ColorChanged += BackgroundColor_ColorChanged;
- app.Add (backgroundColorPicker);
- _backgroundColorLabel = new Label ()
+ _backgroundColorPicker.ColorChanged += BackgroundColor_ColorChanged;
+ app.Add (_backgroundColorPicker);
+
+ _backgroundColorLabel = new ()
{
X = Pos.AnchorEnd (),
- Y = Pos.Bottom (backgroundColorPicker) + 1
+ Y = Pos.Bottom (_backgroundColorPicker) + 1
};
app.Add (_backgroundColorLabel);
-
///////////////////////////////////////
// 16 Color Pickers
///////////////////////////////////////
-
// Foreground ColorPicker 16.
- foregroundColorPicker16 = new ColorPicker16
+ _foregroundColorPicker16 = new ()
{
Title = "_Foreground Color",
BorderStyle = LineStyle.Single,
Width = Dim.Percent (50),
- Visible = false // We default to HSV so hide old one
+ Visible = false // We default to HSV so hide old one
};
- foregroundColorPicker16.ColorChanged += ForegroundColor_ColorChanged;
- app.Add (foregroundColorPicker16);
+ _foregroundColorPicker16.ColorChanged += ForegroundColor_ColorChanged;
+ app.Add (_foregroundColorPicker16);
// Background ColorPicker 16.
- backgroundColorPicker16 = new ColorPicker16
+ _backgroundColorPicker16 = new ()
{
Title = "_Background Color",
X = Pos.AnchorEnd (),
Width = Dim.Percent (50),
BorderStyle = LineStyle.Single,
- Visible = false // We default to HSV so hide old one
+ Visible = false // We default to HSV so hide old one
};
- backgroundColorPicker16.ColorChanged += BackgroundColor_ColorChanged;
- app.Add (backgroundColorPicker16);
-
+ _backgroundColorPicker16.ColorChanged += BackgroundColor_ColorChanged;
+ app.Add (_backgroundColorPicker16);
// Demo Label.
- _demoView = new View
+ _demoView = new ()
{
Title = "Color Sample",
Text = "Lorem Ipsum",
@@ -124,8 +119,7 @@ public class ColorPickers : Scenario
};
app.Add (_demoView);
-
- var osColorModel = new OptionSelector ()
+ var osColorModel = new OptionSelector
{
Y = Pos.Bottom (_demoView),
Width = Dim.Auto (),
@@ -137,87 +131,38 @@ public class ColorPickers : Scenario
"H_SL",
"_16 Colors"
],
- Value = (int)foregroundColorPicker.Style.ColorModel,
+ Value = (int)_foregroundColorPicker.Style.ColorModel
};
- osColorModel.ValueChanged += (_, e) =>
- {
- // 16 colors
- if (e.Value == 3)
- {
-
- foregroundColorPicker16.Visible = true;
- foregroundColorPicker.Visible = false;
-
- backgroundColorPicker16.Visible = true;
- backgroundColorPicker.Visible = false;
-
- // Switching to 16 colors
- ForegroundColor_ColorChanged (null, null);
- BackgroundColor_ColorChanged (null, null);
- }
- else
- {
- foregroundColorPicker16.Visible = false;
- foregroundColorPicker.Visible = true;
-
- if (e.Value is { })
- {
- foregroundColorPicker.Style.ColorModel = (ColorModel)e.Value;
- foregroundColorPicker.ApplyStyleChanges ();
-
- backgroundColorPicker16.Visible = false;
- backgroundColorPicker.Visible = true;
- backgroundColorPicker.Style.ColorModel = (ColorModel)e.Value;
- }
-
- backgroundColorPicker.ApplyStyleChanges ();
-
-
- // Switching to true colors
- foregroundColorPicker.SelectedColor = foregroundColorPicker16.SelectedColor;
- backgroundColorPicker.SelectedColor = backgroundColorPicker16.SelectedColor;
- }
- };
+ osColorModel.ValueChanged += OnOsColorModelOnValueChanged;
app.Add (osColorModel);
// Checkbox for switching show text fields on and off
- var cbShowTextFields = new CheckBox ()
+ var cbShowTextFields = new CheckBox
{
Text = "Show _Text Fields",
Y = Pos.Bottom (osColorModel) + 1,
Width = Dim.Auto (),
Height = Dim.Auto (),
- CheckedState = foregroundColorPicker.Style.ShowTextFields ? CheckState.Checked : CheckState.UnChecked,
+ CheckedState = _foregroundColorPicker.Style.ShowTextFields ? CheckState.Checked : CheckState.UnChecked
};
- cbShowTextFields.CheckedStateChanging += (_, e) =>
- {
- foregroundColorPicker.Style.ShowTextFields = e.Result == CheckState.Checked;
- foregroundColorPicker.ApplyStyleChanges ();
- backgroundColorPicker.Style.ShowTextFields = e.Result == CheckState.Checked;
- backgroundColorPicker.ApplyStyleChanges ();
- };
+ cbShowTextFields.CheckedStateChanging += OnCbShowTextFieldsOnCheckedStateChanging;
app.Add (cbShowTextFields);
// Checkbox for switching show text fields on and off
- var cbShowName = new CheckBox ()
+ var cbShowName = new CheckBox
{
Text = "Show Color _Name",
Y = Pos.Bottom (cbShowTextFields) + 1,
Width = Dim.Auto (),
Height = Dim.Auto (),
- CheckedState = foregroundColorPicker.Style.ShowColorName ? CheckState.Checked : CheckState.UnChecked,
+ CheckedState = _foregroundColorPicker.Style.ShowColorName ? CheckState.Checked : CheckState.UnChecked
};
- cbShowName.CheckedStateChanging += (_, e) =>
- {
- foregroundColorPicker.Style.ShowColorName = e.Result == CheckState.Checked;
- foregroundColorPicker.ApplyStyleChanges ();
- backgroundColorPicker.Style.ShowColorName = e.Result == CheckState.Checked;
- backgroundColorPicker.ApplyStyleChanges ();
- };
+ cbShowName.CheckedStateChanging += OnCbShowTextFieldsOnCheckedStateChanging;
+
app.Add (cbShowName);
var lblDriverName = new Label
@@ -247,41 +192,88 @@ public class ColorPickers : Scenario
};
cbUseTrueColor.CheckedStateChanging += (_, evt) => { Application.Force16Colors = evt.Result == CheckState.Checked; };
app.Add (lblDriverName, cbSupportsTrueColor, cbUseTrueColor);
+
// Set default colors.
- foregroundColorPicker.SelectedColor = _demoView.SuperView!.GetAttributeForRole (VisualRole.Normal).Foreground.GetClosestNamedColor16 ();
- backgroundColorPicker.SelectedColor = _demoView.SuperView.GetAttributeForRole (VisualRole.Normal).Background.GetClosestNamedColor16 ();
+ _foregroundColorPicker.SelectedColor = _demoView.SuperView!.GetAttributeForRole (VisualRole.Normal).Foreground.GetClosestNamedColor16 ();
+ _backgroundColorPicker.SelectedColor = _demoView.SuperView.GetAttributeForRole (VisualRole.Normal).Background.GetClosestNamedColor16 ();
Application.Run (app);
app.Dispose ();
Application.Shutdown ();
+
+ return;
+
+ void OnCbShowTextFieldsOnCheckedStateChanging (object _, ResultEventArgs e)
+ {
+ _foregroundColorPicker.Style.ShowTextFields = e.Result == CheckState.Checked;
+ _foregroundColorPicker.ApplyStyleChanges ();
+ _backgroundColorPicker.Style.ShowTextFields = e.Result == CheckState.Checked;
+ _backgroundColorPicker.ApplyStyleChanges ();
+ }
+
+ void OnOsColorModelOnValueChanged (object _, EventArgs e)
+ {
+ // 16 colors
+ if (e.Value == 3)
+ {
+ _foregroundColorPicker16.Visible = true;
+ _foregroundColorPicker.Visible = false;
+
+ _backgroundColorPicker16.Visible = true;
+ _backgroundColorPicker.Visible = false;
+
+ // Switching to 16 colors
+ ForegroundColor_ColorChanged (null, null);
+ BackgroundColor_ColorChanged (null, null);
+ }
+ else
+ {
+ _foregroundColorPicker16.Visible = false;
+ _foregroundColorPicker.Visible = true;
+
+ if (e.Value is { })
+ {
+ _foregroundColorPicker.Style.ColorModel = (ColorModel)e.Value;
+ _foregroundColorPicker.ApplyStyleChanges ();
+
+ _backgroundColorPicker16.Visible = false;
+ _backgroundColorPicker.Visible = true;
+ _backgroundColorPicker.Style.ColorModel = (ColorModel)e.Value;
+ }
+
+ _backgroundColorPicker.ApplyStyleChanges ();
+
+ // Switching to true colors
+ _foregroundColorPicker.SelectedColor = _foregroundColorPicker16.SelectedColor;
+ _backgroundColorPicker.SelectedColor = _backgroundColorPicker16.SelectedColor;
+ }
+ }
}
/// Fired when background color is changed.
private void BackgroundColor_ColorChanged (object sender, ResultEventArgs e)
{
- UpdateColorLabel (_backgroundColorLabel,
- backgroundColorPicker.Visible ?
- backgroundColorPicker.SelectedColor :
- backgroundColorPicker16.SelectedColor
- );
+ UpdateColorLabel (
+ _backgroundColorLabel,
+ _backgroundColorPicker.Visible ? _backgroundColorPicker.SelectedColor : _backgroundColorPicker16.SelectedColor
+ );
UpdateDemoLabel ();
}
/// Fired when foreground color is changed.
private void ForegroundColor_ColorChanged (object sender, ResultEventArgs e)
{
- UpdateColorLabel (_foregroundColorLabel,
- foregroundColorPicker.Visible ?
- foregroundColorPicker.SelectedColor :
- foregroundColorPicker16.SelectedColor
- );
+ UpdateColorLabel (
+ _foregroundColorLabel,
+ _foregroundColorPicker.Visible ? _foregroundColorPicker.SelectedColor : _foregroundColorPicker16.SelectedColor
+ );
UpdateDemoLabel ();
}
/// Update a color label from his ColorPicker.
private void UpdateColorLabel (Label label, Color color)
{
- label.ClearViewport (null);
+ label.ClearViewport ();
label.Text =
$"{color} ({(int)color}) #{color.R:X2}{color.G:X2}{color.B:X2}";
@@ -290,17 +282,14 @@ public class ColorPickers : Scenario
/// Update Demo Label.
private void UpdateDemoLabel ()
{
- _demoView.SetScheme (new Scheme
- {
- Normal = new Attribute (
- foregroundColorPicker.Visible ?
- foregroundColorPicker.SelectedColor :
- foregroundColorPicker16.SelectedColor,
- backgroundColorPicker.Visible ?
- backgroundColorPicker.SelectedColor :
- backgroundColorPicker16.SelectedColor
- )
- });
+ _demoView.SetScheme (
+ new ()
+ {
+ Normal = new (
+ _foregroundColorPicker.Visible ? _foregroundColorPicker.SelectedColor : _foregroundColorPicker16.SelectedColor,
+ _backgroundColorPicker.Visible ? _backgroundColorPicker.SelectedColor : _backgroundColorPicker16.SelectedColor
+ )
+ });
}
public override List GetDemoKeyStrokes ()
@@ -310,7 +299,7 @@ public class ColorPickers : Scenario
Key.B.WithAlt
];
- for (int i = 0; i < 200; i++)
+ for (var i = 0; i < 200; i++)
{
keys.Add (Key.CursorRight);
}
@@ -318,7 +307,7 @@ public class ColorPickers : Scenario
keys.Add (Key.Tab);
keys.Add (Key.Tab);
- for (int i = 0; i < 200; i++)
+ for (var i = 0; i < 200; i++)
{
keys.Add (Key.CursorLeft);
}
@@ -326,7 +315,7 @@ public class ColorPickers : Scenario
keys.Add (Key.Tab);
keys.Add (Key.Tab);
- for (int i = 0; i < 200; i++)
+ for (var i = 0; i < 200; i++)
{
keys.Add (Key.CursorLeft);
}
diff --git a/Terminal.Gui/Drawing/Color/AnsiColorNameResolver.cs b/Terminal.Gui/Drawing/Color/AnsiColorNameResolver.cs
deleted file mode 100644
index 5a0ebc827..000000000
--- a/Terminal.Gui/Drawing/Color/AnsiColorNameResolver.cs
+++ /dev/null
@@ -1,68 +0,0 @@
-using System.Collections.Immutable;
-using System.Diagnostics.CodeAnalysis;
-
-namespace Terminal.Gui.Drawing;
-
-///
-/// Color name resolver for .
-///
-public class AnsiColorNameResolver : IColorNameResolver
-{
- private static readonly ImmutableArray _ansiColorNames = ImmutableArray.Create (Enum.GetNames ());
-
- ///
- public IEnumerable GetColorNames ()
- {
- return _ansiColorNames;
- }
-
- ///
- public bool TryNameColor (Color color, [NotNullWhen (true)] out string? name)
- {
- if (Color.TryGetExactNamedColor16 (color, out ColorName16 colorName16))
- {
- name = Color16Name (colorName16);
- return true;
- }
- name = null;
- return false;
- }
-
- ///
- public bool TryParseColor (ReadOnlySpan name, out Color color)
- {
- if (Enum.TryParse (name, ignoreCase: true, out ColorName16 colorName16) &&
- // Any numerical value converts to undefined enum value.
- Enum.IsDefined (colorName16))
- {
- color = new Color (colorName16);
- return true;
- }
- color = default;
- return false;
- }
-
- private static string Color16Name (ColorName16 color16)
- {
- return color16 switch
- {
- ColorName16.Black => nameof (ColorName16.Black),
- ColorName16.Blue => nameof (ColorName16.Blue),
- ColorName16.Green => nameof (ColorName16.Green),
- ColorName16.Cyan => nameof (ColorName16.Cyan),
- ColorName16.Red => nameof (ColorName16.Red),
- ColorName16.Magenta => nameof (ColorName16.Magenta),
- ColorName16.Yellow => nameof (ColorName16.Yellow),
- ColorName16.Gray => nameof (ColorName16.Gray),
- ColorName16.DarkGray => nameof (ColorName16.DarkGray),
- ColorName16.BrightBlue => nameof (ColorName16.BrightBlue),
- ColorName16.BrightGreen => nameof (ColorName16.BrightGreen),
- ColorName16.BrightCyan => nameof (ColorName16.BrightCyan),
- ColorName16.BrightRed => nameof (ColorName16.BrightRed),
- ColorName16.BrightMagenta => nameof (ColorName16.BrightMagenta),
- ColorName16.BrightYellow => nameof (ColorName16.BrightYellow),
- ColorName16.White => nameof (ColorName16.White),
- _ => throw new NotSupportedException ($"ColorName16 '{color16}' is not supported.")
- };
- }
-}
diff --git a/Terminal.Gui/Drawing/Color/Color.cs b/Terminal.Gui/Drawing/Color/Color.cs
index e83a05a73..61f16c6ed 100644
--- a/Terminal.Gui/Drawing/Color/Color.cs
+++ b/Terminal.Gui/Drawing/Color/Color.cs
@@ -11,8 +11,28 @@ namespace Terminal.Gui.Drawing;
///
/// Represents a 24-bit color encoded in ARGB32 format.
-///
+///
+/// The RGB components define the color identity (what color it is), while the alpha channel defines
+/// rendering intent (how transparent it should be when drawn).
+///
///
+///
+///
+/// When matching colors to standard color names (e.g., via ),
+/// the alpha channel is ignored. This means colors with the same RGB values but different alpha values
+/// will resolve to the same color name. This design supports transparency features while maintaining
+/// semantic color identity.
+///
+///
+/// While Terminal.Gui does not currently support alpha blending during rendering, the alpha channel
+/// is used to indicate rendering intent:
+///
+/// - Alpha = 0: Fully transparent (don't render)
+/// - Alpha = 255: Fully opaque (normal rendering)
+/// - Other values: Reserved for future alpha blending support
+///
+///
+///
///
///
///
@@ -23,8 +43,16 @@ public readonly partial record struct Color : ISpanParsable, IUtf8SpanPar
{
/// The value of the alpha channel component
///
- /// The alpha channel is not currently supported, so the value of the alpha channel bits will not affect
- /// rendering.
+ ///
+ /// The alpha channel represents rendering intent (transparency) rather than color identity.
+ /// Terminal.Gui does not currently perform alpha blending, but uses this value to determine
+ /// whether to render the color at all (alpha = 0 means don't render).
+ ///
+ ///
+ /// When matching colors to standard color names, the alpha channel is ignored. For example,
+ /// new Color(255, 0, 0, 255) and new Color(255, 0, 0, 128) will both be
+ /// identified as "Red".
+ ///
///
[JsonIgnore]
[field: FieldOffset (3)]
@@ -32,8 +60,8 @@ public readonly partial record struct Color : ISpanParsable, IUtf8SpanPar
/// The value of this as a in ARGB32 format.
///
- /// The alpha channel is not currently supported, so the value of the alpha channel bits will not affect
- /// rendering.
+ /// The alpha channel in the ARGB value represents rendering intent (transparency), not color identity.
+ /// When matching to standard color names, only the RGB components are considered.
///
[JsonIgnore]
[field: FieldOffset (0)]
@@ -134,8 +162,6 @@ public readonly partial record struct Color : ISpanParsable, IUtf8SpanPar
/// Initializes a new instance of the with all channels set to 0.
public Color () { Argb = 0u; }
- // TODO: ColorName and AnsiColorCode are only needed when a driver is in Force16Color mode and we
- // TODO: should be able to remove these from any non-Driver-specific usages.
/// Gets or sets the 3-byte/6-character hexadecimal value for each of the legacy 16-color values.
[ConfigurationProperty (Scope = typeof (SettingsScope), OmitClassName = true)]
public static Dictionary Colors16
@@ -203,31 +229,6 @@ public readonly partial record struct Color : ISpanParsable, IUtf8SpanPar
[MethodImpl (MethodImplOptions.AggressiveInlining)]
public bool IsClosestToNamedColor16 (in ColorName16 namedColor) { return GetClosestNamedColor16 () == namedColor; }
- ///
- /// Determines if the closest named to /> is the provided
- /// .
- ///
- ///
- /// The color to test against the value in
- /// .
- ///
- ///
- /// The to check if this is closer
- /// to than any other configured named color.
- ///
- ///
- /// if the closest named color to is the provided value.
- /// if any other named color is closer to than
- /// .
- ///
- ///
- /// If is equidistant from two named colors, the result of this method is not guaranteed
- /// to be determinate.
- ///
- [Pure]
- [MethodImpl (MethodImplOptions.AggressiveInlining)]
- public static bool IsColorClosestToNamedColor16 (in Color color, in ColorName16 namedColor) { return color.IsClosestToNamedColor16 (in namedColor); }
-
/// Gets the "closest" named color to this value.
///
///
@@ -240,15 +241,6 @@ public readonly partial record struct Color : ISpanParsable, IUtf8SpanPar
return ColorExtensions.ColorToName16Map!.MinBy (pair => CalculateColorDistance (inputColor, pair.Key)).Value;
}
- /// Converts the given color value to exact named color represented by .
- ///
- /// Successfully converted named color.
- /// True if conversion succeeded; otherwise false.
- internal static bool TryGetExactNamedColor16 (Color inputColor, out ColorName16 colorName16)
- {
- return ColorExtensions.ColorToName16Map!.TryGetValue (inputColor, out colorName16);
- }
-
[SkipLocalsInit]
private static float CalculateColorDistance (in Vector4 color1, in Vector4 color2) { return Vector4.Distance (color1, color2); }
@@ -297,16 +289,8 @@ public readonly partial record struct Color : ISpanParsable, IUtf8SpanPar
HSL? hsl = ColorConverter.RgbToHsl (new (R, G, B));
double lNorm = hsl.L / 255.0;
- double newL;
- if (lNorm < 0.5)
- {
- newL = Math.Min (1.0, lNorm + brightenAmount);
- }
- else
- {
- newL = Math.Max (0.0, lNorm - brightenAmount);
- }
+ double newL = lNorm < 0.5 ? Math.Min (1.0, lNorm + brightenAmount) : Math.Max (0.0, lNorm - brightenAmount);
if (Math.Abs (newL - lNorm) < 0.1)
{
diff --git a/Terminal.Gui/Drawing/Color/ColorModel.cs b/Terminal.Gui/Drawing/Color/ColorModel.cs
index 158c03236..7351daff0 100644
--- a/Terminal.Gui/Drawing/Color/ColorModel.cs
+++ b/Terminal.Gui/Drawing/Color/ColorModel.cs
@@ -1,23 +1,24 @@
+// ReSharper disable InconsistentNaming
+
namespace Terminal.Gui.Drawing;
///
-/// Describes away of modelling color e.g. Hue
-/// Saturation Lightness.
+/// Describes a way of modelling color e.g. Hue, Saturation, and Lightness.
///
public enum ColorModel
{
///
- /// Color modelled by storing Red, Green and Blue as (0-255) ints
+ /// Color modelled by storing Red, Green and Blue as (0-255) ints
///
RGB,
///
- /// Color modelled by storing Hue (360 degrees), Saturation (100%) and Value (100%)
+ /// Color modelled by storing Hue (360 degrees), Saturation (100%) and Value (100%)
///
HSV,
///
- /// Color modelled by storing Hue (360 degrees), Saturation (100%) and Lightness (100%)
+ /// Color modelled by storing Hue (360 degrees), Saturation (100%) and Lightness (100%)
///
HSL
}
diff --git a/Terminal.Gui/Drawing/Color/ColorQuantizer.cs b/Terminal.Gui/Drawing/Color/ColorQuantizer.cs
index b163d4f43..b0930b090 100644
--- a/Terminal.Gui/Drawing/Color/ColorQuantizer.cs
+++ b/Terminal.Gui/Drawing/Color/ColorQuantizer.cs
@@ -39,7 +39,7 @@ public class ColorQuantizer
///
public void BuildPalette (Color [,] pixels)
{
- List allColors = new ();
+ List allColors = [];
int width = pixels.GetLength (0);
int height = pixels.GetLength (1);
@@ -56,8 +56,8 @@ public class ColorQuantizer
}
///
- /// Returns the closest color in that matches
- /// based on the color comparison algorithm defined by
+ /// Returns the closest color in that matches
+ /// based on the color comparison algorithm defined by
///
///
///
diff --git a/Terminal.Gui/Drawing/Color/ColorStrings.cs b/Terminal.Gui/Drawing/Color/ColorStrings.cs
index 8f70c6a9e..4a8fd3fd2 100644
--- a/Terminal.Gui/Drawing/Color/ColorStrings.cs
+++ b/Terminal.Gui/Drawing/Color/ColorStrings.cs
@@ -7,47 +7,16 @@ namespace Terminal.Gui.Drawing;
///
public static class ColorStrings
{
- private static readonly AnsiColorNameResolver _ansi = new();
private static readonly StandardColorsNameResolver _standard = new();
- private static readonly MultiStandardColorNameResolver _multi = new();
///
- /// Gets the W3C+ standard string for .
- ///
- /// The color.
- /// if there is no standard color name for the specified color.
- public static string? GetStandardColorName (Color color)
- {
- if (_standard.TryNameColor (color, out string? name))
- {
- return name;
- }
- return null;
- }
-
- ///
- /// Gets the ANSI 4-bit (16) color name for .
- ///
- /// The color.
- /// if there is no standard color name for the specified color.
- // ReSharper disable once InconsistentNaming
- public static string? GetANSIColor16Name (Color color)
- {
- if (_ansi.TryNameColor (color, out string? name))
- {
- return name;
- }
- return null;
- }
-
- ///
- /// Gets backwards compatible color name for .
+ /// Gets the color name for .
///
/// The color.
/// Standard color name for the specified color; otherwise .
public static string? GetColorName (Color color)
{
- if (_multi.TryNameColor (color, out string? name))
+ if (_standard.TryNameColor (color, out string? name))
{
return name;
}
@@ -80,30 +49,14 @@ public static class ColorStrings
}
///
- /// Parses and returns if name is a ANSI 4-bit standard named color.
- ///
- /// The name to parse.
- /// If successful, the color.
- /// if was parsed successfully.
- public static bool TryParseColor16 (ReadOnlySpan name, out Color color)
- {
- if (_ansi.TryParseColor (name, out color))
- {
- return true;
- }
- color = default;
- return false;
- }
-
- ///
- /// Parses and returns if name is either ANSI 4-bit or W3C standard named color.
+ /// Parses and returns if name is a W3C+ standard named color.
///
/// The name to parse.
/// If successful, the color.
/// if was parsed successfully.
public static bool TryParseNamedColor (ReadOnlySpan name, out Color color)
{
- if (_multi.TryParseColor (name, out color))
+ if (_standard.TryParseColor (name, out color))
{
return true;
}
@@ -113,7 +66,7 @@ public static class ColorStrings
return true;
}
- color = default;
+ color = default (Color);
return false;
}
@@ -130,7 +83,7 @@ public static class ColorStrings
}
}
- color = default;
+ color = default (Color);
return false;
}
}
diff --git a/Terminal.Gui/Drawing/Color/MultiStandardColorNameResolver.cs b/Terminal.Gui/Drawing/Color/MultiStandardColorNameResolver.cs
deleted file mode 100644
index 36ed50910..000000000
--- a/Terminal.Gui/Drawing/Color/MultiStandardColorNameResolver.cs
+++ /dev/null
@@ -1,83 +0,0 @@
-using System.Collections.Frozen;
-using System.Collections.Immutable;
-using System.Diagnostics.CodeAnalysis;
-using System.Globalization;
-
-namespace Terminal.Gui.Drawing;
-
-///
-/// Color name resolver prioritizing Standard (W3C+) colors with fallback to ANSI 4-bit (16) colors.
-///
-public class MultiStandardColorNameResolver : IColorNameResolver
-{
- private static readonly AnsiColorNameResolver _ansi = new ();
- private static readonly StandardColorsNameResolver _standard = new ();
-
- private static readonly ImmutableArray _combinedColorNames;
- private static readonly FrozenDictionary _nameToColorMap;
- private static readonly FrozenDictionary _colorToNameMap;
-
- static MultiStandardColorNameResolver ()
- {
- Dictionary nameToColor = new (StringComparer.OrdinalIgnoreCase);
- Dictionary colorToName = new ();
-
- foreach (string name in _standard.GetColorNames ())
- {
- if (_standard.TryParseColor (name, out Color color))
- {
- if (nameToColor.TryAdd (name, color))
- {
- _ = colorToName.TryAdd (color.Argb, name);
- }
- }
- }
-
- foreach (string name in _ansi.GetColorNames ())
- {
- if (_ansi.TryParseColor (name, out Color color))
- {
- nameToColor.TryAdd (name, color);
- colorToName.TryAdd (color.Argb, name);
- }
- }
-
- _nameToColorMap = nameToColor.ToFrozenDictionary ();
- _colorToNameMap = colorToName.ToFrozenDictionary ();
- _combinedColorNames = nameToColor.Keys.Order ().ToImmutableArray ();
- }
-
- ///
- public IEnumerable GetColorNames () => _combinedColorNames;
-
- ///
- public bool TryParseColor (ReadOnlySpan name, out Color color)
- {
- if (name.StartsWith ("#") || name.StartsWith ("0x", StringComparison.OrdinalIgnoreCase))
- {
- try
- {
- color = Color.Parse (name.ToString (), CultureInfo.InvariantCulture);
- return true;
- }
- catch
- {
- color = default;
- return false;
- }
- }
-
- if (_ansi.TryParseColor (name, out color)) return true;
- if (_standard.TryParseColor (name, out color)) return true;
-
- color = default;
- return false;
- }
-
-
- ///
- public bool TryNameColor (Color color, [NotNullWhen (true)] out string? name)
- {
- return _colorToNameMap.TryGetValue (color.Argb, out name);
- }
-}
diff --git a/Terminal.Gui/Drawing/Color/StandardColor.cs b/Terminal.Gui/Drawing/Color/StandardColor.cs
index 45d586a03..0cec9da78 100644
--- a/Terminal.Gui/Drawing/Color/StandardColor.cs
+++ b/Terminal.Gui/Drawing/Color/StandardColor.cs
@@ -1248,5 +1248,64 @@ public enum StandardColor
/// A bright yellowish-green color.
///
///
- YellowGreen = 0x9ACD32
+ YellowGreen = 0x9ACD32,
+
+ // Legacy 16-color names for backwards compatibility
+ // These match the RGB values used in ColorName16 mapping
+
+ ///
+ /// Bright Black RGB(118, 118, 118).
+ ///
+ /// A bright black (dark gray) color from the legacy 16-color palette. This is the ColorName16.DarkGray color.
+ ///
+ ///
+ BrightBlack = 0x767676,
+
+ ///
+ /// Bright Blue RGB(59, 120, 255).
+ ///
+ /// A bright blue color from the legacy 16-color palette.
+ ///
+ ///
+ BrightBlue = 0x3B78FF,
+
+ ///
+ /// Bright Cyan RGB(97, 214, 214).
+ ///
+ /// A bright cyan color from the legacy 16-color palette.
+ ///
+ ///
+ BrightCyan = 0x61D6D6,
+
+ ///
+ /// Bright Green RGB(22, 198, 12).
+ ///
+ /// A bright green color from the legacy 16-color palette.
+ ///
+ ///
+ BrightGreen = 0x16C60C,
+
+ ///
+ /// Bright Magenta RGB(180, 0, 158).
+ ///
+ /// A bright magenta color from the legacy 16-color palette.
+ ///
+ ///
+ BrightMagenta = 0xB4009E,
+
+ ///
+ /// Bright Red RGB(231, 72, 86).
+ ///
+ /// A bright red color from the legacy 16-color palette.
+ ///
+ ///
+ BrightRed = 0xE74856,
+
+ ///
+ /// Bright Yellow RGB(249, 241, 165).
+ ///
+ /// A bright yellow color from the legacy 16-color palette.
+ ///
+ ///
+ BrightYellow = 0xF9F1A5
}
diff --git a/Terminal.Gui/Drawing/Color/StandardColors.cs b/Terminal.Gui/Drawing/Color/StandardColors.cs
index 9f17ac1ac..1d3e99919 100644
--- a/Terminal.Gui/Drawing/Color/StandardColors.cs
+++ b/Terminal.Gui/Drawing/Color/StandardColors.cs
@@ -18,7 +18,7 @@ internal static class StandardColors
{
string [] standardNames = Enum.GetNames ().Order ().ToArray ();
- return ImmutableArray.Create (standardNames);
+ return [.. standardNames];
}
private static readonly Lazy> _argbNameMap = new (
@@ -82,7 +82,10 @@ internal static class StandardColors
/// True if conversion succeeded; otherwise false.
public static bool TryNameColor (Color color, [NotNullWhen (true)] out string? name)
{
- if (_argbNameMap.Value.TryGetValue (color.Argb, out name))
+ // Ignore alpha channel when matching - alpha represents transparency, not color identity
+ uint opaqueArgb = color.Argb | 0xFF000000;
+
+ if (_argbNameMap.Value.TryGetValue (opaqueArgb, out name))
{
return true;
}
diff --git a/Terminal.Gui/Views/Color/BBar.cs b/Terminal.Gui/Views/Color/BBar.cs
index 11c5cc9df..2a1220832 100644
--- a/Terminal.Gui/Views/Color/BBar.cs
+++ b/Terminal.Gui/Views/Color/BBar.cs
@@ -1,5 +1,3 @@
-
-
using ColorHelper;
namespace Terminal.Gui.Views;
diff --git a/Terminal.Gui/Views/Color/ColorBar.cs b/Terminal.Gui/Views/Color/ColorBar.cs
index c3590d955..66503d21c 100644
--- a/Terminal.Gui/Views/Color/ColorBar.cs
+++ b/Terminal.Gui/Views/Color/ColorBar.cs
@@ -1,5 +1,3 @@
-
-
using ColorHelper;
namespace Terminal.Gui.Views;
@@ -15,7 +13,7 @@ internal abstract class ColorBar : View, IColorBar
///
protected ColorBar ()
{
- Height = Dim.Auto(minimumContentDim: 1);
+ Height = Dim.Auto (minimumContentDim: 1);
Width = Dim.Fill ();
CanFocus = true;
@@ -135,7 +133,6 @@ internal abstract class ColorBar : View, IColorBar
mouseEvent.Handled = true;
SetFocus ();
-
}
return mouseEvent.Handled;
diff --git a/Terminal.Gui/Views/Color/ColorModelStrategy.cs b/Terminal.Gui/Views/Color/ColorModelStrategy.cs
index 0f803be18..78deb3956 100644
--- a/Terminal.Gui/Views/Color/ColorModelStrategy.cs
+++ b/Terminal.Gui/Views/Color/ColorModelStrategy.cs
@@ -1,5 +1,3 @@
-
-
using ColorHelper;
using ColorConverter = ColorHelper.ColorConverter;
@@ -7,39 +5,23 @@ namespace Terminal.Gui.Views;
internal class ColorModelStrategy
{
- public IEnumerable CreateBars (ColorModel model)
- {
- switch (model)
+ public IEnumerable CreateBars (ColorModel model) =>
+ model switch
{
- case ColorModel.RGB:
- return CreateRgbBars ();
- case ColorModel.HSV:
- return CreateHsvBars ();
- case ColorModel.HSL:
- return CreateHslBars ();
- default:
- throw new ArgumentOutOfRangeException (nameof (model), model, null);
- }
- }
+ ColorModel.RGB => CreateRgbBars (),
+ ColorModel.HSV => CreateHsvBars (),
+ ColorModel.HSL => CreateHslBars (),
+ _ => throw new ArgumentOutOfRangeException (nameof (model), model, null)
+ };
- public Color GetColorFromBars (IList bars, ColorModel model)
- {
- switch (model)
+ public Color GetColorFromBars (IList bars, ColorModel model) =>
+ model switch
{
- case ColorModel.RGB:
- return ToColor (new ((byte)bars [0].Value, (byte)bars [1].Value, (byte)bars [2].Value));
- case ColorModel.HSV:
- return ToColor (
- ColorConverter.HsvToRgb (new (bars [0].Value, (byte)bars [1].Value, (byte)bars [2].Value))
- );
- case ColorModel.HSL:
- return ToColor (
- ColorConverter.HslToRgb (new (bars [0].Value, (byte)bars [1].Value, (byte)bars [2].Value))
- );
- default:
- throw new ArgumentOutOfRangeException (nameof (model), model, null);
- }
- }
+ ColorModel.RGB => ToColor (new ((byte)bars [0].Value, (byte)bars [1].Value, (byte)bars [2].Value)),
+ ColorModel.HSV => ToColor (ColorConverter.HsvToRgb (new (bars [0].Value, (byte)bars [1].Value, (byte)bars [2].Value))),
+ ColorModel.HSL => ToColor (ColorConverter.HslToRgb (new (bars [0].Value, (byte)bars [1].Value, (byte)bars [2].Value))),
+ _ => throw new ArgumentOutOfRangeException (nameof (model), model, null)
+ };
public void SetBarsToColor (IList bars, Color newValue, ColorModel model)
{
@@ -47,6 +29,7 @@ internal class ColorModelStrategy
{
return;
}
+
switch (model)
{
case ColorModel.RGB:
@@ -75,21 +58,21 @@ internal class ColorModelStrategy
}
}
- private IEnumerable CreateHslBars ()
+ private static IEnumerable CreateHslBars ()
{
- var h = new HueBar
+ HueBar h = new ()
{
Text = "H:"
};
yield return h;
- var s = new SaturationBar
+ SaturationBar s = new ()
{
Text = "S:"
};
- var l = new LightnessBar
+ LightnessBar l = new ()
{
Text = "L:"
};
@@ -104,21 +87,21 @@ internal class ColorModelStrategy
yield return l;
}
- private IEnumerable CreateHsvBars ()
+ private static IEnumerable CreateHsvBars ()
{
- var h = new HueBar
+ HueBar h = new ()
{
Text = "H:"
};
yield return h;
- var s = new SaturationBar
+ SaturationBar s = new ()
{
Text = "S:"
};
- var v = new ValueBar
+ ValueBar v = new ()
{
Text = "V:"
};
@@ -133,19 +116,19 @@ internal class ColorModelStrategy
yield return v;
}
- private IEnumerable CreateRgbBars ()
+ private static IEnumerable CreateRgbBars ()
{
- var r = new RBar
+ RBar r = new ()
{
Text = "R:"
};
- var g = new GBar
+ GBar g = new ()
{
Text = "G:"
};
- var b = new BBar
+ BBar b = new ()
{
Text = "B:"
};
@@ -163,5 +146,5 @@ internal class ColorModelStrategy
yield return b;
}
- private Color ToColor (RGB rgb) { return new (rgb.R, rgb.G, rgb.B); }
+ private static Color ToColor (RGB rgb) { return new (rgb.R, rgb.G, rgb.B); }
}
diff --git a/Terminal.Gui/Views/Color/ColorPicker.Style.cs b/Terminal.Gui/Views/Color/ColorPicker.Style.cs
index afab89d5a..89a9bda8f 100644
--- a/Terminal.Gui/Views/Color/ColorPicker.Style.cs
+++ b/Terminal.Gui/Views/Color/ColorPicker.Style.cs
@@ -1,6 +1,3 @@
-
-
-
namespace Terminal.Gui.Views;
///
diff --git a/Terminal.Gui/Views/Color/GBar.cs b/Terminal.Gui/Views/Color/GBar.cs
index b9dd5b435..b3f3695db 100644
--- a/Terminal.Gui/Views/Color/GBar.cs
+++ b/Terminal.Gui/Views/Color/GBar.cs
@@ -1,5 +1,3 @@
-
-
using ColorHelper;
namespace Terminal.Gui.Views;
diff --git a/Terminal.Gui/Views/Color/IColorBar.cs b/Terminal.Gui/Views/Color/IColorBar.cs
index 176edead9..7197c6534 100644
--- a/Terminal.Gui/Views/Color/IColorBar.cs
+++ b/Terminal.Gui/Views/Color/IColorBar.cs
@@ -1,5 +1,4 @@
-#nullable disable
-namespace Terminal.Gui.Views;
+namespace Terminal.Gui.Views;
internal interface IColorBar
{
diff --git a/Terminal.Gui/Views/Color/LightnessBar.cs b/Terminal.Gui/Views/Color/LightnessBar.cs
index f3d1e3942..cd7627388 100644
--- a/Terminal.Gui/Views/Color/LightnessBar.cs
+++ b/Terminal.Gui/Views/Color/LightnessBar.cs
@@ -1,5 +1,3 @@
-
-
using ColorHelper;
using ColorConverter = ColorHelper.ColorConverter;
diff --git a/Terminal.Gui/Views/Color/RBar.cs b/Terminal.Gui/Views/Color/RBar.cs
index e71dd9246..f4647f227 100644
--- a/Terminal.Gui/Views/Color/RBar.cs
+++ b/Terminal.Gui/Views/Color/RBar.cs
@@ -1,5 +1,3 @@
-
-
using ColorHelper;
namespace Terminal.Gui.Views;
diff --git a/Terminal.Gui/Views/Color/SaturationBar.cs b/Terminal.Gui/Views/Color/SaturationBar.cs
index 9be7ab7f7..2d122abcb 100644
--- a/Terminal.Gui/Views/Color/SaturationBar.cs
+++ b/Terminal.Gui/Views/Color/SaturationBar.cs
@@ -1,5 +1,3 @@
-
-
using ColorHelper;
using ColorConverter = ColorHelper.ColorConverter;
diff --git a/Terminal.sln.DotSettings b/Terminal.sln.DotSettings
index 44cd244e2..7d566d39e 100644
--- a/Terminal.sln.DotSettings
+++ b/Terminal.sln.DotSettings
@@ -424,6 +424,7 @@
True
True
True
+ True
True
True
True
diff --git a/Tests/StressTests/ApplicationStressTests.cs b/Tests/StressTests/ApplicationStressTests.cs
index cb51ac940..d06f797ce 100644
--- a/Tests/StressTests/ApplicationStressTests.cs
+++ b/Tests/StressTests/ApplicationStressTests.cs
@@ -5,7 +5,7 @@ using Xunit.Abstractions;
namespace StressTests;
-public class ApplicationStressTests (ITestOutputHelper output)
+public class ApplicationStressTests
{
private const int NUM_INCREMENTS = 500;
diff --git a/Tests/UnitTestsParallelizable/Application/TimeoutTests.cs b/Tests/UnitTestsParallelizable/Application/TimeoutTests.cs
index d4d675491..75e3de809 100644
--- a/Tests/UnitTestsParallelizable/Application/TimeoutTests.cs
+++ b/Tests/UnitTestsParallelizable/Application/TimeoutTests.cs
@@ -9,7 +9,7 @@ namespace ApplicationTests.Timeout;
/// These tests verify that timeouts fire correctly, can be added/removed,
/// handle exceptions properly, and work with Application.Run() calls.
///
-public class TimeoutTests (ITestOutputHelper output)
+public class TimeoutTests
{
[Fact]
public void AddTimeout_Callback_Can_Add_New_Timeout ()
diff --git a/Tests/UnitTestsParallelizable/Configuration/ColorJsonConverterTests.cs b/Tests/UnitTestsParallelizable/Configuration/ColorJsonConverterTests.cs
index f01cdc950..a2230f8e4 100644
--- a/Tests/UnitTestsParallelizable/Configuration/ColorJsonConverterTests.cs
+++ b/Tests/UnitTestsParallelizable/Configuration/ColorJsonConverterTests.cs
@@ -52,7 +52,7 @@ public class ColorJsonConverterTests
[InlineData (ColorName16.Red, "Red")]
[InlineData (ColorName16.Magenta, "Fuchsia")] // W3C+ Standard overrides
[InlineData (ColorName16.Yellow, "Yellow")]
- [InlineData (ColorName16.DarkGray, "DarkGray")]
+ [InlineData (ColorName16.DarkGray, "BrightBlack")] // Legacy ColorName16.DarkGray now serializes as BrightBlack (first alphabetical match)
[InlineData (ColorName16.BrightBlue, "BrightBlue")]
[InlineData (ColorName16.BrightGreen, "BrightGreen")]
[InlineData (ColorName16.BrightCyan, "BrightCyan")]
@@ -98,7 +98,7 @@ public class ColorJsonConverterTests
[InlineData ("BrightYellow", Color.BrightYellow)]
[InlineData ("Yellow", Color.Yellow)]
[InlineData ("Cyan", Color.Cyan)]
- [InlineData ("DarkGray", Color.DarkGray)]
+ [InlineData ("BrightBlack", Color.DarkGray)] // Legacy ColorName16.DarkGray is now accessible as BrightBlack
[InlineData ("Gray", Color.Gray)]
[InlineData ("Green", Color.Green)]
[InlineData ("Magenta", Color.Magenta)]
@@ -113,7 +113,7 @@ public class ColorJsonConverterTests
var actualColor = JsonSerializer.Deserialize (json, JsonOptions);
// Assert
- Assert.Equal (new Color (expectedColor), actualColor);
+ Assert.Equal (new (expectedColor), actualColor);
}
[Fact]
diff --git a/Tests/UnitTestsParallelizable/Configuration/SourcesManagerTests.cs b/Tests/UnitTestsParallelizable/Configuration/SourcesManagerTests.cs
index fd37edbd1..1bca6304e 100644
--- a/Tests/UnitTestsParallelizable/Configuration/SourcesManagerTests.cs
+++ b/Tests/UnitTestsParallelizable/Configuration/SourcesManagerTests.cs
@@ -12,7 +12,7 @@ public class SourcesManagerTests
// Arrange
var sourcesManager = new SourcesManager ();
var stream = new MemoryStream ();
- var source = "test.json";
+ var source = "Load_WithNullSettingsScope_ReturnsFalse";
var location = ConfigLocations.AppCurrent;
// Act
@@ -37,7 +37,7 @@ public class SourcesManagerTests
}
""";
var location = ConfigLocations.HardCoded;
- var source = "stream";
+ var source = "Load_WithValidStream_UpdatesSettingsScope";
var stream = new MemoryStream ();
var writer = new StreamWriter (stream);
@@ -69,7 +69,7 @@ public class SourcesManagerTests
writer.Flush ();
stream.Position = 0;
- var source = "test.json";
+ var source = "Load_WithInvalidJson_AddsJsonError";
var location = ConfigLocations.AppCurrent;
// Act
@@ -180,7 +180,7 @@ public class SourcesManagerTests
var sourcesManager = new SourcesManager ();
var settingsScope = new SettingsScope ();
- var source = "test.json";
+ var source = "Load_WithNullOrEmptyJson_ReturnsFalse";
var location = ConfigLocations.AppCurrent;
// Act
@@ -206,7 +206,7 @@ public class SourcesManagerTests
"Application.QuitKey": "Ctrl+Z"
}
""";
- var source = "test.json";
+ var source = "Load_WithValidJson_UpdatesSettingsScope";
var location = ConfigLocations.HardCoded;
// Act
@@ -233,7 +233,7 @@ public class SourcesManagerTests
// "Button.DefaultShadowStyle": "None"
// }
// """;
- // var source = "test.json";
+ // var source = "Update_WithValidJson_UpdatesThemeScope";
// var location = ConfigLocations.HardCoded;
// // Act
diff --git a/Tests/UnitTestsParallelizable/Drawing/Color/AnsiColorNameResolverTests.cs b/Tests/UnitTestsParallelizable/Drawing/Color/AnsiColorNameResolverTests.cs
deleted file mode 100644
index bf40a17bc..000000000
--- a/Tests/UnitTestsParallelizable/Drawing/Color/AnsiColorNameResolverTests.cs
+++ /dev/null
@@ -1,168 +0,0 @@
-#nullable enable
-
-namespace DrawingTests;
-
-public class AnsiColorNameResolverTests
-{
- private readonly AnsiColorNameResolver _candidate = new ();
- [Fact]
- public void TryNameColor_Resolves_All_ColorName16 ()
- {
- var resolver = new AnsiColorNameResolver ();
-
- foreach (ColorName16 name in Enum.GetValues ())
- {
- var color = new Color (name);
- bool success = resolver.TryNameColor (color, out string? resultName);
-
- Assert.True (success, $"Expected TryNameColor to succeed for {name}");
- Assert.Equal (name.ToString (), resultName);
- }
- }
-
- [Fact]
- public void TryParseColor_Resolves_All_ColorName16_Names ()
- {
- var resolver = new AnsiColorNameResolver ();
-
- foreach (ColorName16 name in Enum.GetValues ())
- {
- bool success = resolver.TryParseColor (name.ToString (), out Color parsed);
-
- Assert.True (success, $"Expected TryParseColor to succeed for {name}");
- Assert.Equal (new Color (name), parsed);
- }
- }
-
- public static IEnumerable