Addressed review feedback.

Renamed ColorName to ColorName16 to make it clear it's purpose.
Added related TODOs.
This commit is contained in:
Tig
2024-09-23 09:53:52 -06:00
parent 0684c86b77
commit 05ae84382d
35 changed files with 292 additions and 409 deletions

View File

@@ -10,7 +10,7 @@ public static partial class Application // Driver abstractions
/// <summary>
/// Gets or sets whether <see cref="Application.Driver"/> will be forced to output only the 16 colors defined in
/// <see cref="ColorName"/>. The default is <see langword="false"/>, meaning 24-bit (TrueColor) colors will be output
/// <see cref="ColorName16"/>. The default is <see langword="false"/>, meaning 24-bit (TrueColor) colors will be output
/// as long as the selected <see cref="ConsoleDriver"/> supports TrueColor.
/// </summary>
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))]

View File

@@ -1,8 +1,6 @@
#nullable enable
using System.ComponentModel;
using System.Diagnostics;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.VisualBasic.Syntax;
namespace Terminal.Gui;

View File

@@ -37,7 +37,7 @@ internal class ColorJsonConverter : JsonConverter<Color>
return new (color1);
}
if (Enum.TryParse (colorString, true, out ColorName color))
if (Enum.TryParse (colorString, true, out ColorName16 color))
{
// Return the parsed color
return new (in color);

View File

@@ -17,7 +17,7 @@ namespace Terminal.Gui;
[JsonSerializable (typeof (ShadowStyle))]
[JsonSerializable (typeof (HighlightStyle))]
[JsonSerializable (typeof (bool?))]
[JsonSerializable (typeof (Dictionary<ColorName, string>))]
[JsonSerializable (typeof (Dictionary<ColorName16, string>))]
[JsonSerializable (typeof (Dictionary<string, ThemeScope>))]
[JsonSerializable (typeof (Dictionary<string, ColorScheme>))]
internal partial class SourceGenerationContext : JsonSerializerContext

View File

@@ -374,7 +374,7 @@ internal class CursesDriver : ConsoleDriver
);
}
CurrentAttribute = new Attribute (ColorName.White, ColorName.Black);
CurrentAttribute = new Attribute (ColorName16.White, ColorName16.Black);
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
@@ -859,8 +859,8 @@ internal class CursesDriver : ConsoleDriver
return new Attribute (
Curses.ColorPair (v),
CursesColorNumberToColorName (foreground),
CursesColorNumberToColorName (background)
CursesColorNumberToColorName16 (foreground),
CursesColorNumberToColorName16 (background)
);
}
@@ -875,8 +875,8 @@ internal class CursesDriver : ConsoleDriver
if (!RunningUnitTests)
{
return MakeColor (
ColorNameToCursesColorNumber (foreground.GetClosestNamedColor ()),
ColorNameToCursesColorNumber (background.GetClosestNamedColor ())
ColorNameToCursesColorNumber (foreground.GetClosestNamedColor16 ()),
ColorNameToCursesColorNumber (background.GetClosestNamedColor16 ())
);
}
@@ -887,83 +887,83 @@ internal class CursesDriver : ConsoleDriver
);
}
private static short ColorNameToCursesColorNumber (ColorName color)
private static short ColorNameToCursesColorNumber (ColorName16 color)
{
switch (color)
{
case ColorName.Black:
case ColorName16.Black:
return Curses.COLOR_BLACK;
case ColorName.Blue:
case ColorName16.Blue:
return Curses.COLOR_BLUE;
case ColorName.Green:
case ColorName16.Green:
return Curses.COLOR_GREEN;
case ColorName.Cyan:
case ColorName16.Cyan:
return Curses.COLOR_CYAN;
case ColorName.Red:
case ColorName16.Red:
return Curses.COLOR_RED;
case ColorName.Magenta:
case ColorName16.Magenta:
return Curses.COLOR_MAGENTA;
case ColorName.Yellow:
case ColorName16.Yellow:
return Curses.COLOR_YELLOW;
case ColorName.Gray:
case ColorName16.Gray:
return Curses.COLOR_WHITE;
case ColorName.DarkGray:
case ColorName16.DarkGray:
return Curses.COLOR_GRAY;
case ColorName.BrightBlue:
case ColorName16.BrightBlue:
return Curses.COLOR_BLUE | Curses.COLOR_GRAY;
case ColorName.BrightGreen:
case ColorName16.BrightGreen:
return Curses.COLOR_GREEN | Curses.COLOR_GRAY;
case ColorName.BrightCyan:
case ColorName16.BrightCyan:
return Curses.COLOR_CYAN | Curses.COLOR_GRAY;
case ColorName.BrightRed:
case ColorName16.BrightRed:
return Curses.COLOR_RED | Curses.COLOR_GRAY;
case ColorName.BrightMagenta:
case ColorName16.BrightMagenta:
return Curses.COLOR_MAGENTA | Curses.COLOR_GRAY;
case ColorName.BrightYellow:
case ColorName16.BrightYellow:
return Curses.COLOR_YELLOW | Curses.COLOR_GRAY;
case ColorName.White:
case ColorName16.White:
return Curses.COLOR_WHITE | Curses.COLOR_GRAY;
}
throw new ArgumentException ("Invalid color code");
}
private static ColorName CursesColorNumberToColorName (short color)
private static ColorName16 CursesColorNumberToColorName16 (short color)
{
switch (color)
{
case Curses.COLOR_BLACK:
return ColorName.Black;
return ColorName16.Black;
case Curses.COLOR_BLUE:
return ColorName.Blue;
return ColorName16.Blue;
case Curses.COLOR_GREEN:
return ColorName.Green;
return ColorName16.Green;
case Curses.COLOR_CYAN:
return ColorName.Cyan;
return ColorName16.Cyan;
case Curses.COLOR_RED:
return ColorName.Red;
return ColorName16.Red;
case Curses.COLOR_MAGENTA:
return ColorName.Magenta;
return ColorName16.Magenta;
case Curses.COLOR_YELLOW:
return ColorName.Yellow;
return ColorName16.Yellow;
case Curses.COLOR_WHITE:
return ColorName.Gray;
return ColorName16.Gray;
case Curses.COLOR_GRAY:
return ColorName.DarkGray;
return ColorName16.DarkGray;
case Curses.COLOR_BLUE | Curses.COLOR_GRAY:
return ColorName.BrightBlue;
return ColorName16.BrightBlue;
case Curses.COLOR_GREEN | Curses.COLOR_GRAY:
return ColorName.BrightGreen;
return ColorName16.BrightGreen;
case Curses.COLOR_CYAN | Curses.COLOR_GRAY:
return ColorName.BrightCyan;
return ColorName16.BrightCyan;
case Curses.COLOR_RED | Curses.COLOR_GRAY:
return ColorName.BrightRed;
return ColorName16.BrightRed;
case Curses.COLOR_MAGENTA | Curses.COLOR_GRAY:
return ColorName.BrightMagenta;
return ColorName16.BrightMagenta;
case Curses.COLOR_YELLOW | Curses.COLOR_GRAY:
return ColorName.BrightYellow;
return ColorName16.BrightYellow;
case Curses.COLOR_WHITE | Curses.COLOR_GRAY:
return ColorName.White;
return ColorName16.White;
}
throw new ArgumentException ("Invalid curses color code");

View File

@@ -165,8 +165,8 @@ public class FakeDriver : ConsoleDriver
if (attr != redrawAttr)
{
redrawAttr = attr;
FakeConsole.ForegroundColor = (ConsoleColor)attr.Foreground.GetClosestNamedColor ();
FakeConsole.BackgroundColor = (ConsoleColor)attr.Background.GetClosestNamedColor ();
FakeConsole.ForegroundColor = (ConsoleColor)attr.Foreground.GetClosestNamedColor16 ();
FakeConsole.BackgroundColor = (ConsoleColor)attr.Background.GetClosestNamedColor16 ();
}
outputWidth++;

View File

@@ -961,10 +961,10 @@ internal class NetDriver : ConsoleDriver
output.Append (
EscSeqUtils.CSI_SetGraphicsRendition (
MapColors (
(ConsoleColor)attr.Background.GetClosestNamedColor (),
(ConsoleColor)attr.Background.GetClosestNamedColor16 (),
false
),
MapColors ((ConsoleColor)attr.Foreground.GetClosestNamedColor ())
MapColors ((ConsoleColor)attr.Foreground.GetClosestNamedColor16 ())
)
);
}

View File

@@ -72,7 +72,7 @@ internal class WindowsConsole
{
Char = new CharUnion { UnicodeChar = info.Char },
Attributes =
(ushort)((int)info.Attribute.Foreground.GetClosestNamedColor () | ((int)info.Attribute.Background.GetClosestNamedColor () << 4))
(ushort)((int)info.Attribute.Foreground.GetClosestNamedColor16 () | ((int)info.Attribute.Background.GetClosestNamedColor16 () << 4))
};
}

View File

@@ -15,7 +15,7 @@ public readonly record struct Attribute : IEqualityOperators<Attribute, Attribut
{
/// <summary>Default empty attribute.</summary>
[JsonIgnore]
public static Attribute Default => new (Color.White, ColorName.Black);
public static Attribute Default => new (Color.White, Color.Black);
/// <summary>The <see cref="ConsoleDriver"/>-specific color value.</summary>
[JsonIgnore (Condition = JsonIgnoreCondition.Always)]
@@ -65,28 +65,28 @@ public readonly record struct Attribute : IEqualityOperators<Attribute, Attribut
}
/// <summary>
/// Initializes a new instance with a <see cref="ColorName"/> value. Both <see cref="Foreground"/> and
/// Initializes a new instance with a <see cref="ColorName16"/> value. Both <see cref="Foreground"/> and
/// <see cref="Background"/> will be set to the specified color.
/// </summary>
/// <param name="colorName">Value.</param>
internal Attribute (in ColorName colorName) : this (in colorName, in colorName) { }
internal Attribute (in ColorName16 colorName) : this (in colorName, in colorName) { }
/// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
/// <param name="foregroundName">Foreground</param>
/// <param name="backgroundName">Background</param>
public Attribute (in ColorName foregroundName, in ColorName backgroundName)
public Attribute (in ColorName16 foregroundName, in ColorName16 backgroundName)
: this (new Color (in foregroundName), new Color (in backgroundName))
{ }
/// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
/// <param name="foregroundName">Foreground</param>
/// <param name="background">Background</param>
public Attribute (in ColorName foregroundName, in Color background) : this (new Color (in foregroundName), in background) { }
public Attribute (in ColorName16 foregroundName, in Color background) : this (new Color (in foregroundName), in background) { }
/// <summary>Initializes a new instance of the <see cref="Attribute"/> struct.</summary>
/// <param name="foreground">Foreground</param>
/// <param name="backgroundName">Background</param>
public Attribute (in Color foreground, in ColorName backgroundName) : this (in foreground, new Color (in backgroundName)) { }
public Attribute (in Color foreground, in ColorName16 backgroundName) : this (in foreground, new Color (in backgroundName)) { }
/// <summary>
/// Initializes a new instance of the <see cref="Attribute"/> struct with the same colors for the foreground and

View File

@@ -4,59 +4,62 @@ namespace Terminal.Gui;
internal static class ColorExtensions
{
private static FrozenDictionary<Color, ColorName> colorToNameMap;
// TODO: This should be refactored to support all W3CColors (`ColorStrings` and this should be merged).
// 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.
private static FrozenDictionary<Color, ColorName16> colorToNameMap;
static ColorExtensions ()
{
Dictionary<ColorName, AnsiColorCode> nameToCodeMap = new ()
Dictionary<ColorName16, AnsiColorCode> nameToCodeMap = new ()
{
{ ColorName.Black, AnsiColorCode.BLACK },
{ ColorName.Blue, AnsiColorCode.BLUE },
{ ColorName.Green, AnsiColorCode.GREEN },
{ ColorName.Cyan, AnsiColorCode.CYAN },
{ ColorName.Red, AnsiColorCode.RED },
{ ColorName.Magenta, AnsiColorCode.MAGENTA },
{ ColorName.Yellow, AnsiColorCode.YELLOW },
{ ColorName.Gray, AnsiColorCode.WHITE },
{ ColorName.DarkGray, AnsiColorCode.BRIGHT_BLACK },
{ ColorName.BrightBlue, AnsiColorCode.BRIGHT_BLUE },
{ ColorName.BrightGreen, AnsiColorCode.BRIGHT_GREEN },
{ ColorName.BrightCyan, AnsiColorCode.BRIGHT_CYAN },
{ ColorName.BrightRed, AnsiColorCode.BRIGHT_RED },
{ ColorName.BrightMagenta, AnsiColorCode.BRIGHT_MAGENTA },
{ ColorName.BrightYellow, AnsiColorCode.BRIGHT_YELLOW },
{ ColorName.White, AnsiColorCode.BRIGHT_WHITE }
{ ColorName16.Black, AnsiColorCode.BLACK },
{ ColorName16.Blue, AnsiColorCode.BLUE },
{ ColorName16.Green, AnsiColorCode.GREEN },
{ ColorName16.Cyan, AnsiColorCode.CYAN },
{ ColorName16.Red, AnsiColorCode.RED },
{ ColorName16.Magenta, AnsiColorCode.MAGENTA },
{ ColorName16.Yellow, AnsiColorCode.YELLOW },
{ ColorName16.Gray, AnsiColorCode.WHITE },
{ ColorName16.DarkGray, AnsiColorCode.BRIGHT_BLACK },
{ ColorName16.BrightBlue, AnsiColorCode.BRIGHT_BLUE },
{ ColorName16.BrightGreen, AnsiColorCode.BRIGHT_GREEN },
{ ColorName16.BrightCyan, AnsiColorCode.BRIGHT_CYAN },
{ ColorName16.BrightRed, AnsiColorCode.BRIGHT_RED },
{ ColorName16.BrightMagenta, AnsiColorCode.BRIGHT_MAGENTA },
{ ColorName16.BrightYellow, AnsiColorCode.BRIGHT_YELLOW },
{ ColorName16.White, AnsiColorCode.BRIGHT_WHITE }
};
ColorNameToAnsiColorMap = nameToCodeMap.ToFrozenDictionary ();
ColorName16ToAnsiColorMap = nameToCodeMap.ToFrozenDictionary ();
var colorToNameDict = new Dictionary<Color, ColorName> ();
var colorToNameDict = new Dictionary<Color, ColorName16> ();
foreach (ColorName colorName in Enum.GetValues<ColorName> ())
foreach (ColorName16 colorName in Enum.GetValues<ColorName16> ())
{
if (ColorStrings.TryParseW3CColorName (Enum.GetName<ColorName> (colorName), out Color color))
if (ColorStrings.TryParseW3CColorName (Enum.GetName<ColorName16> (colorName), out Color color))
{
colorToNameDict [color] = colorName;
}
}
ColorToNameMap = colorToNameDict.ToFrozenDictionary ();
ColorToName16Map = colorToNameDict.ToFrozenDictionary ();
}
/// <summary>Defines the 16 legacy color names and their corresponding ANSI color codes.</summary>
internal static FrozenDictionary<ColorName, AnsiColorCode> ColorNameToAnsiColorMap { get; }
internal static FrozenDictionary<ColorName16, AnsiColorCode> ColorName16ToAnsiColorMap { get; }
/// <summary>Reverse mapping for <see cref="ColorToNameMap"/>.</summary>
internal static FrozenDictionary<ColorName, Color> ColorNameToColorMap { get; private set; }
/// <summary>Reverse mapping for <see cref="ColorToName16Map"/>.</summary>
internal static FrozenDictionary<ColorName16, Color> ColorName16ToColorMap { get; private set; }
/// <summary>
/// Gets or sets a <see cref="FrozenDictionary{TKey,TValue}"/> that maps legacy 16-color values to the
/// corresponding <see cref="ColorName"/>.
/// corresponding <see cref="ColorName16"/>.
/// </summary>
/// <remarks>
/// Setter should be called as infrequently as possible, as <see cref="FrozenDictionary{TKey,TValue}"/> is
/// expensive to create.
/// </remarks>
internal static FrozenDictionary<Color, ColorName> ColorToNameMap
internal static FrozenDictionary<Color, ColorName16> ColorToName16Map
{
get => colorToNameMap;
set
@@ -64,7 +67,7 @@ internal static class ColorExtensions
colorToNameMap = value;
//Also be sure to set the reverse mapping
ColorNameToColorMap = value.ToFrozenDictionary (static kvp => kvp.Value, static kvp => kvp.Key);
ColorName16ToColorMap = value.ToFrozenDictionary (static kvp => kvp.Value, static kvp => kvp.Key);
}
}
}

View File

@@ -8,10 +8,10 @@ namespace Terminal.Gui;
/// <para>These colors match the 16 colors defined for ANSI escape sequences for 4-bit (16) colors.</para>
/// <para>
/// For terminals that support 24-bit color (TrueColor), the RGB values for each of these colors can be
/// configured using the <see cref="Color.Colors"/> property.
/// configured using the <see cref="Color.Colors16"/> property.
/// </para>
/// </remarks>
public enum ColorName
public enum ColorName16
{
/// <summary>The black color. ANSI escape sequence: <c>\u001b[30m</c>.</summary>
Black,

View File

@@ -205,7 +205,7 @@ public readonly partial record struct Color
/// <summary>Converts the provided <see langword="string"/> to a new <see cref="Color"/> value.</summary>
/// <param name="text">
/// The text to analyze. Formats supported are "#RGB", "#RRGGBB", "#ARGB", "#AARRGGBB", "rgb(r,g,b)",
/// "rgb(r,g,b,a)", "rgba(r,g,b)", "rgba(r,g,b,a)", and any of the <see cref="Gui.ColorName"/> string values.
/// "rgb(r,g,b,a)", "rgba(r,g,b)", "rgba(r,g,b,a)", and any of the <see cref="ColorName16"/> string values.
/// </param>
/// <param name="formatProvider">
/// If specified and not <see langword="null"/>, will be passed to
@@ -246,7 +246,7 @@ public readonly partial record struct Color
/// </summary>
/// <param name="text">
/// The text to analyze. Formats supported are "#RGB", "#RRGGBB", "#RGBA", "#AARRGGBB", "rgb(r,g,b)",
/// "rgb(r,g,b,a)", "rgba(r,g,b)", "rgba(r,g,b,a)", and any of the <see cref="Gui.ColorName"/> string values.
/// "rgb(r,g,b,a)", "rgba(r,g,b)", "rgba(r,g,b,a)", and any of the <see cref="ColorName16"/> string values.
/// </param>
/// <param name="formatProvider">
/// Optional <see cref="IFormatProvider"/> to provide parsing services for the input text.
@@ -351,10 +351,6 @@ public readonly partial record struct Color
{ } when char.IsLetter (text [0]) && ColorStrings.TryParseW3CColorName (text.ToString (), out Color color) =>
new Color (color),
// Attempt to parse as a named color from the ColorName enum
{ } when char.IsLetter (text [0]) && Enum.TryParse (text, true, out ColorName colorName) =>
new Color (colorName),
// Any other input
_ => throw new ColorParseException (in text, "Text did not match any expected format.", in text, [])
};
@@ -475,7 +471,7 @@ public readonly partial record struct Color
/// <summary>Converts the provided <see langword="string"/> to a new <see cref="Color"/> value.</summary>
/// <param name="text">
/// The text to analyze. Formats supported are "#RGB", "#RRGGBB", "#ARGB", "#AARRGGBB", "rgb(r,g,b)",
/// "rgb(r,g,b,a)", "rgba(r,g,b)", "rgba(r,g,b,a)", and any of the <see cref="GetClosestNamedColor (Color)"/> string
/// "rgb(r,g,b,a)", "rgba(r,g,b)", "rgba(r,g,b,a)", and any of the <see cref="GetClosestNamedColor16(Terminal.Gui.Color)"/> string
/// values.
/// </param>
/// <param name="formatProvider">
@@ -505,7 +501,7 @@ public readonly partial record struct Color
/// </summary>
/// <param name="text">
/// The text to analyze. Formats supported are "#RGB", "#RRGGBB", "#ARGB", "#AARRGGBB", "rgb(r,g,b)",
/// "rgb(r,g,b,a)", "rgba(r,g,b)", "rgba(r,g,b,a)", and any of the <see cref="GetClosestNamedColor (Color)"/> string
/// "rgb(r,g,b,a)", "rgba(r,g,b)", "rgba(r,g,b,a)", and any of the <see cref="GetClosestNamedColor16(Terminal.Gui.Color)"/> string
/// values.
/// </param>
/// <param name="formatProvider">
@@ -602,7 +598,7 @@ public readonly partial record struct Color
/// <summary>Converts the provided string to a new <see cref="Color"/> instance.</summary>
/// <param name="text">
/// The text to analyze. Formats supported are "#RGB", "#RRGGBB", "#ARGB", "#AARRGGBB", "rgb(r,g,b)",
/// "rgb(r,g,b,a)", "rgba(r,g,b)", "rgba(r,g,b,a)", and any of the <see cref="Gui.ColorName"/> string values.
/// "rgb(r,g,b,a)", "rgba(r,g,b)", "rgba(r,g,b,a)", and any of the <see cref="ColorName16"/> string values.
/// </param>
/// <param name="color">The parsed value.</param>
/// <returns>A boolean value indicating whether parsing was successful.</returns>

View File

@@ -53,11 +53,11 @@ public readonly partial record struct Color
public static implicit operator Color (uint u) { return new Color (u); }
/// <summary>
/// Implicit conversion from <see cref="GetClosestNamedColor (Color)"/> to <see cref="Color"/> via lookup from
/// <see cref="ColorExtensions.ColorNameToColorMap"/>.
/// Implicit conversion from <see cref="GetClosestNamedColor16(Terminal.Gui.Color)"/> to <see cref="Color"/> via lookup from
/// <see cref="ColorExtensions.ColorName16ToColorMap"/>.
/// </summary>
[Pure]
public static implicit operator Color (ColorName colorName) { return ColorExtensions.ColorNameToColorMap [colorName]; }
public static implicit operator Color (ColorName16 colorName) { return ColorExtensions.ColorName16ToColorMap [colorName]; }
/// <summary>
/// Implicit conversion from <see cref="Vector4"/> to <see cref="Color"/>, where (<see cref="Vector4.X"/>,

View File

@@ -17,7 +17,7 @@ namespace Terminal.Gui;
/// </summary>
/// <seealso cref="Attribute"/>
/// <seealso cref="ColorExtensions"/>
/// <seealso cref="ColorName"/>
/// <seealso cref="ColorName16"/>
[JsonConverter (typeof (ColorJsonConverter))]
[StructLayout (LayoutKind.Explicit)]
public readonly partial record struct Color : ISpanParsable<Color>, IUtf8SpanParsable<Color>, ISpanFormattable,
@@ -109,9 +109,9 @@ public readonly partial record struct Color : ISpanParsable<Color>, IUtf8SpanPar
/// <summary>Initializes a new instance of the <see cref="Color"/> color from a legacy 16-color named value.</summary>
/// <param name="colorName">The 16-color value.</param>
public Color (in ColorName colorName)
public Color (in ColorName16 colorName)
{
string? name = Enum.GetName<ColorName> (colorName);
string? name = Enum.GetName<ColorName16> (colorName);
if (name is null)
{
@@ -144,26 +144,28 @@ public readonly partial record struct Color : ISpanParsable<Color>, IUtf8SpanPar
/// <summary>Initializes a new instance of the <see cref="Color"/> with all channels set to 0.</summary>
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.
/// <summary>Gets or sets the 3-byte/6-character hexadecimal value for each of the legacy 16-color values.</summary>
[SerializableConfigurationProperty (Scope = typeof (SettingsScope), OmitClassName = true)]
public static Dictionary<ColorName, string> Colors
public static Dictionary<ColorName16, string> Colors16
{
get =>
// Transform _colorToNameMap into a Dictionary<ColorNames,string>
ColorExtensions.ColorToNameMap.ToDictionary (static kvp => kvp.Value, static kvp => kvp.Key.ToString ("g"));
ColorExtensions.ColorToName16Map.ToDictionary (static kvp => kvp.Value, static kvp => kvp.Key.ToString ("g"));
set
{
// Transform Dictionary<ColorNames,string> into _colorToNameMap
ColorExtensions.ColorToNameMap = value.ToFrozenDictionary (GetColorToNameMapKey, GetColorToNameMapValue);
ColorExtensions.ColorToName16Map = value.ToFrozenDictionary (GetColorToNameMapKey, GetColorToNameMapValue);
return;
static Color GetColorToNameMapKey (KeyValuePair<ColorName, string> kvp) { return new Color (kvp.Value); }
static Color GetColorToNameMapKey (KeyValuePair<ColorName16, string> kvp) { return new Color (kvp.Value); }
static ColorName GetColorToNameMapValue (KeyValuePair<ColorName, string> kvp)
static ColorName16 GetColorToNameMapValue (KeyValuePair<ColorName16, string> kvp)
{
return Enum.TryParse (kvp.Key.ToString (), true, out ColorName colorName)
return Enum.TryParse (kvp.Key.ToString (), true, out ColorName16 colorName)
? colorName
: throw new ArgumentException ($"Invalid color name: {kvp.Key}");
}
@@ -171,31 +173,31 @@ public readonly partial record struct Color : ISpanParsable<Color>, IUtf8SpanPar
}
/// <summary>
/// Gets the <see cref="Color"/> using a legacy 16-color <see cref="ColorName"/> value. <see langword="get"/> will
/// Gets the <see cref="Color"/> using a legacy 16-color <see cref="ColorName16"/> value. <see langword="get"/> will
/// return the closest 16 color match to the true color when no exact value is found.
/// </summary>
/// <remarks>
/// Get returns the <see cref="GetClosestNamedColor (Color)"/> of the closest 24-bit color value. Set sets the RGB
/// Get returns the <see cref="GetClosestNamedColor16(Color)"/> of the closest 24-bit color value. Set sets the RGB
/// value using a hard-coded map.
/// </remarks>
public AnsiColorCode GetAnsiColorCode () { return ColorExtensions.ColorNameToAnsiColorMap [GetClosestNamedColor ()]; }
public AnsiColorCode GetAnsiColorCode () { return ColorExtensions.ColorName16ToAnsiColorMap [GetClosestNamedColor16 ()]; }
/// <summary>
/// Gets the <see cref="Color"/> using a legacy 16-color <see cref="Gui.ColorName"/> value. <see langword="get"/>
/// Gets the <see cref="Color"/> using a legacy 16-color <see cref="ColorName16"/> value. <see langword="get"/>
/// will return the closest 16 color match to the true color when no exact value is found.
/// </summary>
/// <remarks>
/// Get returns the <see cref="GetClosestNamedColor (Color)"/> of the closest 24-bit color value. Set sets the RGB
/// Get returns the <see cref="GetClosestNamedColor16(Terminal.Gui.Color)"/> of the closest 24-bit color value. Set sets the RGB
/// value using a hard-coded map.
/// </remarks>
public ColorName GetClosestNamedColor () { return GetClosestNamedColor (this); }
public ColorName16 GetClosestNamedColor16 () { return GetClosestNamedColor16 (this); }
/// <summary>
/// Determines if the closest named <see cref="Color"/> to <see langword="this"/> is the provided
/// <paramref name="namedColor"/>.
/// </summary>
/// <param name="namedColor">
/// The <see cref="GetClosestNamedColor (Color)"/> to check if this <see cref="Color"/> is closer
/// The <see cref="GetClosestNamedColor16(Terminal.Gui.Color)"/> to check if this <see cref="Color"/> is closer
/// to than any other configured named color.
/// </param>
/// <returns>
@@ -208,18 +210,18 @@ public readonly partial record struct Color : ISpanParsable<Color>, IUtf8SpanPar
/// </remarks>
[Pure]
[MethodImpl (MethodImplOptions.AggressiveInlining)]
public bool IsClosestToNamedColor (in ColorName namedColor) { return GetClosestNamedColor () == namedColor; }
public bool IsClosestToNamedColor16 (in ColorName16 namedColor) { return GetClosestNamedColor16 () == namedColor; }
/// <summary>
/// Determines if the closest named <see cref="Color"/> to <paramref name="color"/>/> is the provided
/// <paramref name="namedColor"/>.
/// </summary>
/// <param name="color">
/// The color to test against the <see cref="GetClosestNamedColor (Color)"/> value in
/// The color to test against the <see cref="GetClosestNamedColor16(Terminal.Gui.Color)"/> value in
/// <paramref name="namedColor"/>.
/// </param>
/// <param name="namedColor">
/// The <see cref="GetClosestNamedColor (Color)"/> to check if this <see cref="Color"/> is closer
/// The <see cref="GetClosestNamedColor16(Terminal.Gui.Color)"/> to check if this <see cref="Color"/> is closer
/// to than any other configured named color.
/// </param>
/// <returns>
@@ -233,7 +235,7 @@ public readonly partial record struct Color : ISpanParsable<Color>, IUtf8SpanPar
/// </remarks>
[Pure]
[MethodImpl (MethodImplOptions.AggressiveInlining)]
public static bool IsColorClosestToNamedColor (in Color color, in ColorName namedColor) { return color.IsClosestToNamedColor (in namedColor); }
public static bool IsColorClosestToNamedColor16 (in Color color, in ColorName16 namedColor) { return color.IsClosestToNamedColor16 (in namedColor); }
/// <summary>Gets the "closest" named color to this <see cref="Color"/> value.</summary>
/// <param name="inputColor"></param>
@@ -244,9 +246,9 @@ public readonly partial record struct Color : ISpanParsable<Color>, IUtf8SpanPar
/// </remarks>
/// <returns></returns>
[SkipLocalsInit]
internal static ColorName GetClosestNamedColor (Color inputColor)
internal static ColorName16 GetClosestNamedColor16 (Color inputColor)
{
return ColorExtensions.ColorToNameMap.MinBy (pair => CalculateColorDistance (inputColor, pair.Key)).Value;
return ColorExtensions.ColorToName16Map.MinBy (pair => CalculateColorDistance (inputColor, pair.Key)).Value;
}
[SkipLocalsInit]
@@ -297,52 +299,52 @@ public readonly partial record struct Color : ISpanParsable<Color>, IUtf8SpanPar
#region Legacy Color Names
/// <summary>The black color.</summary>
public const ColorName Black = ColorName.Black;
public const ColorName16 Black = ColorName16.Black;
/// <summary>The blue color.</summary>
public const ColorName Blue = ColorName.Blue;
public const ColorName16 Blue = ColorName16.Blue;
/// <summary>The green color.</summary>
public const ColorName Green = ColorName.Green;
public const ColorName16 Green = ColorName16.Green;
/// <summary>The cyan color.</summary>
public const ColorName Cyan = ColorName.Cyan;
public const ColorName16 Cyan = ColorName16.Cyan;
/// <summary>The red color.</summary>
public const ColorName Red = ColorName.Red;
public const ColorName16 Red = ColorName16.Red;
/// <summary>The magenta color.</summary>
public const ColorName Magenta = ColorName.Magenta;
public const ColorName16 Magenta = ColorName16.Magenta;
/// <summary>The yellow color.</summary>
public const ColorName Yellow = ColorName.Yellow;
public const ColorName16 Yellow = ColorName16.Yellow;
/// <summary>The gray color.</summary>
public const ColorName Gray = ColorName.Gray;
public const ColorName16 Gray = ColorName16.Gray;
/// <summary>The dark gray color.</summary>
public const ColorName DarkGray = ColorName.DarkGray;
public const ColorName16 DarkGray = ColorName16.DarkGray;
/// <summary>The bright bBlue color.</summary>
public const ColorName BrightBlue = ColorName.BrightBlue;
public const ColorName16 BrightBlue = ColorName16.BrightBlue;
/// <summary>The bright green color.</summary>
public const ColorName BrightGreen = ColorName.BrightGreen;
public const ColorName16 BrightGreen = ColorName16.BrightGreen;
/// <summary>The bright cyan color.</summary>
public const ColorName BrightCyan = ColorName.BrightCyan;
public const ColorName16 BrightCyan = ColorName16.BrightCyan;
/// <summary>The bright red color.</summary>
public const ColorName BrightRed = ColorName.BrightRed;
public const ColorName16 BrightRed = ColorName16.BrightRed;
/// <summary>The bright magenta color.</summary>
public const ColorName BrightMagenta = ColorName.BrightMagenta;
public const ColorName16 BrightMagenta = ColorName16.BrightMagenta;
/// <summary>The bright yellow color.</summary>
public const ColorName BrightYellow = ColorName.BrightYellow;
public const ColorName16 BrightYellow = ColorName16.BrightYellow;
/// <summary>The White color.</summary>
public const ColorName White = ColorName.White;
public const ColorName16 White = ColorName16.White;
#endregion
}

View File

@@ -61,7 +61,7 @@ public class ColorPicker16 : View
set
{
int colorIndex = value.Y * _cols + value.X;
SelectedColor = (ColorName)colorIndex;
SelectedColor = (ColorName16)colorIndex;
}
}
@@ -132,7 +132,7 @@ public class ColorPicker16 : View
continue;
}
Driver.SetAttribute (new ((ColorName)foregroundColorIndex, (ColorName)colorIndex));
Driver.SetAttribute (new ((ColorName16)foregroundColorIndex, (ColorName16)colorIndex));
bool selected = x == Cursor.X && y == Cursor.Y;
DrawColorBox (x, y, selected);
colorIndex++;
@@ -141,12 +141,12 @@ public class ColorPicker16 : View
}
/// <summary>Selected color.</summary>
public ColorName SelectedColor
public ColorName16 SelectedColor
{
get => (ColorName)_selectColorIndex;
get => (ColorName16)_selectColorIndex;
set
{
if (value == (ColorName)_selectColorIndex)
if (value == (ColorName16)_selectColorIndex)
{
return;
}