Color::Calc - Simple calculations with RGB colors.
use Color::Calc (); my $background = 'green'; print 'background: ',Color::Calc::color_html($background),';'; print 'border-top: solid 1px ',Color::Calc::light_html($background),';'; print 'border-bottom: solid 1px ',Color::Calc::dark_html($background),';'; print 'color: ',contrast_bw_html($background),';';
The Color::Calc module implements simple calculations with RGB colors. This can be used to create a full color scheme from a few colors.
Color::Calc
There are three methods to use the calculation functions: You can create an object, import customised functions into your namespace, or you can access them as class methods.
The object-oriented interface can be used to access the calculation functions through an object reference:
use Color::Calc(); my $cc = new Color::Calc( 'ColorScheme' => 'X', OutputFormat => 'HTML' ); print $cc->invert( 'white' );
$cc = new Color::Calc( 'ColorScheme' => $name, 'OutputFormat' => $format );
Creates a new Color::Calc object. The constructor can be passed the following options:
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.
Graphics::ColorNames
X
HTML
Default: X (Note: This is incompatible with HTML color names).
One of the output formats defined by this module. See below for possible values.
Default: __MODEvar
__MODEvar
You can also choose to import customised funtions into your namespace:
use Color::Calc( 'ColorScheme' => 'X', 'OutputFormat' => 'HTML', 'Prefix' => 'cc' ); print cc_invert( 'white' ); # prints 'black'
On import, you can specify the following parameters:
See above.
Adds a prefix to the front of the method names. You can call the methods as prefix_method_name (the specified value plus an underscore plus the method name). You can also call prefix instead of prefix_get.
_get
Default: color
color
Please note that specifying an empty list of parameters means "don't import anything". Omit the list to import the default functions.
You can also use the following modules to import the function with pre-defined parameters.
use
use Color::Calc::WWW
Same as use Color::Calc( ColorScheme = 'WWW', OutputFormat => 'html' )>
use Color::Calc( ColorScheme =
use Color::Calc::html
(DEPRECATED) Same as use Color::Calc( OutputFormat = 'html')>
use Color::Calc( OutputFormat =
Please note that this only selects HTML as the OutputFormat but not as the ColorScheme (which defaults to 'X'), which is probably not what you expect. Use Color::Calc::WWW instead.
OutputFormat
ColorScheme
Color::Calc::WWW
use Color::Calc::hex
Same as use Color::Calc( OutputFormat = 'hex')>
use Color::Calc::object
Same as use Color::Calc( OutputFormat = 'object')>
use Color::Calc::pdf
Same as use Color::Calc( OutputFormat = 'pdf')>
use Color::Calc::tuple
Same as use Color::Calc( OutputFormat = 'tuple')>
(DEPRECATED) You can also access the methods as class methods of the various packages with and without the color prefix:
use Color::Calc::WWW(); print Color::Calc::WWW::color('FFF'); # prints 'white' print Color::Calc::WWW::color_invert('FFF'); # prints 'black' print Color::Calc::html::invert('FFF'); # prints 'black'
For the main module Color::Calc, you can also add a suffix _output_format to select the output format:
_output_format
use Color::Calc(); print Color::Calc::color_html('FFF'); # prints 'white' print Color::Calc::color_invert_html('FFF'); # prints 'black' print Color::Calc::invert_hex('FFF'); # prints '000000'
The module supports different color formats for in- and output.
As a general rule, all input formats are accepted at any time regardless of the output format specified by OutputFormat. This also means that function calls can be nested easily:
print color_invert(color_invert('blue'));
Note: Nesting does not work as expected if you specify html as the OutputFormat and a ColorScheme incompatible with HTML (e.g. X). (This may change in future versions.)
html
(Note: The module internally only works with color values in the integer range 0..255, i.e. 8 bit precision. This may change in future versions.)
The module accepts color values in the following formats:
An arrayref pointing to an array with three elements in the range 0..255 corresponding to the red, green, and blue component.
0
255
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)).
('0128',128,128)
A string containing a hexadecimal RGB value like #RGB/#RRGGBB/#RRRGGGBBB/..., or RGB/RRGGBB/RRRGGGBBB/...
#RGB
#RRGGBB
#RRRGGGBBB
RGB
RRGGBB
RRRGGGBBB
A color name accepted by Graphics::ColorNames. The interpretation is controlled by the ColorScheme parameter.
A Graphics::ColorObject reference.
Graphics::ColorObject
Color::Calc can return colors in the following formats
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.
length
Returns a hexadecimal RGB value as a string in the format RRGGBB.
(Note: future versions may return an object that mimics a string instead.)
Returns a string compatible with W3C's HTML and CSS specifications, i.e. #RRGGBB or one of the sixteen HTML color names.
Returns a Graphics::ColorObject reference. The module Graphics::ColorObject must be installed.
Returns a string compatible with PDF::API2, i.e. #RRGGBB.
PDF::API2
(DEPRECATED) Uses the value of $Color::Calc::MODE to select one of the above output formats. You should use local when setting this variable.
$Color::Calc::MODE
local
The module supports the following calculation functions, which can be accessed through one of the methods described above:
Returns $color as-is (but in the selected output format). This function can be used for color format conversion/normalisation.
$color
Returns the inverse of $color.
Converts $color to greyscale.
Returns a color that is the mixture of $color1 and $color2.
$color1
$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.
$alpha
Returns a lighter version of $color, i.e. returns mix($color,[255,255,255],$alpha).
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).
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.
$cut
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).
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.
contrast($color)
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.
Graphics::ColorNames (required);
Graphics::ColorObject (optional)
Claus Färber <cfaerber@cpan.org>
Copyright © 2004-2006 Claus Färber
This module is free software; you can redistribute it and/or modify it under the terms of either the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or the "Artistic License".
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
3 POD Errors
The following errors were encountered while parsing the POD:
You forgot a '=back' before '=head3'
Non-ASCII character seen before =encoding in '8 bit'. Assuming CP1252
'=item' outside of any '=over'
To install Color::Calc, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Color::Calc
CPAN shell
perl -MCPAN -e shell install Color::Calc
For more information on module installation, please visit the detailed CPAN module installation guide.