2

Coming from this answer i would like to know if there is a easy way to reverse this method

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;
}

into

public static System.Drawing.Color FromColor( System.ConsoleColor c)
{
    // ??
}
Community
  • 1
  • 1
Byyo
  • 2,163
  • 4
  • 21
  • 35
  • 3
    Did you checked this: http://stackoverflow.com/questions/21092421/convert-consolecolor-to-color Although its in VB.NET but should give you a clue! – Rahul Tripathi Nov 18 '15 at 08:10

3 Answers3

6

i made a answer out of this answer, because values between the names of Color and ConsoleColor are different

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]);
}

Patrick Hofmann's answer works aswell but produces invalid colors!

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());
    }
}
Community
  • 1
  • 1
Byyo
  • 2,163
  • 4
  • 21
  • 35
3

If you only need to do a reverse function, it will be:

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);
}

Note: since ConsoleColor is an enum, it allows you to represent much lower range of colors. It means that when you convert Color to ConsoleColor, you cut off some information; and when you convert ConsoleColor to Color, you will need to restore it. In my implementation this function will set a color component value equal to 64 for dark colors, and 128 for bright colors. However, it may actually be another and may vary for different colors.

The best solution is actually to do a big switch statement with hardcoded colors.

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
2

Converted to C# from this post here, which answers your question, but is in VB.NET (just posting for completeness):

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;
        }
    }
}
infografnet
  • 3,749
  • 1
  • 35
  • 35
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325