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.

Using the calculation functions
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. Object-oriented interface 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". 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" Importing customised functions 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. 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". Default: "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. Same as "use Color::Calc( ColorScheme =" 'WWW', OutputFormat => 'html' )> (DEPRECATED) Same as "use Color::Calc( OutputFormat =" 'html')> 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. Same as "use Color::Calc( OutputFormat =" 'hex')> Same as "use Color::Calc( OutputFormat =" 'object')> Same as "use Color::Calc( OutputFormat =" 'pdf')> Same as "use Color::Calc( OutputFormat =" 'tuple')> Calling unimported class functions (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: 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'

Color formats
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.) (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.) Input 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. 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. Output "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. 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. (Note: future versions may return an object that mimics a string instead.) Returns a "Graphics::ColorObject" reference. The module "Graphics::ColorObject" must be installed. Returns a string compatible with "PDF::API2", i.e. #RRGGBB. (Note: future versions may return an object that mimics a string instead.) (DEPRECATED) Uses the value of $Color::Calc::MODE to select one of the above output formats. You should use "local" when setting this variable.

Calculation functions
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. Returns the inverse of $color. Converts $color to greyscale. 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. 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. The default for $cut is .5, representing a cutoff between 127 and 128. 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. 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.