tags:

views:

132

answers:

3

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  A: 
var color = System.Drawing.Color.Blue;
var known = color.ToKnownColor();
string name = known != null ? known.ToString() : "";
John Fisher
He's talking about XNA.
Bennor McCarthy
@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
Yeah, there's access to them for sure, but you still need to convert from the XNA color.
Bennor McCarthy
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
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
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.
Robert
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
+1  A: 

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.

Struan
Thanks,but still always get "0" for no matter what color is.
Robert
+1  A: 

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

  1. Convert from XNA color to System color
  2. Try and convert the system color to a known color
  3. 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: http://stackoverflow.com/questions/3391462/convert-string-to-color-in-c/3391583#3391583

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.

Bennor McCarthy
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..
Robert
The 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
sounds difficult to me,lol,since I have no experience with reflection,but I will try,thanks,Bennor.
Robert
Let me know if you have trouble, and I'll try and put the exact code in tonight.
Bennor McCarthy