mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-19 21:38:16 +08:00
Added hex color conversion (#1432)
* Added hex color conversion --------- Co-authored-by: Frank Ray <52075808+FrankRay78@users.noreply.github.com> Co-authored-by: Nils Andresen <nils@nils-andresen.de>
This commit is contained in:
@ -213,6 +213,50 @@ public partial struct Color : IEquatable<Color>
|
||||
return ColorTable.GetColor(number);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a color from a hexadecimal string representation.
|
||||
/// </summary>
|
||||
/// <param name="hex">The hexadecimal string representation of the color.</param>
|
||||
/// <returns>The color created from the hexadecimal string.</returns>
|
||||
public static Color FromHex(string hex)
|
||||
{
|
||||
if (hex is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(hex));
|
||||
}
|
||||
|
||||
if (hex.StartsWith("#"))
|
||||
{
|
||||
hex = hex.Substring(1);
|
||||
}
|
||||
|
||||
var r = byte.Parse(hex.Substring(0, 2), NumberStyles.HexNumber);
|
||||
var g = byte.Parse(hex.Substring(2, 2), NumberStyles.HexNumber);
|
||||
var b = byte.Parse(hex.Substring(4, 2), NumberStyles.HexNumber);
|
||||
|
||||
return new Color(r, g, b);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to convert a hexadecimal color code to a <see cref="Color"/> object.
|
||||
/// </summary>
|
||||
/// <param name="hex">The hexadecimal color code.</param>
|
||||
/// <param name="color">When this method returns, contains the <see cref="Color"/> equivalent of the hexadecimal color code, if the conversion succeeded, or <see cref="Color.Default"/> if the conversion failed.</param>
|
||||
/// <returns><c>true</c> if the conversion succeeded; otherwise, <c>false</c>.</returns>
|
||||
public static bool TryFromHex(string hex, out Color color)
|
||||
{
|
||||
try
|
||||
{
|
||||
color = FromHex(hex);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
color = Color.Default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="ConsoleColor"/> to a <see cref="Color"/>.
|
||||
/// </summary>
|
||||
|
Reference in New Issue
Block a user