一个XNA颜色对象转换为字符串转换为、字符串、对象、颜色

2023-09-04 02:46:31 作者:愿否

我知道如何一个字符串转换为XNA 颜色对象,但我怎么转换成一个C#颜色状物体 Col​​or.Blue 成其字符串重新presentation(如蓝)。

I know how to convert a string to an XNA Color object, but how do I convert a C# Color object like Color.Blue into its string representation(e.g. "Blue").

推荐答案

您需要做什么在你的previous问题做了相反的:

You need to do the reverse of what was done in your previous question:

从XNA颜色转换成系统颜色 尝试和系统的颜色转换为已知的颜色 如果转换工作,调用toString上已知的颜色

例如。

// Borrowed from previous question
using XnaColor = Microsoft.Xna.Framework.Graphics.Color;

System.Drawing.Color clrColor = System.Drawing.Color.FromName("Red"); 
XnaColor xnaColor = new XnaColor(clrColor.R, clrColor.G, clrColor.B, clrColor.A);

// Working back the other way
System.Drawing.Color newClrColor = System.Drawing.Color.FromArgb(xnaColor.A, xnaColor.R, xnaColor.G, xnaColor.B);
System.Drawing.KnownColor kColor = newClrColor.ToKnownColor();
string colorName = kColor != 0 ? kColor.ToString() : "";

请注意:这会给你一个空字符串,如果颜色名称是不知道

Note: This will give you an empty string if the color name isn't known.

你可能会想尝试在这里使用的TypeConverter。我不知道,一个存在的XNA颜色类型,但它是值得一试:

You might want to try using a TypeConverter here. I'm not sure that one exists for the XNA Color type, but it's worth a shot:

string colorName = System.ComponentModel.TypeDescriptor.GetConverter(typeof(Microsoft.Xna.Framework.Graphics.Color)).ConvertToString(yourXnaColor);

由于没有以上会做你想要什么,你必须尝试类似的做法,以什么乔恩曾在这里完成的:http://stackoverflow.com/questions/3391462/convert-string-to-color-in-c/3391583#3391583

Since none of the above is going to do what you want, you'll have to try a similar approach to what Jon has done here: http://stackoverflow.com/questions/3391462/convert-string-to-color-in-c/3391583#3391583

您将需要拉所有的XNA色入字典使用反射,就像他做的,但反向键和值,所以它的字典里,然后写访问词典以颜色参数和返回功能名字。

You'll need to pull all the XNA colors into a dictionary using reflection, like he has done, but reverse the keys and values, so it's Dictionary, then write a function that accesses the dictionary taking a Color parameter and returning the name.