using System.Globalization;
namespace Terminal.Gui.Drawing;
///
/// Provides a mapping between and the W3C standard color name strings.
///
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 .
///
/// The color.
/// Standard color name for the specified color; otherwise .
public static string? GetColorName (Color color)
{
if (_multi.TryNameColor (color, out string? name))
{
return name;
}
return null;
}
///
/// Returns the list of W3C+ standard color names.
///
///
public static IEnumerable GetStandardColorNames ()
{
return _standard.GetColorNames ();
}
///
/// 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 TryParseStandardColorName (ReadOnlySpan name, out Color color)
{
if (_standard.TryParseColor (name, out color))
{
return true;
}
// Backwards compatibility: Also parse #RRGGBB.
return TryParseHexColor (name, out color);
}
///
/// 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.
///
/// 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))
{
return true;
}
// Backwards compatibility: Also parse #RRGGBB.
if (TryParseHexColor (name, out color))
{
return true;
}
color = default;
return false;
}
private static bool TryParseHexColor (ReadOnlySpan name, out Color color)
{
if (name.Length == 7 && name [0] == '#')
{
if (int.TryParse (name.Slice (1, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int r) &&
int.TryParse (name.Slice (3, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int g) &&
int.TryParse (name.Slice (5, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int b))
{
color = new Color (r, g, b);
return true;
}
}
color = default;
return false;
}
}