What is the best way to convert a System.Drawing.Color
to a similar System.ConsoleColor
?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
A simple and effective approach can be implemented by using the
GetHue
,GetBrightness
andGetSaturation
methods of theColor
class.Note that the color names of the console do not match the well known colors. So if you test a color mapping scheme, you have to keep in mind that (for instance) in the well known colors, Gray is dark gray, Light Gray is gray, Green is dark green, Lime is pure green, and Olive is dark yellow.
Here are the console color hex values, as converted by .NET 4.5. First the program:
And here's the output. As you can see, there's a problem with the reporting for
DarkYellow
. The full 32-bits of that one show up as zero. All the others have 0xFF for the alpha channel.edit: I got a little more carried away just now, so here's a converter from
RGB
to the nearestConsoleColor
value. Note that the dependency onSystem.Windows.Media
is only for the demonstration harness; the actual function itself only referencesSystem.Drawing
.The output (partial)...