Imager::Color(3pm) | User Contributed Perl Documentation | Imager::Color(3pm) |
Imager::Color - Color handling for Imager.
use Imager; $color = Imager::Color->new($red, $green, $blue); $color = Imager::Color->new($red, $green, $blue, $alpha); $color = Imager::Color->new("#C0C0FF"); # html color specification $color->set($red, $green, $blue); $color->set($red, $green, $blue, $alpha); $color->set("#C0C0FF"); # html color specification ($red, $green, $blue, $alpha) = $color->rgba(); @hsv = $color->hsv(); $color->info(); if ($color->equals(other=>$other_color)) { ... }
This module handles creating color objects used by Imager. The idea is that in the future this module will be able to handle color space calculations as well.
An Imager color consists of up to four components, each in the range 0 to 255. Unfortunately the meaning of the components can change depending on the type of image you're dealing with:
An alpha value of zero is fully transparent, an alpha value of 255 is fully opaque.
Compares all four channels unless "ignore_alpha" is set. If "ignore_alpha" is set only the first three channels are compared.
You can specify colors in several different ways, you can just supply simple values:
rgb(50% 50% 100%) rgb(0, 0, 255) rgb(0.5 0.5 1.0 / 0.8) rgb(50%, 50%, 100%, 80%)
Samples from percentages or decimals are rounded up per CSS Color 3 and 4.
This accepts some colors not accepted by the CSS rgb() specification, this may change.
You can supply named parameters:
# all of the following are equivalent my $c1 = Imager::Color->new(red=>100, blue=>255, green=>0); my $c2 = Imager::Color->new(r=>100, b=>255, g=>0); my $c3 = Imager::Color->new(r=>100, blue=>255, g=>0);
# the same as RGB(127,255,127) my $c1 = Imager::Color->new(hue=>120, v=>1, s=>0.5); my $c1 = Imager::Color->new(hue=>120, value=>1, saturation=>0.5);
my $c1 = Imager::Color->new(web=>'#FFC0C0'); # pale red
# exactly the same my $c1 = Imager::Color->new(gray=>128); my $c1 = Imager::Color->new(grey=>128);
# the same my $c1 = Imager::Color->new(rgb=>[255, 100, 0]); my $c1 = Imager::Color->new(r=>255, g=>100, b=>0);
# the same my $c1 = Imager::Color->new(hsv=>[120, 0.5, 1]); my $c1 = Imager::Color->new(hue=>120, v=>1, s=>0.5);
my $c1 = Imager::Color->new(gimp=>'snow'); my $c1 = Imager::Color->new(gimp=>'snow', palette=>'testimg/test_gimp_pal);
my $c1 = Imager::Color->new(xname=>'blue') # usually RGB(0, 0, 255)
my $c1 = Imager::Color->new(builtin=>'black') # always RGB(0, 0, 0)
Optionally you can add an alpha channel to a color with the 'alpha' or 'a' parameter.
These color specifications can be used for both constructing new colors with the new() method and modifying existing colors with the set() method.
my($h, $s, $v, $alpha) = $color->hsv();
Returns the color as a Hue/Saturation/Value/Alpha tuple.
If the alpha is not full coverage (255) it will be rounded if the result of converting the color back to an 8 bit color would return the same alpha, eg. if the color alpha is 128, it will be formatted as 0.5, not as the more precise 50.2%.
Arnar M. Hrafnkelsson, addi@umich.edu And a great deal of help from others - see the "README" for a complete list.
Imager(3), Imager::Color http://imager.perl.org/
2023-01-11 | perl v5.36.0 |