Experiments with HSL colour and higher order functions

Posted: August 11, 2012 in Uncategorized

The Hue/Saturation/Lightness colour model is useful tool in graphics processing, so I decided to implement it in Clisk.

This manifests itself in two new functions:

  • rgb-from-hsl : converts an HSL colour vector into an RGB colour vector
  • hsl-from-rgb : converts an RGB colour vector into an HSL colour vector

A simple example is to create an HSL spectrum by varying the Hue and Lightness components according to the x and y position in the image respectively, while keeping the Saturation maxed at 1.0:

(show (rgb-from-hsl [x 1.0 y]))

Image

We can now compose the basic HSL functions to make more powerful image manipulation capabilities. Most of the time we will be working in RGB colour space, but the HSL space can be useful for specific transformations. So the general approach is:

  • Convert an image into HSL space
  • Apply some transformation to the HSL values
  • Convert the result back into RGB space

As an example, the following function can be used to adjust the Hue of an image while keeping the Saturation and Lightness constant:

(defn adjust-hue [shift source]
  (rgb-from-hsl 
    (v+ [(component 0 shift) 0.0 0.0]
        (hsl-from-rgb source))))

This is effectively a higher-order-function for image generation – since both the hue shift and the source function are themselves functions.

Using a constant value for the hue shift enables us to get some fun hue-adjusting effects very easily, for example using a sample photo of a nice old cannon I discovered in Melaka:

(def cannon (texture-map "Cannon.jpg"))

(show cannon)

(show (adjust-hue -0.1 cannon))

ImageImage

But it really gets fun when you start using variable functions for the hue shift. I particularly liked this effect, obtained by defining a new psychedelic function:

(defn psychedelic [src]
  (adjust-hue (scale 0.2 noise) src)) 

(show (psychedelic cannon))

Image:

We now have a psychedelic effect that can be applied to any image – nice! In the next Clisk release I’m planning to build a small library of these “effects” that will hopefully be generally useful as well as providing some good examples of how to do “higher order” image generation in Clisk.

Advertisements

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