I am encountering a problem which is how do I convert input strings like “RED” to the actual Color type Color.Red in C#. Is there a good way to do this?
I could think of using a switch statement and cases statement for each color type but I don’t think that is clever enough.
The MSDN doesn’t say one way or another, so there’s a good chance that it is case-sensitive. (UPDATE: Apparently, it is not.)
As far as I can tell,
ColorTranslator.FromHtmlis also.If
Color.FromNamecannot find a match, it returnsnew Color(0,0,0);If
ColorTranslator.FromHtmlcannot find a match, it throws an exception.UPDATE:
Since you’re using Microsoft.Xna.Framework.Graphics.Color, this gets a bit tricky: