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").
3 Answers
var color = System.Drawing.Color.Blue;
var known = color.ToKnownColor();
string name = known != null ? known.ToString() : "";

- 22,355
- 2
- 39
- 64
-
@Bennor: I haven't done significant XNA work, but aren't System.Drawing.Color and System.Drawing.KnownColor available? Your own answer assumes that it is. – John Fisher Aug 02 '10 at 22:09
-
Yeah, there's access to them for sure, but you still need to convert from the XNA color. – Bennor McCarthy Aug 02 '10 at 22:13
-
On the XBox360 you can't use System.Drawing, but based on the first answer he accepted, I am guessing this is a Windows app. – Struan Aug 02 '10 at 22:18
-
The answer to both questions would be quite different using XNA only. You'd have to use the TypeConverter approach (if available), or Reflection (there's a good example in the linked question). – Bennor McCarthy Aug 02 '10 at 22:22
-
Hi John,thanks for your input.Following your method,I always got a "0" as the resulting string,could you suggest a reason,plz?thanks. – Kevin Aug 02 '10 at 23:01
-
Are you sure that the color you're trying to convert is an exact match for the RGBA values of the KnownColor that you expect? – John Fisher Aug 04 '10 at 20:12
You need to do the reverse of what was done in your previous question:
- Convert from XNA color to System color
- Try and convert the system color to a known color
- If the conversion worked, call ToString on the known color
e.g.
// 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.
[EDIT] 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);
[EDIT]
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: Convert string to Color in C#
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.

- 1
- 1

- 11,415
- 1
- 49
- 51
-
Thanks again Bennor! I like your first part code,however,it will always return a 0 as the resulting string,even if I make the clrColor as a constant,say,like Color.Green,the colorName will still be a "0".Have no idea where went wrong.. – Kevin Aug 02 '10 at 22:44
-
1The good news is you haven't done anything wrong. The bad news is that the solution I've given won't work (and neither will the others posted so far). It looks like it's only possible to convert back to a a known color (with ToKnownColor), when the Color was created from a known color in the first place. – Bennor McCarthy Aug 02 '10 at 23:06
-
sounds difficult to me,lol,since I have no experience with reflection,but I will try,thanks,Bennor. – Kevin Aug 02 '10 at 23:42
-
1Let me know if you have trouble, and I'll try and put the exact code in tonight. – Bennor McCarthy Aug 02 '10 at 23:52
You will need to first convert the Microsoft.Xna.Framework.Graphics.Color into a System.Drawing.Color.
var color = System.Drawing.Color.FromArgb(a,r,g,b);
Then you get its name (if it has one) with the Name property.

- 645
- 1
- 5
- 11