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 StandardColorsNameResolver _standard = new(); /// /// Gets the color name for . /// /// The color. /// Standard color name for the specified color; otherwise . public static string? GetColorName (Color color) { if (_standard.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 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 (_standard.TryParseColor (name, out color)) { return true; } // Backwards compatibility: Also parse #RRGGBB. if (TryParseHexColor (name, out color)) { return true; } color = default (Color); 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 (Color); return false; } }