Hue, Luminance and Saturation in .NET

Convert to and from HLS using Managed Code

HLS - RGB Sample

The .NET System.Drawing.Color package allows you to obtain Hue, Luminance and Saturation values but puzzlingly do not allow you to set them. This article provides a HLSRGB colour class allowing you to read and write HLS and then demonstrates how to draw a HLS colour wheel.

About Hue, Saturation and Luminance

Normally in computers colours are described in terms of their Red, Green and Blue components. Whilst you can specify all displayable colours this way, it leaves something to be desired when it comes to picking a colour. For example, most people find it very difficult to determine what RGB values you would use to create a Pink or Brown colour (try it!).

There are numerous colour models which attempt to work around this limitation and provide a more intuitive way of getting to the colour you want. One the simpler among them is the Hue Lightness and Saturation (HLS) Model.

The HLS model describes colours in the following terms:

  • Hue, which varies from magenta - red - yellow - green - cyan - blue - magenta, described as an angle around a circle from 0.0 - 360.0 degrees.
  • Saturation, which varies from 0.0 and 1.0 and describes how "grey" the colour is, with 0 being completely unsaturated (grey, white or black) and 1 being completely saturated.
  • Lightness, which varies from 0.0 and 1.0 and ranges from black at 0.0, through the standard colour itself at 0.5 to white at 1.0.

With this model it is easier to pick the correct colour. The model also allows you to do some things in code which you can't really achieve with RGB, such as determining what a lighter or darker tone of a given RGB colour is.

Limitations of the HLS Model

Whilst the HLS Model is simple to implement, and gives a relatively easy colour picking interface, it is not a physically accurate model. That is to say, linear changes in lightness, hue or saturation do not correspond to a linear change to the eye. More accurate models which overcome these limitations do exist, such as the CIE scheme, but the increased complexity of the model makes them harder to implement and harder for a user to interact with. Anyway, HLS works well enough unless you are interested in exact colour matching and processing between different display devices.

The Code

The code in the download provides VB and C# versions of the HLSRGB class, which provide you with read/write Hue, Lightness and Saturation values as well as the standard R,G,B. You can construct an instance of the class from a System.Drawing.Color, from the R,G,B values or from the H,L,S values.

Using this code, the sample application provides three sliders allowing you to adjust H, L and S. It then draws a colour wheel using standard System.Drawing methods to show all of the other hues at the specified Lightness and Saturation (adjusting the Hue simply causes the colour wheel to rotate).

Finally, there is a ComboBox containing all of the colors defined in System.Drawing.KnownColor which uses the OwnerDraw style to show a sample of the colour being picked in the drop-down.