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;
17 *_color = \&Imager::_color;
20 my ($class, %hsh) = @_;
22 my $self = bless { }, $class;
23 $hsh{combine} = Imager->_combine($hsh{combine}, 0);
25 my $solid = _color($hsh{solid});
26 if (UNIVERSAL::isa($solid, 'Imager::Color')) {
28 Imager::i_new_fill_solid($solid, $hsh{combine});
30 elsif (UNIVERSAL::isa($solid, 'Imager::Color::Float')) {
32 Imager::i_new_fill_solidf($solid, $hsh{combine});
35 $Imager::ERRSTR = "solid isn't a color";
39 elsif (defined $hsh{hatch}) {
42 $hsh{fg} ||= Imager::Color->new(0, 0, 0);
43 if (ref $hsh{hatch}) {
44 $hsh{cust_hatch} = pack("C8", @{$hsh{hatch}});
47 elsif ($hsh{hatch} =~ /\D/) {
48 unless (exists($hatch_types{$hsh{hatch}})) {
49 $Imager::ERRSTR = "Unknown hatch type $hsh{hatch}";
52 $hsh{hatch} = $hatch_types{$hsh{hatch}};
54 my $fg = _color($hsh{fg});
55 if (UNIVERSAL::isa($fg, 'Imager::Color')) {
56 my $bg = _color($hsh{bg} || Imager::Color->new(255, 255, 255));
58 Imager::i_new_fill_hatch($fg, $bg, $hsh{combine},
59 $hsh{hatch}, $hsh{cust_hatch},
62 elsif (UNIVERSAL::isa($fg, 'Imager::Color::Float')) {
63 my $bg = _color($hsh{bg} || Imager::Color::Float->new(1, 1, 1));
65 Imager::i_new_fill_hatchf($fg, $bg, $hsh{combine},
66 $hsh{hatch}, $hsh{cust_hatch},
70 $Imager::ERRSTR = "fg isn't a color";
74 elsif (defined $hsh{fountain}) {
75 # make sure we track the filter's defaults
76 my $fount = $Imager::filters{fountain};
77 my $def = $fount->{defaults};
78 my $names = $fount->{names};
80 $hsh{ftype} = $hsh{fountain};
81 # process names of values
82 for my $name (keys %$names) {
83 if (defined $hsh{$name} && exists $names->{$name}{$hsh{$name}}) {
84 $hsh{$name} = $names->{$name}{$hsh{$name}};
89 my @parms = @{$fount->{callseq}};
91 for my $name (@parms) {
92 unless (defined $hsh{$name}) {
94 "required parameter '$name' not set for fountain fill";
99 # check that the segments supplied is an array ref
100 unless (ref $hsh{segments} && $hsh{segments} =~ /ARRAY/) {
102 "segments must be an array reference or Imager::Fountain object";
106 # make sure the segments are specified with colors
108 for my $segment (@{$hsh{segments}}) {
109 my @new_segment = @$segment;
111 $_ = _color($_) or return for @new_segment[3,4];
112 push @segments, \@new_segment;
116 Imager::i_new_fill_fount($hsh{xa}, $hsh{ya}, $hsh{xb}, $hsh{yb},
117 $hsh{ftype}, $hsh{repeat}, $hsh{combine}, $hsh{super_sample},
118 $hsh{ssample_param}, \@segments);
120 elsif (defined $hsh{image}) {
124 Imager::i_new_fill_image($hsh{image}{IMG}, $hsh{matrix}, $hsh{xoff},
125 $hsh{yoff}, $hsh{combine});
126 $self->{DEPS} = [ $hsh{image}{IMG} ];
128 elsif (defined $hsh{type} && $hsh{type} eq "opacity") {
129 my $other_fill = delete $hsh{other};
130 unless (defined $other_fill) {
131 Imager->_set_error("'other' parameter required to create opacity fill");
134 unless (ref $other_fill &&
135 eval { $other_fill->isa("Imager::Fill") }) {
136 # try to auto convert to a fill object
137 if (ref $other_fill && $other_fill =~ /HASH/) {
138 $other_fill = Imager::Fill->new(%$other_fill)
144 unless ($other_fill) {
145 Imager->_set_error("'other' parameter must be an Imager::Fill object to create an opacity fill");
150 my $raw_fill = $other_fill->{fill};
151 my $opacity = delete $hsh{opacity};
152 defined $opacity or $opacity = 0.5; # some sort of default
154 Imager::i_new_fill_opacity($raw_fill, $opacity);
155 $self->{DEPS} = [ $other_fill ]; # keep reference to old fill and its deps
158 $Imager::ERRSTR = "No fill type specified";
159 warn "No fill type!";
171 return Imager->combines;
178 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, ...);
190 my $fill5 = Imager::Fill->new(type => "opacity", other => $fill,
195 Creates fill objects for use by most filled area drawing functions.
197 All fills are created with the new method.
203 my $fill = Imager::Fill->new(...);
205 The parameters depend on the type of fill being created. See below
210 The currently available fills are:
224 fountain (similar to gradients in paint software)
228 image - fill with an image, possibly transformed
232 opacity - a lower opacity version of some other fill
236 =head1 Common options
242 The way in which the fill data is combined with the underlying image.
243 See L<Imager::Draw/"Combine Types">.
247 In general colors can be specified as L<Imager::Color> or
248 L<Imager::Color::Float> objects. The fill object will typically store
249 both types and convert from one to the other. If a fill takes 2 color
250 objects they should have the same type.
254 my $fill = Imager::Fill->new(solid=>$color, combine =>$combine)
256 Creates a solid fill, the only required parameter is C<solid> which
257 should be the color to fill with.
259 A translucent red fill:
261 my $red = Imager::Fill->new(solid => "FF000080", combine => "normal");
265 my $fill = Imager::Fill->new(hatch=>$type, fg=>$fgcolor, bg=>$bgcolor,
268 Creates a hatched fill. You can specify the following keywords:
274 C<hatch> - The type of hatch to perform, this can either be the
275 numeric index of the hatch (not recommended), the symbolic name of the
276 hatch, or an array of 8 integers which specify the pattern of the
279 Hatches are represented as cells 8x8 arrays of bits, which limits their
282 Current hatch names are:
288 C<check1x1>, C<check2x2>, C<check4x4> - checkerboards at various sizes
292 C<vline1>, C<vline2>, C<vline4> - 1, 2, or 4 vertical lines per cell
296 C<hline1>, C<hline2>, C<hline4> - 1, 2, or 4 horizontal lines per cell
300 C<slash1>, C<slash2> - 1 or 2 / lines per cell.
304 C<slosh1>, C<slosh2> - 1 or 2 \ lines per cell
308 C<grid1>, C<grid2>, C<grid4> - 1, 2, or 4 vertical and horizontal
313 C<dots1>, C<dots4>, C<dots16> - 1, 4 or 16 dots per cell
317 C<stipple>, C<stipple2> - see the samples
321 C<weave> - I hope this one is obvious.
325 C<cross1>, C<cross2> - 2 densities of crosshatch
329 C<vlozenge>, C<hlozenge> - something like lozenge tiles
333 C<scalesdown>, C<scalesup>, C<scalesleft>, C<scalesright> - Vaguely
334 like fish scales in each direction.
338 C<tile_L> - L-shaped tiles
344 C<fg>, C<bg> - The C<fg> color is rendered where bits are set in the
345 hatch, and the C<bg> where they are clear. If you use a transparent
346 C<fg> or C<bg>, and set combine, you can overlay the hatch onto an
349 C<fg> defaults to black, C<bg> to white.
353 C<dx>, C<dy> - An offset into the hatch cell. Both default to zero.
357 A blue and white 4-pixel check patten:
359 my $fill = Imager::Fill->new(hatch => "check2x2", fg => "blue");
361 You can call Imager::Fill->hatches for a list of hatch names.
363 =head2 Fountain fills
365 my $fill = Imager::Fill->new(fountain=>$ftype,
366 xa=>$xa, ya=>$ya, xb=>$xb, yb=>$yb,
367 segments=>$segments, repeat=>$repeat, combine=>$combine,
368 super_sample=>$super_sample, ssample_param=>$ssample_param);
370 This fills the given region with a fountain fill. This is exactly the
371 same fill as the C<fountain> filter, but is restricted to the shape
372 you are drawing, and the fountain parameter supplies the fill type,
375 A radial fill from white to transparent centered on (50, 50) with a 50
378 use Imager::Fountain;
379 my $segs = Imager::Fountain->simple(colors => [ "FFFFFF", "FFFFFF00" ],
380 positions => [ 0, 1 ]);
381 my $fill = Imager::Fill->new(fountain => "radial", segments => $segs,
382 xa => 50, ya => 50, xb => 0, yb => 50,
383 combine => "normal");
388 my $fill = Imager::Fill->new(image=>$src, xoff=>$xoff, yoff=>$yoff,
389 matrix=>$matrix, combine => $combine);
391 Fills the given image with a tiled version of the given image. The
392 first non-zero value of C<xoff> or C<yoff> will provide an offset
393 along the given axis between rows or columns of tiles respectively.
395 The matrix parameter performs a co-ordinate transformation from the
396 co-ordinates in the target image to the fill image co-ordinates.
397 Linear interpolation is used to determine the fill pixel. You can use
398 the L<Imager::Matrix2d> class to create transformation matrices.
400 The matrix parameter will significantly slow down the fill.
402 # some image to act as a texture
403 my $txim = Imager->new(...);
406 my $fill = Imager::Fill->new(image => $txim);
408 # tile with a vertical offset
409 my $fill = Imager::Fill->new(image => $txim, yoff => 10);
411 # tile with a horizontal offset
412 my $fill = Imager::Fill->new(image => $txim, xoff => 10);
415 use Imager::Matrix2d;
416 my $fill = Imager::Fill->new(image => $txim,
417 matrix => Imager::Matrix2d->rotate(degrees => 20));
419 =head2 Opacity modification fill
421 my $fill = Imager::Fill->new(type => "opacity",
422 other => $fill, opacity => 0.25);
424 This can be used to make a fill that is a more translucent or opaque
425 version of an existing fill. This is intended for use where you
426 receive a fill object as a parameter and need to change the opacity.
434 type => "opacity" - Required
438 other - the fill to produce a modified version of. This must be an
439 Imager::Fill object. Required.
443 opacity - multiplier for the source fill opacity. Default: 0.5.
447 The source fills combine mode is used.
449 my $hatch = Imager::Fill->new(hatch => "check4x4", combine => "normal");
450 my $fill = Imager::Fill->new(type => "opacity", other => $hatch);
456 =item Imager::Fill->hatches
458 A list of all defined hatch names.
460 =item Imager::Fill->combines
462 A list of all combine types.
468 I'm planning on adding the following types of fills:
474 C<checkerboard> - combines 2 other fills in a checkerboard
478 C<combine> - combines 2 other fills using the levels of an image
482 C<regmach> - uses the transform2() register machine to create fills
488 Tony Cook <tony@develop-help.com>