[英]System.Windows.Media.Color to color name
I have the following: 我有以下几点:
Color color = Colors.Red;
color.ToString();
which outputs as the hexadecimal representation. 输出为十六进制表示形式。 Is there any way to output "Red"?
有什么办法输出“红色”?
Bonus points to whoever gives a solutions that works with different cultures (ie output "Rojo" for spanish). 奖励指向任何提供适用于不同文化的解决方案的人(例如,西班牙语输出“ Rojo”)。
It looks like you might have to hand-roll your own solution using Reflection. 看来您可能必须使用Reflection手动推出自己的解决方案。 Here's my first shot:
这是我的第一枪:
public static string GetColorName(this System.Windows.Media.Color color)
{
Type colors = typeof(System.Windows.Media.Colors);
foreach(var prop in colors.GetProperties())
{
if(((System.Windows.Media.Color)prop.GetValue(null, null)) == color)
return prop.Name;
}
throw new Exception("The provided Color is not named.");
}
Keep in mind that this is by no means efficient, but from what I can see in the documentation it would be the only way. 请记住,这绝不是有效的,但是从我在文档中看到的那是唯一的方法。
One option might be to convert the Media.Color to a Drawing.Color 一种选择是将Media.Color转换为Drawing.Color
private System.Drawing.Color ColorFromMediaColor(System.Windows.Media.Color clr)
{
return System.Drawing.Color.FromArgb(clr.A, clr.R, clr.G, clr.B);
}
Then use the Name property from the Drawing.Color object to get the color name. 然后使用Drawing.Color对象的Name属性获取颜色名称。
As for localizing, you could look up the color name in a translation dictionary built from resource files you provide. 至于本地化,您可以在根据您提供的资源文件构建的翻译词典中查找颜色名称 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.