[英]C# Convert System.ConsoleColor to System.Drawing.Color
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)
{
// ??
}
i made a answer out of this answer , because values between the names of Color and ConsoleColor are different我从这个答案中做出了一个答案,因为 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'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());
}
}
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.注意:由于
ConsoleColor
是一个枚举,它允许您表示更低范围的颜色。 It means that when you convert Color
to ConsoleColor
, you cut off some information;也就是说,当你将
Color
转换为ConsoleColor
,你截断了一些信息; and when you convert ConsoleColor
to Color
, you will need to restore it.当您将
ConsoleColor
转换为Color
,您将需要恢复它。 In my implementation this function will set a color component value equal to 64 for dark colors, and 128 for bright colors.在我的实现中,此函数将设置一个颜色分量值,对于暗色等于 64,对于亮色设置等于 128。 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.最好的解决方案实际上是用硬编码颜色做一个大的
switch
语句。
Converted to C# from this post here , which answers your question, but is in VB.NET (just posting for completeness):从这里的这篇文章转换为 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.