3 # this needs to be kept in sync with the array of hatches in fills.c
5 qw/check1x1 check2x2 check4x4 vline1 vline2 vline4
6 hline1 hline2 hline4 slash1 slosh1 slash2 slosh2
7 grid1 grid2 grid4 dots1 dots4 dots16 stipple weave cross1 cross2
8 vlozenge hlozenge scalesdown scalesup scalesleft scalesright stipple2
11 @hatch_types{@hatch_types} = 0..$#hatch_types;
14 qw/none normal multiply dissolve add subtract diff lighten darken
15 hue saturation value color/;
17 @combine_types{@combine_types} = 0 .. $#combine_types;
18 $combine_types{mult} = $combine_types{multiply};
19 $combine_types{'sub'} = $combine_types{subtract};
20 $combine_types{sat} = $combine_types{saturation};
22 # this function tries to DWIM for color parameters
23 # color objects are used as is
24 # simple scalars are simply treated as single parameters to Imager::Color->new
25 # hashrefs are treated as named argument lists to Imager::Color->new
26 # arrayrefs are treated as list arguments to Imager::Color->new iff any
28 # other arrayrefs are treated as list arguments to Imager::Color::Float
35 if (UNIVERSAL::isa($arg, "Imager::Color")
36 || UNIVERSAL::isa($arg, "Imager::Color::Float")) {
40 if ($arg =~ /^HASH\(/) {
41 $result = Imager::Color->new(%$arg);
43 elsif ($arg =~ /^ARRAY\(/) {
44 if (grep $_ > 1, @$arg) {
45 $result = Imager::Color->new(@$arg);
48 $result = Imager::Color::Float->new(@$arg);
52 $Imager::ERRSTR = "Not a color";
57 # assume Imager::Color::new knows how to handle it
58 $result = Imager::Color->new($arg);
65 my ($class, %hsh) = @_;
67 my $self = bless { }, $class;
69 if (exists $combine_types{$hsh{combine}}) {
70 $hsh{combine} = $combine_types{$hsh{combine}};
73 my $solid = _color($hsh{solid});
74 if (UNIVERSAL::isa($solid, 'Imager::Color')) {
76 Imager::i_new_fill_solid($solid, $hsh{combine});
78 elsif (UNIVERSAL::isa($solid, 'Imager::Color::Float')) {
80 Imager::i_new_fill_solidf($solid, $hsh{combine});
83 $Imager::ERRSTR = "solid isn't a color";
87 elsif (defined $hsh{hatch}) {
90 $hsh{fg} ||= Imager::Color->new(0, 0, 0);
91 if (ref $hsh{hatch}) {
92 $hsh{cust_hatch} = pack("C8", @{$hsh{hatch}});
95 elsif ($hsh{hatch} =~ /\D/) {
96 unless (exists($hatch_types{$hsh{hatch}})) {
97 $Imager::ERRSTR = "Unknown hatch type $hsh{hatch}";
100 $hsh{hatch} = $hatch_types{$hsh{hatch}};
102 my $fg = _color($hsh{fg});
103 if (UNIVERSAL::isa($fg, 'Imager::Color')) {
104 my $bg = _color($hsh{bg} || Imager::Color->new(255, 255, 255));
106 Imager::i_new_fill_hatch($fg, $bg, $hsh{combine},
107 $hsh{hatch}, $hsh{cust_hatch},
110 elsif (UNIVERSAL::isa($fg, 'Imager::Color::Float')) {
111 my $bg = _color($hsh{bg} || Imager::Color::Float->new(1, 1, 1));
113 Imager::i_new_fill_hatchf($fg, $bg, $hsh{combine},
114 $hsh{hatch}, $hsh{cust_hatch},
118 $Imager::ERRSTR = "fg isn't a color";
122 elsif (defined $hsh{fountain}) {
123 # make sure we track the filter's defaults
124 my $fount = $Imager::filters{fountain};
125 my $def = $fount->{defaults};
126 my $names = $fount->{names};
128 $hsh{ftype} = $hsh{fountain};
129 # process names of values
130 for my $name (keys %$names) {
131 if (defined $hsh{$name} && exists $names->{$name}{$hsh{$name}}) {
132 $hsh{$name} = $names->{$name}{$hsh{$name}};
136 %hsh = (%$def, %hsh);
137 my @parms = @{$fount->{callseq}};
139 for my $name (@parms) {
140 unless (defined $hsh{$name}) {
142 "required parameter '$name' not set for fountain fill";
148 Imager::i_new_fill_fount($hsh{xa}, $hsh{ya}, $hsh{xb}, $hsh{yb},
149 $hsh{ftype}, $hsh{repeat}, $hsh{combine}, $hsh{super_sample},
150 $hsh{ssample_param}, $hsh{segments});
152 elsif (defined $hsh{image}) {
156 Imager::i_new_fill_image($hsh{image}{IMG}, $hsh{matrix}, $hsh{xoff},
157 $hsh{yoff}, $hsh{combine});
158 $self->{DEPS} = [ $hsh{image}{IMG} ];
161 $Imager::ERRSTR = "No fill type specified";
162 warn "No fill type!";
174 return @combine_types;
181 Imager::Fill - general fill types
185 my $fill1 = Imager::Fill->new(solid=>$color, combine=>$combine);
186 my $fill2 = Imager::Fill->new(hatch=>'vline2', fg=>$color1, bg=>$color2,
188 my $fill3 = Imager::Fill->new(fountain=>$type, ...);
189 my $fill4 = Imager::Fill->new(image=>$img, ...);
193 Creates fill objects for use by some drawing functions, currently just
194 the Imager box() method.
196 The currently available fills are:
210 fountain (similar to gradients in paint software)
214 =head1 Common options
220 The way in which the fill data is combined with the underlying image,
221 possible values include:
227 The fill pixel replaces the target pixel.
231 The fill pixels alpha value is used to combine it with the target pixel.
237 Each channel of fill and target is multiplied, and the result is
238 combined using the alpha channel of the fill pixel.
242 If the alpha of the fill pixel is greater than a random number, the
243 fill pixel is alpha combined with the target pixel.
247 The channels of the fill and target are added together, clamped to the range of the samples and alpha combined with the target.
251 The channels of the fill are subtracted from the target, clamped to be
252 >= 0, and alpha combined with the target.
256 The channels of the fill are subtracted from the target and the
257 absolute value taken this is alpha combined with the target.
261 The higher value is taken from each channel of the fill and target pixels, which is then alpha combined with the target.
265 The higher value is taken from each channel of the fill and target pixels, which is then alpha combined with the target.
269 The combination of the saturation and value of the target is combined
270 with the hue of the fill pixel, and is then alpha combined with the
275 The combination of the hue and value of the target is combined
276 with the saturation of the fill pixel, and is then alpha combined with the
281 The combination of the hue and value of the target is combined
282 with the value of the fill pixel, and is then alpha combined with the
287 The combination of the value of the target is combined with the hue
288 and saturation of the fill pixel, and is then alpha combined with the
295 In general colors can be specified as Imager::Color or
296 Imager::Color::Float objects. The fill object will typically store
297 both types and convert from one to the other. If a fill takes 2 color
298 objects they should have the same type.
302 my $fill = Imager::Fill->new(solid=>$color, $combine =>$combine)
304 Creates a solid fill, the only required parameter is C<solid> which
305 should be the color to fill with.
309 my $fill = Imager::Fill->new(hatch=>$type, fg=>$fgcolor, bg=>$bgcolor,
312 Creates a hatched fill. You can specify the following keywords:
318 The type of hatch to perform, this can either be the numeric index of
319 the hatch (not recommended), the symbolic name of the hatch, or an
320 array of 8 integers which specify the pattern of the hatch.
322 Hatches are represented as cells 8x8 arrays of bits, which limits their
325 Current hatch names are:
329 =item check1x1, check2x2, check4x4
331 checkerboards at varios sizes
333 =item vline1, vline2, vline4
335 1, 2, or 4 vertical lines per cell
337 =item hline1, hline2, hline4
339 1, 2, or 4 horizontal lines per cell
343 1 or 2 / lines per cell.
347 1 or 2 \ lines per cell
349 =item grid1, grid2, grid4
351 1, 2, or 4 vertical and horizontal lines per cell
353 =item dots1, dots4, dots16
355 1, 4 or 16 dots per cell
357 =item stipple, stipple2
363 I hope this one is obvious.
367 2 densities of crosshatch
369 =item vlozenge, hlozenge
371 something like lozenge tiles
373 =item scalesdown, scalesup, scalesleft, scalesright
375 Vaguely like fish scales in each direction.
387 The fg color is rendered where bits are set in the hatch, and the bg
388 where they are clear. If you use a transparent fg or bg, and set
389 combine, you can overlay the hatch onto an existing image.
391 fg defaults to black, bg to white.
397 An offset into the hatch cell. Both default to zero.
401 You can call Imager::Fill->hatches for a list of hatch names.
403 =head2 Fountain fills
405 my $fill = Imager::Fill->new(fountain=>$ftype,
406 xa=>$xa, ya=>$ya, xb=>$xb, yb=>$yb,
407 segment=>$segments, repeat=>$repeat, combine=>$combine,
408 super_sample=>$super_sample, ssample_param=>$ssample_param);
410 This fills the given region with a fountain fill. This is exactly the
411 same fill as the C<fountain> filter, but is restricted to the shape
412 you are drawing, and the fountain parameter supplies the fill type,
417 my $fill = Imager::Fill->new(image=>$src, xoff=>$xoff, yoff=>$yoff,
418 matrix=>$matrix, $combine);
420 Fills the given image with a tiled version of the given image. The
421 first non-zero value of xoff or yoff will provide an offset along the
422 given axis between rows or columns of tiles respectively.
424 The matrix parameter performs a co-ordinate transformation from the
425 co-ordinates in the target image to the fill image co-ordinates.
426 Linear interpolation is used to determine the fill pixel. You can use
427 the L<Imager::Matrix2d> class to create transformation matrices.
429 The matrix parameter will significantly slow down the fill.
435 =item Imager::Fill->hatches
437 A list of all defined hatch names.
439 =item Imager::Fill->combines
441 A list of all combine types.
447 I'm planning on adding the following types of fills:
453 combines 2 other fills in a checkerboard
457 combines 2 other fills using the levels of an image
461 uses the transform2() register machine to create fills
467 Tony Cook <tony@develop-help.com>