7 # this needs to be kept in sync with the array of hatches in fills.c
9 qw/check1x1 check2x2 check4x4 vline1 vline2 vline4
10 hline1 hline2 hline4 slash1 slosh1 slash2 slosh2
11 grid1 grid2 grid4 dots1 dots4 dots16 stipple weave cross1 cross2
12 vlozenge hlozenge scalesdown scalesup scalesleft scalesright stipple2
15 @hatch_types{@hatch_types} = 0..$#hatch_types;
18 qw/none normal multiply dissolve add subtract diff lighten darken
19 hue saturation value color/;
21 @combine_types{@combine_types} = 0 .. $#combine_types;
22 $combine_types{mult} = $combine_types{multiply};
23 $combine_types{'sub'} = $combine_types{subtract};
24 $combine_types{sat} = $combine_types{saturation};
26 *_color = \&Imager::_color;
29 my ($class, %hsh) = @_;
31 my $self = bless { }, $class;
33 if (exists $combine_types{$hsh{combine}}) {
34 $hsh{combine} = $combine_types{$hsh{combine}};
37 my $solid = _color($hsh{solid});
38 if (UNIVERSAL::isa($solid, 'Imager::Color')) {
40 Imager::i_new_fill_solid($solid, $hsh{combine});
42 elsif (UNIVERSAL::isa($solid, 'Imager::Color::Float')) {
44 Imager::i_new_fill_solidf($solid, $hsh{combine});
47 $Imager::ERRSTR = "solid isn't a color";
51 elsif (defined $hsh{hatch}) {
54 $hsh{fg} ||= Imager::Color->new(0, 0, 0);
55 if (ref $hsh{hatch}) {
56 $hsh{cust_hatch} = pack("C8", @{$hsh{hatch}});
59 elsif ($hsh{hatch} =~ /\D/) {
60 unless (exists($hatch_types{$hsh{hatch}})) {
61 $Imager::ERRSTR = "Unknown hatch type $hsh{hatch}";
64 $hsh{hatch} = $hatch_types{$hsh{hatch}};
66 my $fg = _color($hsh{fg});
67 if (UNIVERSAL::isa($fg, 'Imager::Color')) {
68 my $bg = _color($hsh{bg} || Imager::Color->new(255, 255, 255));
70 Imager::i_new_fill_hatch($fg, $bg, $hsh{combine},
71 $hsh{hatch}, $hsh{cust_hatch},
74 elsif (UNIVERSAL::isa($fg, 'Imager::Color::Float')) {
75 my $bg = _color($hsh{bg} || Imager::Color::Float->new(1, 1, 1));
77 Imager::i_new_fill_hatchf($fg, $bg, $hsh{combine},
78 $hsh{hatch}, $hsh{cust_hatch},
82 $Imager::ERRSTR = "fg isn't a color";
86 elsif (defined $hsh{fountain}) {
87 # make sure we track the filter's defaults
88 my $fount = $Imager::filters{fountain};
89 my $def = $fount->{defaults};
90 my $names = $fount->{names};
92 $hsh{ftype} = $hsh{fountain};
93 # process names of values
94 for my $name (keys %$names) {
95 if (defined $hsh{$name} && exists $names->{$name}{$hsh{$name}}) {
96 $hsh{$name} = $names->{$name}{$hsh{$name}};
100 %hsh = (%$def, %hsh);
101 my @parms = @{$fount->{callseq}};
103 for my $name (@parms) {
104 unless (defined $hsh{$name}) {
106 "required parameter '$name' not set for fountain fill";
111 # check that the segments supplied is an array ref
112 unless (ref $hsh{segments} && $hsh{segments} =~ /ARRAY/) {
114 "segments must be an array reference or Imager::Fountain object";
118 # make sure the segments are specified with colors
120 for my $segment (@{$hsh{segments}}) {
121 my @new_segment = @$segment;
123 $_ = _color($_) or return for @new_segment[3,4];
124 push @segments, \@new_segment;
128 Imager::i_new_fill_fount($hsh{xa}, $hsh{ya}, $hsh{xb}, $hsh{yb},
129 $hsh{ftype}, $hsh{repeat}, $hsh{combine}, $hsh{super_sample},
130 $hsh{ssample_param}, \@segments);
132 elsif (defined $hsh{image}) {
136 Imager::i_new_fill_image($hsh{image}{IMG}, $hsh{matrix}, $hsh{xoff},
137 $hsh{yoff}, $hsh{combine});
138 $self->{DEPS} = [ $hsh{image}{IMG} ];
141 $Imager::ERRSTR = "No fill type specified";
142 warn "No fill type!";
154 return @combine_types;
161 Imager::Fill - general fill types
165 my $fill1 = Imager::Fill->new(solid=>$color, combine=>$combine);
166 my $fill2 = Imager::Fill->new(hatch=>'vline2', fg=>$color1, bg=>$color2,
168 my $fill3 = Imager::Fill->new(fountain=>$type, ...);
169 my $fill4 = Imager::Fill->new(image=>$img, ...);
173 Creates fill objects for use by some drawing functions, currently just
174 the Imager box() method.
176 The currently available fills are:
190 fountain (similar to gradients in paint software)
194 =head1 Common options
200 The way in which the fill data is combined with the underlying image,
201 possible values include:
207 The fill pixel replaces the target pixel.
211 The fill pixels alpha value is used to combine it with the target pixel.
217 Each channel of fill and target is multiplied, and the result is
218 combined using the alpha channel of the fill pixel.
222 If the alpha of the fill pixel is greater than a random number, the
223 fill pixel is alpha combined with the target pixel.
227 The channels of the fill and target are added together, clamped to the range of the samples and alpha combined with the target.
231 The channels of the fill are subtracted from the target, clamped to be
232 >= 0, and alpha combined with the target.
236 The channels of the fill are subtracted from the target and the
237 absolute value taken this is alpha combined with the target.
241 The higher value is taken from each channel of the fill and target
242 pixels, which is then alpha combined with the target.
246 The higher value is taken from each channel of the fill and target
247 pixels, which is then alpha combined with the target.
251 The combination of the saturation and value of the target is combined
252 with the hue of the fill pixel, and is then alpha combined with the
257 The combination of the hue and value of the target is combined
258 with the saturation of the fill pixel, and is then alpha combined with the
263 The combination of the hue and value of the target is combined
264 with the value of the fill pixel, and is then alpha combined with the
269 The combination of the value of the target is combined with the hue
270 and saturation of the fill pixel, and is then alpha combined with the
277 In general colors can be specified as Imager::Color or
278 Imager::Color::Float objects. The fill object will typically store
279 both types and convert from one to the other. If a fill takes 2 color
280 objects they should have the same type.
284 my $fill = Imager::Fill->new(solid=>$color, $combine =>$combine)
286 Creates a solid fill, the only required parameter is C<solid> which
287 should be the color to fill with.
291 my $fill = Imager::Fill->new(hatch=>$type, fg=>$fgcolor, bg=>$bgcolor,
294 Creates a hatched fill. You can specify the following keywords:
300 The type of hatch to perform, this can either be the numeric index of
301 the hatch (not recommended), the symbolic name of the hatch, or an
302 array of 8 integers which specify the pattern of the hatch.
304 Hatches are represented as cells 8x8 arrays of bits, which limits their
307 Current hatch names are:
311 =item check1x1, check2x2, check4x4
313 checkerboards at varios sizes
315 =item vline1, vline2, vline4
317 1, 2, or 4 vertical lines per cell
319 =item hline1, hline2, hline4
321 1, 2, or 4 horizontal lines per cell
325 1 or 2 / lines per cell.
329 1 or 2 \ lines per cell
331 =item grid1, grid2, grid4
333 1, 2, or 4 vertical and horizontal lines per cell
335 =item dots1, dots4, dots16
337 1, 4 or 16 dots per cell
339 =item stipple, stipple2
345 I hope this one is obvious.
349 2 densities of crosshatch
351 =item vlozenge, hlozenge
353 something like lozenge tiles
355 =item scalesdown, scalesup, scalesleft, scalesright
357 Vaguely like fish scales in each direction.
369 The fg color is rendered where bits are set in the hatch, and the bg
370 where they are clear. If you use a transparent fg or bg, and set
371 combine, you can overlay the hatch onto an existing image.
373 fg defaults to black, bg to white.
379 An offset into the hatch cell. Both default to zero.
383 You can call Imager::Fill->hatches for a list of hatch names.
385 =head2 Fountain fills
387 my $fill = Imager::Fill->new(fountain=>$ftype,
388 xa=>$xa, ya=>$ya, xb=>$xb, yb=>$yb,
389 segment=>$segments, repeat=>$repeat, combine=>$combine,
390 super_sample=>$super_sample, ssample_param=>$ssample_param);
392 This fills the given region with a fountain fill. This is exactly the
393 same fill as the C<fountain> filter, but is restricted to the shape
394 you are drawing, and the fountain parameter supplies the fill type,
399 my $fill = Imager::Fill->new(image=>$src, xoff=>$xoff, yoff=>$yoff,
400 matrix=>$matrix, $combine);
402 Fills the given image with a tiled version of the given image. The
403 first non-zero value of xoff or yoff will provide an offset along the
404 given axis between rows or columns of tiles respectively.
406 The matrix parameter performs a co-ordinate transformation from the
407 co-ordinates in the target image to the fill image co-ordinates.
408 Linear interpolation is used to determine the fill pixel. You can use
409 the L<Imager::Matrix2d> class to create transformation matrices.
411 The matrix parameter will significantly slow down the fill.
417 =item Imager::Fill->hatches
419 A list of all defined hatch names.
421 =item Imager::Fill->combines
423 A list of all combine types.
429 I'm planning on adding the following types of fills:
435 combines 2 other fills in a checkerboard
439 combines 2 other fills using the levels of an image
443 uses the transform2() register machine to create fills
449 Tony Cook <tony@develop-help.com>