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.

This creates a color object to pass to functions that need a color argument.
This changes an already defined color. Note that this does not affect any places where the color has been used previously.
This returns the red, green, blue and alpha channels of the color the object contains.
Calling info merely dumps the relevant color to the log.
Compares $self and color $other_color returning true if the color components are the same.

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:

You can supply named parameters:

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.

Returns the respective component as an integer from 0 to 255.
Returns the color as a Imager::Color::Float object.
Returns the color as a CSS rgb() format color. This is always returned in the byte form, eg. rgb(255 128 64).

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/

2024-04-01 perl v5.38.2