Metro Colour ValueConverter

I recently needed a converter that would take one of the Windows Phone accent colours as a string and return a SolidColourBrush of that colour. If anybody else has ever needed this functionality (or just a list of the bush values for the accent colours) I have included it below.

public class MetroColourConverter : IValueConverter 
{
   public object Convert(object value, Type targetType, object parameter, 
      System.Globalization.CultureInfo culture)
   {
      SolidColorBrush scb = new SolidColorBrush();
      
      switch (value as string)
      {
         case "Magenta":
            scb.Color = Color.FromArgb(255, 255, 0, 148);
            return scb;
         case "Purple":
            scb.Color = Color.FromArgb(255, 165, 0, 255);
            return scb;
         case "Teal":
            scb.Color = Color.FromArgb(255, 0, 170, 173);
            return scb;
         case "Lime":
            scb.Color = Color.FromArgb(255, 140, 190, 41);
            return scb;
         case "Brown":
            scb.Color = Color.FromArgb(255, 156, 81, 0);
            return scb;
         case "Pink":
            scb.Color = Color.FromArgb(255, 230, 113, 181);
            return scb;
         case "Orange":
            scb.Color = Color.FromArgb(255, 239, 150, 8);
            return scb;
         case "Blue":
            scb.Color = Color.FromArgb(255, 25, 162, 222);
            return scb;
         case "Red":
            scb.Color = Color.FromArgb(255, 230, 20, 0);
            return scb;
         case "Green":
            scb.Color = Color.FromArgb(255,49,154,49);
            return scb;
        default:
            scb.Color = Color.FromArgb(255,255,255,255);
            return scb;
       }
    }

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

WPColoursThis ValueConverter inherits from IValueConverter but I have not implemented the ConvertBack method.

On the right is a picture of several ListBoxItems (within a ListBox) which have had their Foreground properties bound to the Content property using this converter.

If you have never seen this before and want to know about converters, check out this article on MSDN.

Please remember that you should never hard code an accent colour where possible and use the PhoneAccentColour static resource to reference the colour the user has chosen in the settings

7 thoughts on “Metro Colour ValueConverter

  1. whats wrong with :

    public object Convert(object value, Type targetType, object parameter,
    System.Globalization.CultureInfo culture)
    {
    SolidColorBrush scb = new SolidColorBrush();

    scb.Color.FromName(value as string);
    return scb;
    }

    1. Thanks for your comment!

      The other reason I went down this road is because the colours returned by switching the string name are not the “proper” Metro colours.
      For example, Colors.Magenta is #FFFF00FF but the proper Metro colour value (from where I got them from) is #FFFF0094.

Leave a Reply