Andy Wardley > Template-Plugin-Colour > Template::Colour

Download:
Template-Plugin-Colour-0.05.tar.gz

Dependencies

Annotate this POD

CPAN RT

New  1
Open  0
View/Report Bugs
Module Version: 0.05   Source  

NAME ^

Template::Colour - module for colour manipulation

SYNOPSIS ^

    use Template::Colour;
    
    # various ways to define a Red/Green/Blue (RGB) colour
    $orange = Template::Colour->new('#ff7f00');
    $orange = Template::Colour->new( rgb => '#ff7f00' );
    $orange = Template::Colour->new({ rgb => '#ff7f00' });
    $orange = Template::Colour->new( rgb => [255, 127, 0] );
    $orange = Template::Colour->new({ rgb => [255, 127, 0] });
    $orange = Template::Colour->new({
        red   => 255, 
        green => 127,
        blue  => 0,
    });
    
    # or go direct to the RGB method
    $orange = Template::Colour->RGB('#ff7f00');
    $orange = Template::Colour->RGB(255, 127, 0);
    # ...etc...
    
    # methods to fetch/modify the red, green and blue components
    print $orange->red();     # 255
    print $orange->green();   # 127
    print $orange->blue();    # 0
    
    # various ways to define a Hue/Saturation/Value (HSV) colour
    $orange = Template::Colour->new( hsv => [25, 255, 255] );
    $orange = Template::Colour->new({ hsv => [25, 255, 255] });
    $orange = Template::Colour->new({
        hue        =>  25,    # 0-359 degrees around colour wheel
        saturation => 255,    # 0-255
        value      => 255,    # 0-255
    });
    $orange = Template::Colour->new({
        hue =>  25,
        sat => 255,    # because life is too short for
        val => 255,    # typing long parameter names
    });
    
    print $orange->hue();        # 25
    print $orange->saturation(); # 255
    print $orange->sat();        # 255
    print $orange->value();      # 255
    print $orange->val();        # 255
    
    # convert freely between colour spaces
    $orange = $orange->rgb();    # $orange is now RGB
    $orange = $orange->hsv();    # $orange is now HSV

DESCRIPTION ^

This module allows you to define and manipulate colours using the RGB (red, green, blue) and HSV (hue, saturation, value) colour spaces.

It delegates to the Template::Colour::RGB and Template::Colour::HSV modules to do all the hard work.

METHODS ^

new(@args)

Creates a new colour object by delegation to either of the RGB() or HSV() methods, depending on the arguments passed.

See the SYNOPSIS above for examples of use.

RGB(@args)

Creates a new Template::Colour::RGB object using the Red/Green/Blue (RGB) colour space.

HSV(@args)

Creates a new Template::Colour::HSV colour object using the Hue/Saturation/Value (HSV) colour space.

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.

EXAMPLES ^

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 Template::Colour;
    my $orange = Template::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.

    my $html_hex = $orange->rgb->html();

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?

    my $orange = Template::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.

VERSION ^

This is version 0.04 of the Template::Plugin::Colour module set.

AUTHOR ^

Andy Wardley <abw@cpan.org>, http://wardley.org

COPYRIGHT ^

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.

ACKNOWLEDGEMENTS ^

Written using algorithms from "Computer Graphics -- Principles and Practice", Foley et al, 1996, p. 592-593.

SEE ALSO ^

Template::Colour::RGB, Template::Colour::HSV

syntax highlighting: