|
Graphics::ColorUtils - Easy-to-use color space conversions and more. |
Graphics::ColorUtils - Easy-to-use color space conversions and more.
use Graphics::ColorUtils;
( $y, $i, $q ) = rgb2yiq( $r, $g, $b ); ( $r, $g, $b ) = yiq2rgb( $y, $i, $q ); $hex_string = yiq2rgb( $y, $i, $q );
( $c, $m, $y ) = rgb2cmy( $r, $g, $b ); ( $r, $g, $b ) = cmy2rgb( $c, $m, $y ); $hex_string = cmy2rgb( $c, $m, $y );
( $h, $l, $s ) = rgb2hls( $r, $g, $b ); ( $r, $g, $b ) = hls2rgb( $h, $l, $s ); $hex_string = hls2rgb( $h, $l, $s );
( $h, $s, $v ) = rgb2hsv( $r, $g, $b ); ( $r, $g, $b ) = hsv2rgb( $h, $s, $v ); $hex_string = hsv2rgb( $h, $s, $v );
# -----
use Graphics::ColorUtils qw( :gradients );
( $r, $g, $b ) = grad2rgb( $name, $f ); # where 0.0 <= $f < 1.0 $hex_string = grad2rgb( $name, $f );
%color_count_for_gradient_name = available_gradients(); $array_ref_of_rgb_triples = gradient( $name ); $array_ref_old_grad = register_gradient( $name, $array_ref_of_rgb_triples );
# -----
use Graphics::ColorUtils qw( :names );
( $r, $g, $b ) = name2rgb( $name ); $hex_string = name2rgb( $name );
$hash_ref_rgb_triples_for_name = available_names(); ( $old_r, $old_g, $old_b ) = register_name( $name, $r, $g, $b ); $old_hex_string = register_name( $name, $r, $g, $b ); $default_ns = get_default_namespace(); $old_ns = set_default_namespace( $new_ns );
This modules provides some utility functions to handle colors and color space conversions.
The interface has been kept simple, so that most functions can be called
``inline'' when making calls to graphics libraries such as GD, Tk, or
when generating HTML/CSS. (E.g. for GD:
$c = $img->colorAllocate( hsv2rgb( 270, 0.5, 0.3 ) );.)
Features:
Legal values:
Y, I, Q: 0..1 C, M, Y: 0..1
R, G, B: 0..255 (may be float on input, guaranteed int on output)
H: 0..360 (red=0->yellow->green=120->cyan->blue=240->magenta steps of 60) S, V: 0..1 L, S: 0..1
All ...2rgb functions return a three-element array in list context,
and a string formatted according to "#%02x%02x%02x" (e.g. '#ff3a18')
in scalar context.
rgb2yiq( $r, $g, $b ) and yiq2rgb( $y, $i, $q)
rgb2cmy( $r, $g, $b ) and cmy2rgb( $c, $m, $y)
rgb2hsv( $r, $g, $b ) and hsv2rgb( $h, $s, $v)
rgb2hls( $r, $g, $b ) and hls2rgb( $h, $l, $s)
All these methods take a triple of values and return a triple of
converted values. However, in scalar context the ...2rgb
methods return a string formatted according to "#%02x%02x%02x"
(e.g. '#ff3a18'). This format is appropriate e.g. for calls to
Tk routines: $mw->widget( -color = hls2rgb( 180, 0.2, 0.1 ) );>, etc.
Names can be arbitrary strings. If names contain a colon (':'),
the part of the name before the colon is considered a ``namespace''
specification. Namespaces allow to have multiple color values
corresponding to the same name and to control the priority in
which those values will be retrieved.
name2rgb( $name )( $r, $g, $b ) in list context or a a hex-string
in scalar context if the name has been found, undef otherwise.
The name is normalized before lookup is attempted. Normalization consists of: lowercasing and elimination of whitespace. Also, ``gray'' is replaced with ``grey''.
If the name is prefixed with a namespace (separated by colon a ':'),
only this namespace is searched. If no namespace is specified, then
the lookup occurs first in the global namespace, then in the default
namespace.
available_names()register_name( $name, $r, $g, $b )If the name is not prefixed by a namespace, the color will be entered into the global namespace.
Returns the old value for the name, if the name already exists,
undef otherwise.
get_default_namespace()'' corresponds to the global namespace.
set_default_namespace()Giving an empty string as argument makes the global namespace the default. Note that the global namespace is initially empty.
(On startup, the default namespace is 'x11'.)
grad2rgb( $name, $f )$f.
Returns undef when gradient not found or $f outside valid range.
available_gradients()gradient( $name )undef if the gradient is not found.
register_gradient( $name, $array_ref )undef otherwise.
An introduction, together with a large number of sample gradients can be found at Paul Bourke's webpage: http://local.wasp.uwa.edu.au/~pbourke/texture_colour/colourramp/
Exports by default:
rgb2yiq(), yiq2rgb() rgb2cmy(), cmy2rgb() rgb2hls(), hls2rgb() rgb2hsv(), hsv2rgb()
Using the export tag :names, exports the following additional methods:
name2rgb() available_names() register_name() set_default_namespace() get_default_namespace()
Using the export tag :gradients, exports the following additional methods:
gradient() grad2rgb() available_gradients() register_gradient()
hsv2rgb() and
hls2rgb() tolerate ``moderate'' violation of this constraint (up
to +/- 359).
A comprehensive reference. Beware of typos in the algorithms!
A textbook based on the previous title. Possibly more accessible and available.
Another textbook.
Philipp K. Janert, <janert at ieee dot org >, http://www.beyondcode.org
Copyright (C) 2006 by Philipp K. Janert
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.3 or, at your option, any later version of Perl 5 you may have available.
|
Graphics::ColorUtils - Easy-to-use color space conversions and more. |