1Imager::Color(3) User Contributed Perl Documentation Imager::Color(3)
2
3
4
6 Imager::Color - Color handling for Imager.
7
9 use Imager;
10
11 $color = Imager::Color->new($red, $green, $blue);
12 $color = Imager::Color->new($red, $green, $blue, $alpha);
13 $color = Imager::Color->new("#C0C0FF"); # html color specification
14
15 $color->set($red, $green, $blue);
16 $color->set($red, $green, $blue, $alpha);
17 $color->set("#C0C0FF"); # html color specification
18
19 ($red, $green, $blue, $alpha) = $color->rgba();
20 @hsv = $color->hsv();
21
22 $color->info();
23
24 if ($color->equals(other=>$other_color)) {
25 ...
26 }
27
29 This module handles creating color objects used by Imager. The idea is
30 that in the future this module will be able to handle color space
31 calculations as well.
32
33 An Imager color consists of up to four components, each in the range 0
34 to 255. Unfortunately the meaning of the components can change
35 depending on the type of image you're dealing with:
36
37 • for 3 or 4 channel images the color components are red, green,
38 blue, alpha.
39
40 • for 1 or 2 channel images the color components are gray, alpha,
41 with the other two components ignored.
42
43 An alpha value of zero is fully transparent, an alpha value of 255 is
44 fully opaque.
45
47 new This creates a color object to pass to functions that need a color
48 argument.
49
50 set This changes an already defined color. Note that this does not
51 affect any places where the color has been used previously.
52
53 rgba()
54 This returns the red, green, blue and alpha channels of the color
55 the object contains.
56
57 info
58 Calling info merely dumps the relevant color to the log.
59
60 equals(other=>$other_color)
61 equals(other=>$other_color, ignore_alpha=>1)
62 Compares $self and color $other_color returning true if the color
63 components are the same.
64
65 Compares all four channels unless "ignore_alpha" is set. If
66 "ignore_alpha" is set only the first three channels are compared.
67
68 You can specify colors in several different ways, you can just supply
69 simple values:
70
71 • an Imager::Color object
72
73 • an Imager::Color::Float object, the ranges of samples are
74 translated from 0.0...1.0 to 0...255.
75
76 • simple numeric parameters - if you supply 3 or 4 numeric arguments,
77 you get a color made up of those RGB (and possibly A) components.
78
79 • a six hex digit web color, either "RRGGBB" or "#RRGGBB"
80
81 • an eight hex digit web color, either "RRGGBBAA" or "#RRGGBBAA".
82
83 • a CSS rgb() color, based on CSS Color 4. The "none" keyword is not
84 supported and numbers must be simple decimals without exponents.
85 eg.
86
87 rgb(50% 50% 100%)
88 rgb(0, 0, 255)
89 rgb(0.5 0.5 1.0 / 0.8)
90 rgb(50%, 50%, 100%, 80%)
91
92 Samples from percentages or decimals are rounded up per CSS Color 3
93 and 4.
94
95 This accepts some colors not accepted by the CSS rgb()
96 specification, this may change.
97
98 • a 3 hex digit web color, "#RGB" - a value of F becomes 255.
99
100 • a color name, from whichever of the gimp "Named_Colors" file or X
101 "rgb.txt" is found first. The same as using the "name" keyword.
102
103 You can supply named parameters:
104
105 • 'red', 'green' and 'blue', optionally shortened to 'r', 'g' and
106 'b'. The color components in the range 0 to 255.
107
108 # all of the following are equivalent
109 my $c1 = Imager::Color->new(red=>100, blue=>255, green=>0);
110 my $c2 = Imager::Color->new(r=>100, b=>255, g=>0);
111 my $c3 = Imager::Color->new(r=>100, blue=>255, g=>0);
112
113 • "hue", "saturation" and "value", optionally shortened to "h", "s"
114 and "v", to specify a HSV color. 0 <= hue < 360, 0 <= s <= 1 and 0
115 <= v <= 1.
116
117 # the same as RGB(127,255,127)
118 my $c1 = Imager::Color->new(hue=>120, v=>1, s=>0.5);
119 my $c1 = Imager::Color->new(hue=>120, value=>1, saturation=>0.5);
120
121 • "web", which can specify a 6 or 3 hex digit web color, in any of
122 the forms "#RRGGBB", "#RGB", "RRGGBB" or "RGB".
123
124 my $c1 = Imager::Color->new(web=>'#FFC0C0'); # pale red
125
126 • "gray" or "grey" which specifies a single channel, from 0 to 255.
127
128 # exactly the same
129 my $c1 = Imager::Color->new(gray=>128);
130 my $c1 = Imager::Color->new(grey=>128);
131
132 • "rgb" which takes a 3 member arrayref, containing each of the red,
133 green and blue values.
134
135 # the same
136 my $c1 = Imager::Color->new(rgb=>[255, 100, 0]);
137 my $c1 = Imager::Color->new(r=>255, g=>100, b=>0);
138
139 • "hsv" which takes a 3 member arrayref, containing each of hue,
140 saturation and value.
141
142 # the same
143 my $c1 = Imager::Color->new(hsv=>[120, 0.5, 1]);
144 my $c1 = Imager::Color->new(hue=>120, v=>1, s=>0.5);
145
146 • "gimp" which specifies a color from a GIMP palette file. You can
147 specify the file name of the palette file with the 'palette'
148 parameter, or let Imager::Color look in various places, typically
149 "$HOME/gimp-1.x/palettes/Named_Colors" with and without the version
150 number, and in "/usr/share/gimp/palettes/". The palette file must
151 have color names.
152
153 my $c1 = Imager::Color->new(gimp=>'snow');
154 my $c1 = Imager::Color->new(gimp=>'snow', palette=>'testimg/test_gimp_pal);
155
156 • "xname" which specifies a color from an X11 "rgb.txt" file. You
157 can specify the file name of the "rgb.txt" file with the "palette"
158 parameter, or let Imager::Color look in various places, typically
159 "/usr/lib/X11/rgb.txt".
160
161 my $c1 = Imager::Color->new(xname=>'blue') # usually RGB(0, 0, 255)
162
163 • "builtin" which specifies a color from the built-in color table in
164 Imager::Color::Table. The colors in this module are the same as
165 the default X11 "rgb.txt" file.
166
167 my $c1 = Imager::Color->new(builtin=>'black') # always RGB(0, 0, 0)
168
169 • "name" which specifies a name from either a GIMP palette, an X
170 "rgb.txt" file or the built-in color table, whichever is found
171 first.
172
173 • 'channel0', 'channel1', etc, each of which specifies a single
174 channel. These can be abbreviated to 'c0', 'c1' etc.
175
176 • 'channels' which takes an arrayref of the channel values.
177
178 Optionally you can add an alpha channel to a color with the 'alpha' or
179 'a' parameter.
180
181 These color specifications can be used for both constructing new colors
182 with the new() method and modifying existing colors with the set()
183 method.
184
186 hsv()
187 my($h, $s, $v, $alpha) = $color->hsv();
188
189 Returns the color as a Hue/Saturation/Value/Alpha tuple.
190
191 red
192 green
193 blue
194 alpha
195 Returns the respective component as an integer from 0 to 255.
196
197 as_float
198 Returns the color as a Imager::Color::Float object.
199
200 as_css_rgb
201 Returns the color as a CSS rgb() format color. This is always
202 returned in the byte form, eg. rgb(255 128 64).
203
204 If the alpha is not full coverage (255) it will be rounded if the
205 result of converting the color back to an 8 bit color would return
206 the same alpha, eg. if the color alpha is 128, it will be formatted
207 as 0.5, not as the more precise 50.2%.
208
210 Arnar M. Hrafnkelsson, addi@umich.edu And a great deal of help from
211 others - see the "README" for a complete list.
212
214 Imager(3), Imager::Color http://imager.perl.org/
215
216
217
218perl v5.36.0 2023-01-20 Imager::Color(3)