簡體   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