Color::Calc - Simple calculations with RGB colors.
use Color::Calc ();
my $background = 'green';
print 'background: ',Color::Calc::color_html($background),";\n";
print 'border-top: solid 1px ',Color::Calc::light_html($background),";\n";
print 'border-bottom: solid 1px ',Color::Calc::dark_html($background),";\n";
print 'color: ',Color::Calc::contrast_bw_html($background),";\n";
The Color::Calc module implements simple calculations with RGB colors. This can be used to create a full color scheme from a few colors.
This class method creates a new Color::Calc object.
my $cc = new Color::Calc( 'ColorScheme' => 'X', OutputFormat => 'HTML' );
print $cc->invert( 'white' );
It accepts the following parameters:
One of the color schemes accepted by Graphics::ColorNames, which is used to interpret color names on input. Valid values include X (color names used in X-Windows) and HTML (color names defined in the HTML 4.0 specification). For a full list of possible values, please refer to the documentation of of Graphics::ColorNames.
Unlike Graphics::ColorNames, barewords are always interpreted as a module name under Graphics::ColorNames. If you really want to use a filename like "foo", you have to write it as "./foo".
Default: X (Note: This is incompatible with HTML color names).
One of the output formats defined by this module. Possible values are:
Returns a list of three values in the range 0..255. The first value is guaranteed to have a length that is not a multiple of three.
Returns a hexadecimal RGB value as a scalar that contains a string in the format RRGGBB and a number representing the hexadecimal number 0xRRGGBB.
Returns a string compatible with W3C's HTML and CSS specifications, i.e. #RRGGBB or one of the sixteen HTML color names.
(DEPRECATED) Returns a Color::Object reference. The module Color::Object must be installed, of course.
Returns a Graphics::ColorObject reference. The module Graphics::ColorObject must be installed, of course.
Returns a string compatible with PDF::API2, i.e. #RRGGBB.
(DEPRECATED) Uses the value of $Color::Calc::MODE to select one of the above output formats. You should use local when setting this variable:
local $Color::Calc::MODE = 'html';
Default: __MODEvar (for compatibility)
This method creates a new, hidden object and binds its methods to the namespace of the calling module.
This method is usually not called directly but from perl's use statement:
'ColorScheme' => 'X',
'OutputFormat' => 'HTML',
'Prefix' => 'cc' );
print cc_invert( 'white' ); # prints 'black'
On import, you can specify the following parameters:
Adds a prefix to the front of the method names. The calculation methods are bound to the name prefix_method_name (the specified prefix, an underscore, the calculation method's name). Further, prefix is made an alias for prefix_get.
Please note that with perl's use and import statemehts, omitting the list and specifying an empty list has different meanings:
use Color::Calc; # import with default settings (see below)
use Color::Calc(); # don't import anything
These methods are inaccessible without a object reference, i.e. when the functions have been imported.
Changes the output format for an existing Color::Calc object.
All calculation methods always accept the following formats for $color or $color1/$color2:
An arrayref pointing to an array with three elements in the range 0..255 corresponding to the red, green, and blue component.
A list of three values in the range 0..255 corresponding to the red, green, and blue component where the first value does not have 3 or a multiple of 3 digits (e.g. ('0128',128,128)).
A string containing a hexadecimal RGB value like #RGB/#RRGGBB/#RRRGGGBBB/..., or RGB/RRGGBB/RRRGGGBBB/...
A color name accepted by Graphics::ColorNames. The interpretation is controlled by the ColorScheme parameter.
A Graphics::ColorObject reference.
The calculation methods can be either accessed through a Color::Calc object reference (here: $cc) or through the method names imported by import (here using the prefix color).
Returns $color as-is (but in the selected output format). This function can be used for color format conversion/normalisation.
Returns the inverse of $color.
Returns a color that is on the opposite side of the color wheel but roughly keeps the saturation and lightness.
Converts $color to greyscale.
Rounds each component to to the nearest number determined by dividing the range 0..255 into $value_count+1 portions.
The default for $value_count is 6, yielding 6^3 = 216 colors. Values that are one higher than divisors of 255 yield the best results (e.g. 3+1, 5+1, 7+1, 9+1, 15+1, 17+1, ...).
Rounds each color component to a multiple of 0x33 (dec. 51) or to a named color defined in the HTML 4.01 specification.
Historically, these colors have been known as web-safe colors. They still provide a convenient color palette.
Returns a color that is the mixture of $color1 and $color2.
The optional $alpha parameter can be a value between 0.0 (use $color1 only) and 1.0 (use $color2 only), the default is 0.5.
Returns a lighter version of $color, i.e. returns mix($color,[255,255,255],$alpha).
The optional $alpha parameter can be a value between 0.0 (use $color only) and 1.0 (use [255,255,255] only), the default is 0.5.
Returns a darker version of $color, i.e. returns mix($color,[0,0,0],$alpha).
The optional $alpha parameter can be a value between 0.0 (use $color only) and 1.0 (use [0,0,0] only), the default is 0.5.
Returns a color that has the highest possible contrast to the input color.
This is done by setting the red, green, and blue values to 0 if the corresponding value in the input is above ($cut * 255) and to 255 otherwise.
($cut * 255)
The default for $cut is .5, representing a cutoff between 127 and 128.
Returns black or white, whichever has the higher contrast to $color.
This is done by returning black if the grey value of $color is above ($cut * 255) and white otherwise.
Returns a color that blends into the background, i.e. it returns mix($color,contrast($color),$alpha).
The optional $alpha parameter can be a value between 0.0 (use $color only) and 1.0 (use contrast($color) only), the default is 0.5.
The idea is that $color is the foreground color, so contrast($color) is similar to the background color. Mixing them returns a color somewhere between them.
You might want to use mix($color, $background, $alpha) instead if you know the real background color.
mix($color, $background, $alpha)
Returns a mix of $color and black or white, whichever has the higher contrast to $color.
The optional $alpha parameter can be a value between 0.0 (use $color only) and 1.0 (use black/white only), the default is 0.5.
The calculation methods are also available as functions. The output format is selected through the function name.
These functions are deprecated as they do not allow selecting the scheme of recognized color names, which defaults to Graphics::ColorNames::X (and is incompatible with HTML's color names).
By default, i.e. when no list is specified with use or import, all of these functions are exported.
Use $Color::Calc::MODE as the output format. This is the default.
Use hex as the output format.
Use html as the output format. Please note that the color names recognized are still based on X's color names, which are incompatible with HTML. You can't use the output of these functions as input for other color_*_html functions.
See Color::Calc::WWW for an alternative that does not suffer from this problem.
Use pdf as the output format.
Use object as the output format.
Graphics::ColorNames (required); Graphics::ColorObject (optional)
Claus Färber <CFAERBER@cpan.org>
Copyright 2004-2010 Claus Färber. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Color::Calc, copy and paste the appropriate command in to your terminal.
perl -MCPAN -e shell
For more information on module installation, please visit the detailed CPAN module installation guide.