コード例 #1
0
ファイル: Color.cs プロジェクト: gitfool/spectre.console
    /// <summary>
    /// Converts a <see cref="Color"/> to a <see cref="ConsoleColor"/>.
    /// </summary>
    /// <param name="color">The color to convert.</param>
    /// <returns>A <see cref="ConsoleColor"/> representing the <see cref="Color"/>.</returns>
    public static ConsoleColor ToConsoleColor(Color color)
    {
        if (color.IsDefault)
        {
            return((ConsoleColor)(-1));
        }

        if (color.Number == null || color.Number.Value >= 16)
        {
            color = ColorPalette.ExactOrClosest(ColorSystem.Standard, color);
        }

        // Should not happen, but this will make things easier if we mess things up...
        Debug.Assert(
            color.Number >= 0 && color.Number < 16,
            "Color does not fall inside the standard palette range.");

        return(color.Number.Value switch
        {
            0 => ConsoleColor.Black,
            1 => ConsoleColor.DarkRed,
            2 => ConsoleColor.DarkGreen,
            3 => ConsoleColor.DarkYellow,
            4 => ConsoleColor.DarkBlue,
            5 => ConsoleColor.DarkMagenta,
            6 => ConsoleColor.DarkCyan,
            7 => ConsoleColor.Gray,
            8 => ConsoleColor.DarkGray,
            9 => ConsoleColor.Red,
            10 => ConsoleColor.Green,
            11 => ConsoleColor.Yellow,
            12 => ConsoleColor.Blue,
            13 => ConsoleColor.Magenta,
            14 => ConsoleColor.Cyan,
            15 => ConsoleColor.White,
            _ => throw new InvalidOperationException("Cannot convert color to console color."),
        });