There are a lot of good free web sites that can create a group of matching colors for you. While it’s nice to go online and create such a palette, it’s another thing to transform a –sometimes lengthy- list of colors on an HTML page into something that can be used in XAML, e.g. a list of brush definitions.
Luckily, most of these web sites have one thing in common: they allow you to export the color palette to a so-called Color Swatch. That’s an Adobe Photoshop file format for storing a list of named colors. Photoshop uses the .aco extension for this file type.
In this blog post, I’ll introduce you to some of these web sites, and I also present a Portable Class Library project together with a Universal Windows Platform app to read a Color Swatch, and export its content to a list of XAML SolidColorBrush definitions that you can use as a resource.
How to create a color palette
Here are some approaches that you can take to come up with a consistent color palette for your app.
The mathematical approach
In the mathematical approach, you hand pick one or two primary colors and calculate the rest of the palette. This calculation is based on the position of the colors in a color wheel. The generated theme will contain your selected base colors, plus their adjacent (a.k.a. analog or harmonious) colors or triad colors or tetrad colors, and optionally their complementary color.
Not only does this site allow you to interactively design a color palette, it also has a lot of preview options: site samples, drawings, and even animations. It also allows you to export the color scheme to different formats, including XML, CSS, and ACO:
There is a lot more in color theory and color psychology than just the color wheel: warmth, shades, tints, hue, and so on. But unless you are a talented designer, a color palette based on the color wheel will have better harmony and contrast (and hence will be better perceived by your users) than a random set of hand picked colors.
The image approach
A second approach for generating a color scheme is analyzing the pixels of an image that you find attractive. There a a lot of web sites around that create a color palette out of an image that you upload.
Here’s an example of a palette that was generated from the picture of a fox, by Pictaculous:
The site not only returns the colors of the calculated palette, but also links to related color schemes that are stored in the libraries of Adobe Kuler and ColourLovers. There’s also an API for all of this. The export format for Pictaculous is … Adobe Photoshop Color Swatch.
Every site or tool uses a different algorithm to pick scheme colors, so you may want to shop around. Here’s what Adobe –my favorite site for this approach- generates from that same fox:
With the image-to-palette approach you rely on the inherent beauty and harmony of nature. Well … more probably you rely on the talent of a photographer, artist, fashion designer, or film maker. When they create an image, they do that with a color palette in mind. That palette is based on … the color wheel. Here’s a nice introduction to cinematic color design, and there are more examples at MoviesInColor (which unfortunately has no export feature).
By the way, it’s not only photos from nature that yield nice color palettes. Here’s what CSSDrive generates from a photo of a city:
Themes that are generated by CSS Drive can be saved as CSS or ACO.
Hint: if you’re looking for candidate pictures for this approach, any Bing background will do:
Get a designer
Of course you can also get a professional graphic designer to create a color palette for you. The odds are very high that he or she will come up with an Adobe Color Swatch. That’s also what Google’s Material Design’s designers have done: you can download all of their color palettes right here. It should be no surprise that this is … an .aco file.
Reading an Adobe Color Swatch file
I was getting tired of copy-pasting individual hexadecimal color codes out of HTML, and into XAML. So I was pleased to discover that they all support the .aco format. The next step was building a Universal Windows Platform app to transform the list of named colors in a Color Swatch into a list of XAML SolidColorBrush definitions.
The C# code that reads the color swatch content is written by Richard Moss, and described in this excellent article. I just copy-pasted the algorithm out of its Windows Forms application into a stand-alone Portable Class Library. So it can be called now from any .NET environment, including Xamarin. I removed the references to File and Color from the original code (they don’t exist in PCL), and stored the algorithm in its own class.
The AcoConverter class has only one public method –ReadPhotoShopSwatchFile– that takes a stream –the content of the .aco file- and returns a collection of SwatchColor – a small helper class.
Here’s a palette from Paletton:
This is how that palette looks like in the app:
And here’s the export:
<SolidColorBrush x:Key='Primary color 1' Color='#FFAA3939' /> <SolidColorBrush x:Key='Primary color 2' Color='#FFFF5555' /> <SolidColorBrush x:Key='Primary color 3' Color='#FFF35151' /> <SolidColorBrush x:Key='Primary color 4' Color='#FF612020' /> <SolidColorBrush x:Key='Primary color 5' Color='#FF180808' /> <SolidColorBrush x:Key='Secondary color (1) 1' Color='#FF226666' /> <SolidColorBrush x:Key='Secondary color (1) 2' Color='#FF4BE2E2' /> <SolidColorBrush x:Key='Secondary color (1) 3' Color='#FF319292' /> <SolidColorBrush x:Key='Secondary color (1) 4' Color='#FF133A3A' /> <SolidColorBrush x:Key='Secondary color (1) 5' Color='#FF050E0E' /> <SolidColorBrush x:Key='Secondary color (2) 1' Color='#FF7B9F35' /> <SolidColorBrush x:Key='Secondary color (2) 2' Color='#FFC3FA53' /> <SolidColorBrush x:Key='Secondary color (2) 3' Color='#FFB0E34C' /> <SolidColorBrush x:Key='Secondary color (2) 4' Color='#FF465A1E' /> <SolidColorBrush x:Key='Secondary color (2) 5' Color='#FF111607' />
The source code lives here on GitHub. As already mentioned, the core logic is in a PCL project, so it can be called from any .NET environment.