HTML::Rainbow - Put colour into your HTML


This document describes version 0.06 of HTML::Rainbow, released 2009-10-04.


  use HTML::Rainbow 'rainbow';
  print rainbow('hello, world');


HTML::Rainbow will take plain text string (or array of strings) and mark it up with <font> tags (or <span> tags if you're feeling particularly orthodox), and produce text that drifts endlessly from one colour to the next.

The intensity of the red, green and blue channels follow mutually prime sinusoidal periods.

This comes in handy when you have the burning desire to say

Perl is a language optimized for scanning arbitrary text files, extracting information from those text files, and printing reports based on that information. It's also a good language for many system management tasks. The language is intended to be practical (easy to use, efficient, complete) rather than beautiful (tiny, elegant, minimal).

Win friends, and influence enemies, on your favourite HTML bulletin board.



Creates a new HTML::Rainbow object. A set of key/value parameters can be supplied to control the finer details of the object's behaviour.

The colour-space of HTML is defined by a tuple of red, green and blue components. Each component can vary between 0 and 255. Setting all components to 0 produces black, and setting them all to 255 produces white. The parameters for new() allow you to control the behaviour of the components, either individually or as a whole.

Each value may be specifed as a number from 0 to 255, or as a percentage (such as 50%). Percentages are rounded to the nearest integer, and values out of range are clipped to the nearest bound.


Sets the minimum value for all three components. For example, a value of 0 (zero) may result in white being produced. This may produce invisible text if the background colour is also white. Hence, one may wish to use a value between 20 to 40 if this is the case.


Sets the maximum value for all three components. Setting it to 100% or 255 may result in black being produced. A similar warning concerning a background colour of black applies here.

min_red, min_green, min_blue

Sets the minimum value for the specified colour component.

max_red, max_green, max_blue

Sets the maximum value for the specified colour component.

red, green, blue

Sets the value of the specified colour component to a fixed value. For example, the following call to new()...

  my $r = HTML::Rainbow->new(
    red      =>   0,
    green    =>   0,
    min_blue =>  10,
    max_blue => 240,

... will result in a rainbow generator that moves through various shades of blue.


Set the periods available to choose from. At each peak and trough of the sine wave followed by each colour component, a new period length is chosen at random. This is to ensure that the sequence of colours does not repeat itself too rapidly. Prime numbers are well suited, and the value of period should be at least 10 (ten) or more for best results. A list of periods, from 17 to 79, is used by default. Very long texts will benefit from longer periods. The parameter is a reference to an array.

  my $r = HTML::Rainbow->new(
    min => 0,
    max => '80%',
    period_list => [qw[ 19 37 53 71 89 107 131 151 173 193 ]],

Use the HTML <span> element instead of the <font> element for specifying the colour. The result uses 6 more characters per marked up character.

The most specific parameter wins. If both, for example, a red and a red_min parameter are found, the red parameter wins. If a red_min and a min parameter is found, the red_min parameter wins.


Converts each passed parameter to rainbowed markup, and returns a single scalar with the resulting marked up text.

  print $r->rainbow( 'somewhere over the rainbow, bluebirds fly' );

You can avoid using an intermediate variable by chaining the rainbow method on from the new method:

  print HTML::Rainbow->new(
    max => 127,
    min =>   0,
    period_list => [qw[ 11 29 47 71 97 113 149 173 ]],
  )->rainbow( $text );





The individual red, green and blue colour components follow sinewaves produced by this module.


If you want to modify an existing HTML page, you'll probably have to parse it in order to extract the text. The eg directory contains some examples to show how this may be done.


The following example produces a valid HTML page.

  use strict;
  use warnings;

  use CGI ':standard';
  use HTML::Rainbow;

  print header(),
    HTML::Rainbow->new->rainbow('hello, world'),


None known.

Please report all bugs at|

Make sure you include the output from the following two commands:

  perl -MHTML::Rainbow -le 'print $HTML::Rainbow::VERSION'
  perl -V


This module is dedicated to John Lang, someone I used to work with back in the early days of the web. I found him one day painstakingly writing HTML in a text editor and reviewing the results in Netscape. He was trying to do something like this, to post to a bulletin board, so I wrote some very ugly Perl to help him out. Ten years later, I finally got around to cleaning it up.


David Landgren, copyright (C) 2005-2009. All rights reserved.

If you (find a) use this module, I'd love to hear about it. If you want to be informed of updates, send me a note. You know my first name, you know my domain. Can you guess my e-mail address?


This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.