C#将System.ConsoleColor转换为System.Drawing.Color

时间:2015-11-18 08:03:36

标签: c# colors

来自这个answer我想知道是否有一种简单的方法来扭转这种方法

public static System.ConsoleColor FromColor(System.Drawing.Color c)
{
    int index = (c.R > 128 | c.G > 128 | c.B > 128) ? 8 : 0; // Bright bit
    index |= (c.R > 64) ? 4 : 0; // Red bit
    index |= (c.G > 64) ? 2 : 0; // Green bit
    index |= (c.B > 64) ? 1 : 0; // Blue bit
    return (System.ConsoleColor)index;
}

public static System.Drawing.Color FromColor( System.ConsoleColor c)
{
    // ??
}

3 个答案:

答案 0 :(得分:3)

我从这个answer做出了答案,因为Color和ConsoleColor名称之间的值不同

public static System.Drawing.Color FromColor(System.ConsoleColor c)
{
    int[] cColors = {   0x000000, //Black = 0
                        0x000080, //DarkBlue = 1
                        0x008000, //DarkGreen = 2
                        0x008080, //DarkCyan = 3
                        0x800000, //DarkRed = 4
                        0x800080, //DarkMagenta = 5
                        0x808000, //DarkYellow = 6
                        0xC0C0C0, //Gray = 7
                        0x808080, //DarkGray = 8
                        0x0000FF, //Blue = 9
                        0x00FF00, //Green = 10
                        0x00FFFF, //Cyan = 11
                        0xFF0000, //Red = 12
                        0xFF00FF, //Magenta = 13
                        0xFFFF00, //Yellow = 14
                        0xFFFFFF  //White = 15
                    };
    return Color.FromArgb(cColors[(int)c]);
}
帕特里克霍夫曼的回答也起作用,但却产生了无效的颜色!

public static System.Drawing.Color FromColor(System.ConsoleColor c)
{
    switch (c)
    {
        case ConsoleColor.DarkYellow:
            return Color.FromArgb(255, 128, 128, 0);
        default:
            return Color.FromName(c.ToString());
    }
}

答案 1 :(得分:2)

this post here转换为C#,它回答了您的问题,但是在VB.NET中(仅发布完整性):

static class ColorExtension
{
    public static Color DrawingColor(ConsoleColor color)
    {
        switch (color) {
            case ConsoleColor.Black:

                return color.Black;
            case ConsoleColor.Blue:

                return color.Blue;
            case ConsoleColor.Cyan:

                return color.Cyan;
            case ConsoleColor.DarkBlue:

                return color.DarkBlue;
            case ConsoleColor.DarkGray:

                return color.DarkGray;
            case ConsoleColor.DarkGreen:

                return color.DarkGreen;
            case ConsoleColor.DarkMagenta:

                return color.DarkMagenta;
            case ConsoleColor.DarkRed:

                return color.DarkRed;
            case ConsoleColor.DarkYellow:

                return color.FromArgb(255, 128, 128, 0);
            case ConsoleColor.Gray:

                return color.Gray;
            case ConsoleColor.Green:

                return color.Green;
            case ConsoleColor.Magenta:

                return color.Magenta;
            case ConsoleColor.Red:

                return color.Red;
            case ConsoleColor.White:

                return color.White;
            default:
                return color.Yellow;
        }
    }
}

答案 2 :(得分:2)

如果您只需要执行反向功能,它将是:

public static System.Drawing.Color FromColor(System.ConsoleColor c)
{
    int cInt = (int) c;

    int brightnessCoefficient = ((cInt & 8) > 0) ? 2 : 1;
    int r = ((cInt & 4) > 0) ? 64 * brightnessCoefficient : 0;
    int g = ((cInt & 2) > 0) ? 64 * brightnessCoefficient : 0;
    int b = ((cInt & 1) > 0) ? 64 * brightnessCoefficient : 0;

    return Color.FromArgb(r, g, b);
}

注意:由于ConsoleColor是一个枚举,它允许您表示更低范围的颜色。这意味着当您将Color转换为ConsoleColor时,会切断一些信息;当您将ConsoleColor转换为Color时,您需要将其恢复。在我的实现中,此函数将为深色设置颜色分量值等于64,为明亮颜色设置128。但是,它实际上可能是另一种,并且可能因不同颜色而有所不同。

最佳解决方案实际上是使用硬编码颜色执行大switch语句。