NAME

Color::Calc - Simple calculations with RGB colors.

SYNOPSIS

use Color::Calc ();
my $background = 'green';
print 'background: ',$background,';';
print 'border-top: solid 1px #',Color::Calc::light_hex($background),';';
print 'border-bottom: solid 1px #',Color::Calc::dark_hex($background),';';
print 'color: #',contrast_hex($background),';';

DESCRIPTION

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 Input Formats

All of the functions accept colors as parameters 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 string containing a hexadecimal RGB value in the form of #RRGGBB or RRGGBB.

  • A color name accepted by Graphics::ColorNames.

  • A Color::Object reference.

Instead of an arrayref, you can also pass a list with three values for functions that take a single parameter.

Color Output Formats

Each of the functions exists in different variants that provide different output formats:

  • function

    Returns a list of three values in the range 0..255.

  • function_hex

    Returns a hexadecimal RGB value in the format RRGGBB.

  • function_html

    Returns a value compatible with W3C's HTML and CSS specifications, typically #RRGGBB.

Accessing the Functions

The functions can be accessed in several ways:

  • The functions are exported by default with a prefix of color_:

    use Color::Calc;
    print color_dark_hex('#FFFFFF');	# returns '808080';
  • If you prefer not to import the functions, you can call them by specifying the package name:

    use Color::Calc ();
    print Color::Calc::dark_hex('#FFFFFF'); # returns '808080';

Note that function and color_function are fully equivalent except that only color_function is exported by default.

Available Functions

The following functions are available:

  • get($color)

    Returns the color as-is (but in the format specified). This function is mostly used internally, but it's available for completeness.

  • mix($color1, $color2, $alpha)

    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.

  • contrast($color)

    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 >= 128 and to 255 otherwise.

  • blend($color,$alpha)

    Returns a color that blends into the background by using less contrast, i.e. it returns mix($color,contrast($color),$alpha).

    The $alpha parameter is optional.

  • invert($color)

    Returns the inverse color.

  • light($color,$alpha)

    Returns a lighter version of $color, i.e. returns mix($color,'FFFFFF',$alpha);

    The $alpha parameter is optional.

  • dark($color)

    Returns a darker version of $color, i.e. returns mix($color,'000000',$alpha);

  • grey($color)

    Converts $color to greyscale.

  • contrast_bw($color)

    Returns black or white, whichever has the higher contrast to $color.

  • blend_bw($color,$alpha)

    Returns a mix of $color and black or white, whichever has the higher contrast to $color.

    sub blend_bw { return mix($_[0],[contrast_bw($_[0])],$_[1]); }

SEE ALSO

Graphics::ColorNames Color::Object

AUTHOR

Claus A. Färber <perl@faerber.muc.de>

COPYRIGHT

Copyright © 2003 Claus A. Färber All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 305:

Non-ASCII character seen before =encoding in 'Färber'. Assuming CP1252