【问题标题】:C# Convert System.ConsoleColor to System.Drawing.ColorC# 将 System.ConsoleColor 转换为 System.Drawing.Color
【发布时间】:2016-02-19 20:46:57
【问题描述】:

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

【问题讨论】:

标签: c# colors


【解决方案1】:

我从这个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]);
}

Patrick Hofmann 的回答也有效,但会产生无效的颜色!

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

【讨论】:

    【解决方案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 语句。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 名称相同但值不同,如此处所述stackoverflow.com/a/28211539/5201815
      • 好的,但是你要逻辑转换还是技术转换?
      • 正确的转换。重复标记的答案不包含带有代码的正确答案
      • @PatrickHofman:- 我认为您可以保留它,因为它正在回答预期的问题! +1
      猜你喜欢
      • 1970-01-01
      • 2011-06-04
      • 1970-01-01
      • 1970-01-01
      • 2011-05-05
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 2011-06-07
      相关资源
      最近更新 更多