繁体   English   中英

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

[英]C# Convert System.ConsoleColor to System.Drawing.Color

来自这个答案,我想知道是否有一种简单的方法可以逆转这种方法

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)
{
    // ??
}

我从这个答案中做出了一个答案,因为 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());
    }
}

如果你只需要做一个反向功能,它将是:

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语句。

这里的这篇文章转换为 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;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM