- COLOUR SPACE CONVERSION ALGORITHMS
- SEE ALSO
Template::Plugin::Colour - Template plugin for colour manipulation
# long or short hex triplets, with or without '#' [% USE Colour('abc') %] [% USE Colour('#abc') %] [% USE Colour('ff0000') %] [% USE Colour('#ff0000') %] # decimal r, g, b values [% USE Colour(255, 128, 0) %] # named parameters [% USE Colour( red=255, green=128, blue=0 ) %] # explicit colour space [% USE Colour( rgb = [255, 128, 10] ) %] [% USE Colour( hsv = [120, 180, 20] ) %] # alternately, call Colour methods [% USE Colour; # create RGB colours red = Colour.RGB('#c00'); green = Colour.RGB('#0c0'); blue = Colour.RGB('#00c'); # create HSV colours orange = Colour.HSV(30, 255, 255); %]
This Template Toolkit plugin module allows you to define and manipulate colours using the RGB (red, green, blue) and HSV (hue, saturation, value) colour spaces.
As a convenience to our American friends and other international users who spell '
Colour' as '
Color', all the '
Colour' plugin modules have '
Color' equivalents. So you can write either:
[% USE Colour %]
[% USE Color %]
The same is true of the other plugins as well (Color.RGB, Color.HSV).
Creates a new colour object. The first argument can denote the intended colour space: 'rgb' or 'hsv' (either upper or lower case is accepted).
[% USE Colour( rgb = [100, 150, 200] ) %] [% USE Colour( hsv = [120, 140, 160] ) %]
If the colour space argument isn't specified then it defaults to RGB.
[% USE Colour( 100, 150, 200 ) %] # RGB colour
The new() method delegates to RGB() or HSV() depending on the colour space.
Create a new colour object using the RGB colour space. See Template::Plugin::Colour::RGB.
Create a new colour object using the HSV colour space. See Template::Plugin::Colour::HSV.
COLOUR SPACE CONVERSION ALGORITHMS
The algorithms used to covert between the RGB and HSV colour spaces are based on the the C Code in "Computer Graphics -- Principles and Practice,", Foley et al, 1996, p. 592-593.
Due to a limitation in the particular implementation chosen (to use integers rather than floating point numbers to represent RGB and HSV components), the conversion between colour spaces is not totally symmetrical. That is, if you convert a colour from RGB to HSV and then back again, you may not get back exactly the same colour you started with.
What Colour is Orange?
Everyone needs to know what colour orange is in RGB and HSV.
I find the easiest way to remember is that its Hue is 30 degrees, with full Saturation and Value.
[% USE Colour; orange = Colour.HSV(30, 255, 255); %]
Use the 'rgb' method to convert it to RGB, and 'html' to display it as an HTML formatted hex string.
<p style="color: [% orange.rgb.html %]"> I like orange! </p>
As it happens, orange is pretty easy to remember in RGB, too. It's #ff7f00 which is full red (ff), half green (7f) and no blue (00). It just goes to reinforce the widely held belief that orange really is one of the best colours ever. Whoever invented it should probably get an award of some kind, or maybe even a pony.
How Do I Make a Nice Colour Scheme?
Let's start with orange, shall we?
[% USE Colour; orange = Colour.HSV(30, 255, 255) %]
Now copy it twice to create a lighter (more white) version by reducing the saturation, and a darker version (more black) by reducing the value.
[% lighter = orange.copy( saturation = 127 ); darker = orange.copy( value = 127 ); %]
Now you can convert them to RGB for display in your HTML page.
[% orange.html %] => #ff7f00 [% lighter.html %] => #ffbf80 [% darker.html %] => #7f3f00
If you want a strongly contrasting colour, then shift the hue 180 degrees around the colour wheel. In this case, going from 30 to 210 to give a nice shade of blue.
[% contrast = orange.copy( hue = 210 ).html %] => #007fff
How Much More Black Could This Be?
How much more black could this be?
[% black = Colour.RGB %] # defaults to 0, 0, 0
The answer is none. None more black.
This is version 0.04 of the Template::Plugin::Colour module set.
Andy Wardley <email@example.com>, http://wardley.org
Copyright (C) 2006-2012 Andy Wardley. All Rights Reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Written using algorithms from "Computer Graphics -- Principles and Practice", Foley et al, 1996, p. 592-593.