SharePoint Dev Notes

Notes on SharePoint Development

Convert Color Scheme Designer 3 to XAML Resources

1 Comment

In a recent project, I wanted a quick way to change out color schemes for a Silverlight project. One resource that I have used to build sample color schemes is the Color Scheme Designer 3 by Petr Stanicek. The designer allows the color scheme to be exported to an XML file, but the format doesn’t lend itself to being quickly imported as a XAML resource file — so I decided to write a little console application to convert it to a XAML resource friendly file. You could also simply to an XSLT transformation, but this was used as a way for me to learn to use LINQ to XML to both read and write XML files. If you find this useful, enjoy — but this is so I can have a reference to it at a later time.

    public class SchemeConverter
    {
        static void ConvertTheme(String inFileName, String outFileName)
        {
            XElement doc = XElement.Load(inFileName);

            var colorsets = (from set in doc.Descendants(XName.Get("colorset"))
                             select new ColorSet
                                 {
                                     Title = set.Attribute(XName.Get("title")).Value,
                                     Colors = (from color in set.Descendants(XName.Get("color"))
                                               select new Color
                                               {
                                                   Title = color.Attribute(XName.Get("id")).Value,
                                                   RGB = color.Attribute(XName.Get("rgb")).Value
                                               }).ToList()
                                 }).ToList();

            XNamespace defaultNamespace = XNamespace.Get("http://schemas.microsoft.com/winfx/2006/xaml/presentation");
            XNamespace x = XNamespace.Get("http://schemas.microsoft.com/winfx/2006/xaml");

            XElement silverDoc = new XElement(defaultNamespace + "ResourceDictionary",
                                new XAttribute(XNamespace.Xmlns + "x", x));

            foreach (ColorSet CS in colorsets)
            {
                foreach(Color C in CS.Colors)
                {
                    XElement FullColor = new XElement("Color", new XAttribute(x + "Key", C.Title.ToUpper() + "-FULL"), "#FF" + C.RGB.ToUpper());
                    XElement ThreeQuarterColor = new XElement("Color", new XAttribute(x + "Key", C.Title.ToUpper() + "-THREEQUARTER"), "#BF" + C.RGB.ToUpper());
                    XElement HalfColor = new XElement("Color", new XAttribute(x + "Key", C.Title.ToUpper() + "-HALF"), "#80" + C.RGB.ToUpper());
                    XElement OneQuarterColor = new XElement("Color", new XAttribute(x + "Key", C.Title.ToUpper() + "-ONEQUARTER"), "#40" + C.RGB.ToUpper());
                    XElement ClearColor = new XElement("Color", new XAttribute(x + "Key", C.Title.ToUpper() + "-CLEAR"), "#00" + C.RGB.ToUpper());

                    silverDoc.Add(FullColor);
                    silverDoc.Add(ThreeQuarterColor);
                    silverDoc.Add(HalfColor);
                    silverDoc.Add(OneQuarterColor);
                    silverDoc.Add(ClearColor);
                }
            }

            silverDoc.Save(outFileName);
        }
    }
About these ads

One thought on “Convert Color Scheme Designer 3 to XAML Resources

  1. Pingback: Convert Color Scheme Designer 3 to XAML Resources | SharePoint Dev … | Mastering Sharepoint

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 492 other followers