Thursday, June 23, 2016

Parsing Color strings

This is a reminder to myself about how to parse strings like "MistyRose" and "#ffe4e1" into a strongly typed Color. I haven't performed extensive tests, but all of the methods here seem to support the x11 Color Names and #RGB format.

WPF

Color c = (Color)ColorConverter.ConvertFromString(string value);
You have to cast the result because the return type of this static method is object. There are overloaded instance methods.

System.Drawing

Color c = ColorTranslator.FromHtml(string htmlColor);

No comments:

Post a Comment