简体   繁体   English

System.Windows.Media.Color为颜色名称

[英]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.

相关问题 在IronPython中创建System.Windows.Media.Color - Create System.Windows.Media.Color in IronPython 将 System.Windows.Media.Color 转换为 System.Drawing.Color - Converting System.Windows.Media.Color to System.Drawing.Color 绑定System.Windows.Media.Color到TextBlock.Foreground? - Binding System.Windows.Media.Color to TextBlock.Foreground? 如何使用StringFormat在XAML中格式化System.Windows.Media.Color - How to format a System.Windows.Media.Color in XAML using StringFormat 如何从BitmapImage获取数组System.Windows.Media.Color? - How to get an array System.Windows.Media.Color from a BitmapImage? 如何比较System.Drawing.Color和System.Windows.Media.Color? - How to compare a System.Drawing.Color and a System.Windows.Media.Color? 如何从 System.Drawing.Color 转换为 System.Windows.Media.Color? - How to convert from System.Drawing.Color to System.Windows.Media.Color? 如何处理System.Drawing.Color &lt; - &gt; System.Windows.Media.Color模糊引用 - How to deal with System.Drawing.Color <--> System.Windows.Media.Color ambiguous reference 如何从HEX Windows 8应用程序获取System.Windows.Media.Color - How to get System.Windows.Media.Color From From HEX Windows 8 Application 如何使用自定义值创建 System.Windows.Media.Color 类型的属性 - How to create a property of type System.Windows.Media.Color with a custom value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM