Tom Fischer

  Home  |   Contact  |   Syndication    |   Login
  21 Posts | 0 Stories | 44 Comments | 0 Trackbacks

News

Archives

Post Categories

.NET Framwork

TFS

Tools

Visual Studio

I ran into this problem last night when I was preparing a bigger demo for my blog. It took me some time to find a solution for a simple but important piece. I decided to go ahead an break the demo down into smaller pieces.

The problem is simple: How to get a Color Object from a String of Text  (e.g. AliceBlue, Blanched Almond, or just Blue)

At the Silverlight Forum I found a good link which I used as a base:
http://silverlight.net/forums/p/9134/28837.aspx

Here is my modified snippet:

private Color getColorFromString(string colorString)
{
 Type colorType = (typeof(System.Windows.Media.Colors));
 
if (colorType.GetProperty(colorString) != null)
 
{
   object color = colorType.InvokeMember(colorString, BindingFlags.GetProperty, null, null, null);
   if (color != null)
   {
 
    return (Color)color;
   }
   throw new InvalidCastException("Color not defined");

}

I decided to throw an InvalidCastException so I can process the Exception later on.  Of course this also could be the place to return a default Color right now. I just like to keep it more flexible.

To be able to use it as a Converter Parameter in UI Bindinding,  I wrapped it into a ColorConverter:
public class ColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
  
   if
(value.GetType() == typeof(String))
   {
    
try
    
{
      
Color c = getColorFromString(value as string);
      
return new SolidColorBrush(c);
     }
   
catch (InvalidCastException ex)
    {
     
return null;
    }
  }
 return null;
}

 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
 {
   throw new NotImplementedException();
 }
}

I decided here to return null in case something goes wrong. In this case the binding Framework Element has the chance to use an attached Property form its parent.

Let me know if you have an issus with it or if you have a suggestion to make the code better. Feedback is always highly welcome.

Next time I will present the rest of the demo with a full blown solution. So stay tuned in.

Tom

P.S.: Is there a tool to format the codesnippets automatically?

posted on Thursday, August 21, 2008 1:44 PM

Feedback

# re: Silverlight 2: How to get a Color or a SolidColorBrush from a String (by ColorName) for Binding 8/21/2008 4:04 PM Randolpho
I'm extremely disappointed Microsoft didn't include System.Windows.Media.ColorConverter in Silverlight 2. Using reflection for this strikes me as amazingly inelegant.

Also, keep in mind that System.Windows.Media.Colors in Silverlight 2 only supports about a dozen bland colors, not the dozens and dozens of "dazzling colors" that the original class supports. Running from intellisense, it looks like Colors supports:

Black, Blue, Brown, Cyan, DarkGray, Gray, Green, LightGray, Magenta, Orange, Purple, Red, Transparent, White, Yellow.

Given that, I think a more elegant solution might be a massive switch statement with all the names you want to support.

# re: Silverlight 2: How to get a Color or a SolidColorBrush from a String (by ColorName) for Binding 11/10/2008 8:47 AM TheTucko
If you want to get a Color from a String (like #FF123456) you can go like this:

//For test I have enetered const color #FF909cd0

private string _gripBarColor = "#FF909cd0";

//then use Convert.ToByte method to convert Hex code

SolidColorBrush brush = new SolidColorBrush();
Color c = new Color();
c.A = Convert.ToByte(_gripBarColor.Substring(1, 2), 16);
c.R = Convert.ToByte(_gripBarColor.Substring(3, 2), 16);
c.G = Convert.ToByte(_gripBarColor.Substring(5, 2), 16);
c.B = Convert.ToByte(_gripBarColor.Substring(7, 2), 16);
brush.Color = c;
someControl.Background = brush;


# re: Silverlight 2: How to get a Color or a SolidColorBrush from a String (by ColorName) for Binding 1/19/2009 4:34 PM Mark
Here's the part I don't understand: the conversion is obviously present somewhere, since you can use a #rrggbb string to initialize a color property in XAML in SL2. I assume they must have written a type converter as above, and just didn't expose it. Is there that much overhead that comes along with bringing in ColorConverter?

# re: Silverlight 2: How to get a Color or a SolidColorBrush from a String (by ColorName) for Binding 7/29/2009 8:17 PM Scott Marlowe
I was trying to solve this same problem and you got me started. I did ultimately go with the solutions proposed in these two links, though. Not the most elegant, but it works, and gives access to all of the WPF colors.

http://silverlight.net/forums/p/13225/43867.aspx

http://stackoverflow.com/questions/1203843/accessing-all-the-many-colors-of-silverlight

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: