4

I search a way to save the color of a brush as a string. For example I have a Brush which has the color red. Now I want to write "red" in a textbox.

Thanks for any help.

Waronius
  • 363
  • 2
  • 4
  • 10
  • 3
    Which `Brush` class do you mean exactly? From what namespace? – Oded Oct 11 '12 at 14:32
  • Possible solution? http://stackoverflow.com/q/372693/122005 – chridam Oct 11 '12 at 14:35
  • I mean the Brush class under System.Windows.Media.Brush – Waronius Oct 11 '12 at 14:35
  • 1
    If you're using a `SolidColorBrush` this is easy - try `brush.Color.ToString()`, but I think this might give an RGB representation rather than a name like 'red'. – Oliver Oct 11 '12 at 14:35
  • @chridam: He wants string => Brush, I want Brush => string – Waronius Oct 11 '12 at 14:37
  • 1
    Do you have some ideas on how specific the color should be? i.e. converting `#FF0000` and `#DC143C` both look very close to "red" but are very different colors. Most `ToString(`) representations will try to preserve the detail... If you want a generic color name, you'll probably have to do the work yourself. – jheddings Oct 11 '12 at 14:37

4 Answers4

3

What type of brush is this? If its drawing namespace, then brush is an abstract class.! For SolidBrush, do:

brush.Color.ToString()

Otherwise, get the color property and use ToString() method to convert color to its string representation.

  • I mean the Brush class under System.Windows.Media.Brush. When I make in this class .toString will I become the number of the color, for example: #FF0000FF – Waronius Oct 11 '12 at 14:44
2

If the Brush was created using a Color from System.Drawing.Color, then you can use the Color's Name property.

Otherwise, you could just try to look up the color using reflection

// hack
var b = new SolidBrush(System.Drawing.Color.FromArgb(255, 255, 235, 205));
var colorname = (from p in typeof(System.Drawing.Color).GetProperties()
                 where p.PropertyType.Equals(typeof(System.Drawing.Color))
                 let value = (System.Drawing.Color)p.GetValue(null, null)
                 where value.R == b.Color.R &&
                       value.G == b.Color.G &&
                       value.B == b.Color.B &&
                       value.A == b.Color.A
                 select p.Name).DefaultIfEmpty("unknown").First();

// colorname == "BlanchedAlmond"

or create a mapping yourself (and look the color up via a Dictionary), probably using one of many color tables around.

Edit:

You wrote a comment saying you use System.Windows.Media.Color, but you could still use System.Drawing.Color to look up the name of the color.

var b = System.Windows.Media.Color.FromArgb(255, 255, 235, 205);
var colorname = (from p in typeof(System.Drawing.Color).GetProperties()
                 where p.PropertyType.Equals(typeof(System.Drawing.Color))
                 let value = (System.Drawing.Color)p.GetValue(null, null)
                 where value.R == b.R &&
                       value.G == b.G &&
                       value.B == b.B &&
                       value.A == b.A
                 select p.Name).DefaultIfEmpty("unknown").First();
sloth
  • 99,095
  • 21
  • 171
  • 219
  • Thanks, I have changed to System.Drawing.Color and now I become with .Name the right name of the color. Many thanks! – Waronius Oct 11 '12 at 15:08
0

Basically I'll post what was already answered.

string color = textBox1.Text;

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

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

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

// in WPF you can use a BrushConverter
SolidColorBrush redBrush = (SolidColorBrush)new BrushConverter().ConvertFromString(color);

Original answer: Convert string to Brushes/Brush color name in C#

Community
  • 1
  • 1
inzenir
  • 116
  • 5
0

I had a object of System.Drawing.Brush and the color information is not accessible. It couldn't be case to type color either. It is possible to cast this to SolidBrush where the color information is available. I was able to cast my Brush color object to a SolidBrush and then pull the name from the color this way.

((SolidBrush)color).Color.Name
Dan Hastings
  • 3,241
  • 7
  • 34
  • 71