Colour spaces in .NET
Switch branches/tags
Nothing to show
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
Spectrum.Universal Work so far Nov 6, 2014
Spectrum Fix bug in RGB to HSV conversion. Resolves #1 Aug 15, 2016
design logos Nov 10, 2014


What is it?

Spectrum is a library to manipulate colours in the .NET framework, specifically in the RGB, HSL and HSV colour spaces.

It's currently not designed to the be all and end all in terms of colour but more to assist developers in building harmonious colour schemes for their applications.

It exists as two parts:

  1. Spectrum is the core library that contains the classes you'd expect in a library such as this. It's a portable class library covering all the major platforms (math is very portable).
  2. Spectrum.Universal is the interesting part and is where I hope to extend most of all, it's a playground for how we can use colour space calculations in our apps. It's portable across Windows 8 and Windows Phone 8.1.

How does it work?

The core Spectrum library has only three classes Color.RGB, Color.HSL and Color.HSV, these naturally have methods to convert between themselves.

var accent = new Color.RGB(240, 150, 9);

var hsl = accent.ToHSL();

Assert.AreEqual(new Color.HSL(36.6, 0.928, 0.488), hsl);

The Color.HSL class has number of helper methods to create other related colours.

var hsl = new Color.HSL(90, 0.5, 0.5);

var compliment = hsl.ShiftHue(180.0d);

var analogous = hsl.GetAnalogous();

It gets more interesting when we start to use Spectrum.Universal. This contains a few Xaml Value Converters that can shift between Windows.UI.Color instances for us.

Given resource declarations like the following:

<converters:ComplimentConverter x:Key="Compliment" />
<converters:ShiftLightnessConverter x:Key="ShiftLightness" />
<converters:ShiftHueConverter x:Key="ShiftHue" />

and our base accent colour:

<Color x:Key="AccentColor">#FF0088D1</Color>
<SolidColorBrush x:Key="AccentBrush" Color="{StaticResource AccentColor}"/>

We can create lighter and darker versions of that same colour using

<SolidColorBrush x:Key="LightenedAccentBrush" 
		Source={StaticResource AccentColor}, 
		Converter={StaticResource ShiftLightness}, 

<SolidColorBrush x:Key="DarkenedAccentBrush"
		Source={StaticResource AccentColor}, 
		Converter={StaticResource ShiftLightness}, 

as well as create a minor accent colour

<SolidColorBrush x:Key="SecondaryAccentBrush" 
		Source={StaticResource AccentColor}, 
		Converter={StaticResource ShiftHue}, 

We can now manage an app's full palatte of colour resources by tweaking a few base colours.

Where I can get this?

Source is obviously here, and is also on nuget.