Sample code to import colors from Kuler into Visio. This code makes use of LINQ to XML, C# 3.0, Visual Studio 2008 Beta 2, Visual 2007, the AutoVisio library, Adobe Kuler, and the Kuler API.
This code just takes the highest rated Kuler colors and draws them in Visio.
The sample code follows ...
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using AutoVisio;
using Visio = Microsoft.Office.Interop.Visio;
namespace KulerUtil
{
public class KulerLib
{
public static System.Drawing.Color SwatchFromFromXmlElement(XElement swatch_el)
{
string mode = swatch_el.Element("mode").Value;
int[] indices = { 1, 2, 3, 4 };
var color_components = indices
.Select(x => "c" + x.ToString())
.Select(x => swatch_el.Element(x).Value)
.Select(x => double.Parse(x))
.ToArray();
System.Drawing.Color final_color;
if (mode == "rgb")
{
Isotope.Drawing.RGBColor color = new Isotope.Drawing.RGBColor(color_components[0], color_components[1], color_components[2]);
final_color = color.ToSystemDrawingColor();
}
else if (mode == "hsv")
{
Isotope.Drawing.HSVColor color = new Isotope.Drawing.HSVColor(color_components[0], color_components[1], color_components[2]);
final_color = color.ToSystemDrawingColor();
}
else
{
throw new System.Exception("Unknown mode type");
}
return final_color;
}
public static Theme ThemeFromXmlElement(XElement theme_el)
{
Theme theme = new Theme();
theme.Name = theme_el.Element("label").Value;
theme.Author = theme_el.Element("author").Element("label").Value;
theme.Swatches = theme_el.Descendants("swatch")
.Select(x => KulerUtil.KulerLib.SwatchFromFromXmlElement(x))
.ToList();
return theme;
}
public static List<Theme> GetThemesFromURL(string url)
{
List<KulerUtil.Theme> themes = new List<KulerUtil.Theme>();
System.Xml.Linq.XDocument doc = XDocument.Load(url);
foreach (var theme_el in doc.Descendants("theme"))
{
KulerUtil.Theme theme_obj = KulerUtil.KulerLib.ThemeFromXmlElement(theme_el);
themes.Add(theme_obj);
}
return themes;
}
}
public class Theme
{
public string Name;
public string Author;
public List<System.Drawing.Color> Swatches;
}
}
namespace DemoDrawKuler
{
class Program
{
static void Main(string[] args)
{
string url = "http://kuler.adobe.com/kuler/services/theme/getList.cfm?listtype=rating";
List<KulerUtil.Theme> themes = KulerUtil.KulerLib.GetThemesFromURL(url);
Visio.Application visio_app = AutoVisio.VisioUtil.LaunchVisioApplication();
visio_app.CreateDocument(new Isotope.Drawing.Size(10, themes.Count()+2 ));
visio_app.ActiveWindow.ShowGrid = 0;
Visio.Page page = visio_app.ActivePage;
Isotope.Drawing.GridUnboundLayout layout = new Isotope.Drawing.GridUnboundLayout(
new Isotope.Drawing.Point(0, 0),
new Isotope.Drawing.Size(0.8, 0.8),
new Isotope.Drawing.Size(0.0, 0.2));
for (int theme_n=0;theme_n<themes.Count;theme_n++)
{
int row = themes.Count - theme_n - 1;
KulerUtil.Theme cur_theme = themes[theme_n];
Visio.Shape shape1 = page.DrawRectangle( layout.GetRangeRect(0,row,3,row));
shape1.Text = cur_theme.Name + "\nby " + cur_theme.Author;
shape1.SetTextHAlign(TextHAlign.Right);
shape1.SetLinePattern(0);
for (int color_n = 0; color_n < cur_theme.Swatches.Count; color_n++)
{
Visio.Shape shape2= page.DrawRectangle(layout.GetRangeRect(color_n+4, row, color_n+4, row));
System.Drawing.Color cur_color = cur_theme.Swatches[color_n];
Isotope.Drawing.HSLColor hsv_color = Isotope.Drawing.HSLColor.FromSystemDrawingColor(cur_color);
if (hsv_color.Luminance < 0.5)
{
shape2.SetTextColor(System.Drawing.Color.White);
}
shape2.Text = Isotope.Drawing.ColorUtil.ToWebColorString(cur_color);
shape2.SetFillForegroundColor(cur_theme.Swatches[color_n]);
shape2.SetLinePattern(0);
}
}
Visio.Shape shape3 = page.DrawRectangle(layout.GetRangeRect(0, themes.Count, 8, themes.Count));
shape3.Text = "From Adobe Kuler\n" + "URL: " + url ;
shape3.SetFillForegroundColor(System.Drawing.Color.Black);
shape3.SetTextColor(System.Drawing.Color.White);
page.ResizePageToFitContents(new Isotope.Drawing.Size(1, 1));
}
}
}