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

问题描述

什么是转换的最佳途径的System.Drawing.Color 来一个类似 System.ConsoleColor

解决方案

不幸的是,即使在Windows控制台可以支持RGB颜色,控制台类只暴露ConsoleColor枚举这大大限制了可能的颜色就可以使用。如果你想有一个Color结构映射到了最接近ConsoleColor,这将是棘手的。

但如果你想指定的颜色,以配合相应的ConsoleColor你可以做一个地图,如:

  VAR地图=新的解释和LT;颜色,ConsoleColor>();
图[Color.Red] = ConsoleColor.Red;
图[Color.Blue] = ConsoleColor.Blue;
等等...

或者,如果表现并不重要,你可以通过字符串往返。 (仅适用于的命名的颜色的)

  VAR颜色= Enum.Parse(typeof运算(ConsoleColor),color.Name);

编辑:这里有一个链接question关于发现颜色亲近。

What is the best way to convert a System.Drawing.Color to a similar System.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.

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...

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

EDIT: Here's a link to a question about finding color "closeness".

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

09-09 17:54