将颜色转换为 ConsoleColor? [英] Converting Color to ConsoleColor?

查看:21
本文介绍了将颜色转换为 ConsoleColor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.Drawing.Color 转换为类似的 System.ConsoleColor 的最佳方法是什么?

What is the best way to convert a System.Drawing.Color to a similar System.ConsoleColor?

推荐答案

不幸的是,尽管 Windows 控制台可以支持 RGB 颜色,但 Console 类只公开了 ConsoleColor 枚举,这极大地限制了您可以使用的可能颜色.如果您希望将 Color 结构映射到最接近"的 ConsoleColor,那将很棘手.

Unfortunately, even though the Windows console can support RGB colors, the Console class only exposes the ConsoleColor enumeration which greatly limits the possible colors you can use. If you want a Color structure to be mapped to the "closest" ConsoleColor, that will be tricky.

但是如果您希望命名的 Color 与相应的 ConsoleColor 匹配,您可以制作一个地图,例如:

But if you want the named Color to match a corresponding ConsoleColor you can make a map such as:

var map = new Dictionary<Color, ConsoleColor>();
map[Color.Red] = ConsoleColor.Red;
map[Color.Blue] = ConsoleColor.Blue;
etc...

或者如果性能不是那么重要,你可以通过 String 来回.(仅适用于命名颜色)

Or if performance is not that important, you can round trip through String. (Only works for named colors)

var color = Enum.Parse(typeof(ConsoleColor), color.Name);

这是一个关于寻找颜色接近度"的问题.

这篇关于将颜色转换为 ConsoleColor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆