43

I have a configuration file where a developer can specify a text color by passing in a string:

 <text value="Hello, World" color="Red"/>

Rather than have a gigantic switch statement look for all of the possible colors, it'd be nice to just use the properties in the class System.Drawing.Brushes instead so internally I can say something like:

 Brush color = Brushes.Black;   // Default

 // later on...
 this.color = (Brush)Enum.Parse(typeof(Brush), prasedValue("color"));

Except that the values in Brush/Brushes aren't enums. So Enum.Parse gives me no joy. Suggestions?

Clinton Pierce
  • 12,859
  • 15
  • 62
  • 90

10 Answers10

81

Recap of all previous answers, different ways to convert a string to a Color or Brush:

// best, using Color's static method
Color red1 = Color.FromName("Red");

// using a ColorConverter
TypeConverter tc1 = TypeDescriptor.GetConverter(typeof(Color)); // ..or..
TypeConverter tc2 = new ColorConverter();
Color red2 = (Color)tc.ConvertFromString("Red");

// using Reflection on Color or Brush
Color red3 = (Color)typeof(Color).GetProperty("Red").GetValue(null, null);

// in WPF you can use a BrushConverter
SolidColorBrush redBrush = (SolidColorBrush)new BrushConverter().ConvertFromString("Red");
Lucas
  • 17,277
  • 5
  • 45
  • 40
  • 3
    Mind adding how you'd do this with WPF's XAML as well? – Chuck Savage Jun 05 '13 at 17:10
  • I get this error: `Error 1 'System.Windows.Media.Color' does not contain a definition for 'FromName' ` VS Express 2012, and I am in a standard WPF application codebehind. – Matt Apr 07 '15 at 18:55
  • 1
    @Matt FromColor() is a member of `Windows.Drawing.Color` which is not a WPF class. – escape-llc Aug 11 '15 at 14:07
50

String to brush:

myTextBlock.Foreground = new BrushConverter().ConvertFromString("#FFFFFF") as SolidColorBrush;

That's my case here!

Junior Mayhé
  • 16,144
  • 26
  • 115
  • 161
16

A brush can be declared like this

Brush myBrush = new SolidBrush(Color.FromName("Red"));
WhatsThePoint
  • 3,395
  • 8
  • 31
  • 53
8

D'oh. After a while of looking I found:

 Color.FromName(a.Value)

After hitting "post". From there it's a short step to:

 color = new SolidBrush(Color.FromName(a.Value));

I'll leave this question here for others....

Clinton Pierce
  • 12,859
  • 15
  • 62
  • 90
  • Strictly speaking, it's a "coincidence" that the static properties on Brushes use the same name as the static properties on Color. However, that's probably nothing to worry about. – Jon B Dec 16 '08 at 21:12
3

You could use reflection for this:

Type t = typeof(Brushes);
Brush b = (Brush)t.GetProperty("Red").GetValue(null, null);

Of course, you'll want some error handling/range checking if the string is wrong.

Jon B
  • 51,025
  • 31
  • 133
  • 161
  • Unlike other answers, this one does not create new brush instances, which is good, see https://stackoverflow.com/questions/40904567/how-do-i-prevent-memory-leak-from-solidcolorbrush-object – Sasha Yakobchuk Oct 22 '18 at 22:40
1

I agree that using TypeConverters are the best method:

 Color c = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString("Red");
 return new Brush(c);
Brian Rudolph
  • 6,142
  • 2
  • 23
  • 19
0

If you want, you can extend this even more and allow them to specify values for the R, G and B values. Then you just call Color.FromArgb(int r, int g, int b);

BFree
  • 102,548
  • 21
  • 159
  • 201
-1

Try using a TypeConverter. Example:

var tc = TypeDescriptor.GetConverter(typeof(Brush));

Another alternative is to use reflection, and go over the properties in SystemBrushes.

leppie
  • 115,091
  • 17
  • 196
  • 297
  • TypeDescriptor cannot convert from string to Brush. It can convert string to Color, though... – Lucas May 22 '09 at 20:10
-1

I was getting namespace conflicts when trying solutions to this question. Turns out Brush and Color classes exists in two difference namespaces System.Windows.Media and System.Drawing and they have similar function names but there are differences too.

If you working with controls like below, it uses Media namespace so make sure you have the correct namespace added wherever you use this objects.

<Button Content="Change Color" Background="AliceBlue"/>

This part in the accepted answer is really right(best) answer but it's good to know about the namespaces.

Brush color = (SolidColorBrush)new BrushConverter().ConvertFromString("Green");

Do note that the following (as suggested in accepted answer) doesn't compile in this namespace.

Color red1 = Color.FromName("Red"); // don't even try, its wrong namespace

Because the Color class in System.Windows.Media doesn't have FromName() member so it's not even applicable.

So so many solutions above but nobody has mentioned namespaces and the answer really depends on that. This question is actually about the Media namespace because that has color names but not mentioning that can confuse new readers who is trying these solutions.

Many people like myself may even not even need the conversion and use the enum as noted in the question itself.

Brush color = Brushes.Red;
zar
  • 11,361
  • 14
  • 96
  • 178
-2

You can use System.Drawing.KnownColor enum. It specifies all known system colors.

Const Mi
  • 39
  • 3