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 *_color = \&Imager::_color;
25 my ($class, %hsh) = @_;
27 my $self = bless { }, $class;
29 if (exists $combine_types{$hsh{combine}}) {
30 $hsh{combine} = $combine_types{$hsh{combine}};
33 my $solid = _color($hsh{solid});
34 if (UNIVERSAL::isa($solid, 'Imager::Color')) {
36 Imager::i_new_fill_solid($solid, $hsh{combine});
38 elsif (UNIVERSAL::isa($solid, 'Imager::Color::Float')) {
40 Imager::i_new_fill_solidf($solid, $hsh{combine});
43 $Imager::ERRSTR = "solid isn't a color";
47 elsif (defined $hsh{hatch}) {
50 $hsh{fg} ||= Imager::Color->new(0, 0, 0);
51 if (ref $hsh{hatch}) {
52 $hsh{cust_hatch} = pack("C8", @{$hsh{hatch}});
55 elsif ($hsh{hatch} =~ /\D/) {
56 unless (exists($hatch_types{$hsh{hatch}})) {
57 $Imager::ERRSTR = "Unknown hatch type $hsh{hatch}";
60 $hsh{hatch} = $hatch_types{$hsh{hatch}};
62 my $fg = _color($hsh{fg});
63 if (UNIVERSAL::isa($fg, 'Imager::Color')) {
64 my $bg = _color($hsh{bg} || Imager::Color->new(255, 255, 255));
66 Imager::i_new_fill_hatch($fg, $bg, $hsh{combine},
67 $hsh{hatch}, $hsh{cust_hatch},
70 elsif (UNIVERSAL::isa($fg, 'Imager::Color::Float')) {
71 my $bg = _color($hsh{bg} || Imager::Color::Float->new(1, 1, 1));
73 Imager::i_new_fill_hatchf($fg, $bg, $hsh{combine},
74 $hsh{hatch}, $hsh{cust_hatch},
78 $Imager::ERRSTR = "fg isn't a color";
82 elsif (defined $hsh{fountain}) {
83 # make sure we track the filter's defaults
84 my $fount = $Imager::filters{fountain};
85 my $def = $fount->{defaults};
86 my $names = $fount->{names};
88 $hsh{ftype} = $hsh{fountain};
89 # process names of values
90 for my $name (keys %$names) {
91 if (defined $hsh{$name} && exists $names->{$name}{$hsh{$name}}) {
92 $hsh{$name} = $names->{$name}{$hsh{$name}};
97 my @parms = @{$fount->{callseq}};
99 for my $name (@parms) {
100 unless (defined $hsh{$name}) {
102 "required parameter '$name' not set for fountain fill";
107 # check that the segments supplied is an array ref
108 unless (ref $hsh{segments} && $hsh{segments} =~ /ARRAY/) {
110 "segments must be an array reference or Imager::Fountain object";
114 # make sure the segments are specified with colors
116 for my $segment (@{$hsh{segments}}) {
117 my @new_segment = @$segment;
119 $_ = _color($_) or return for @new_segment[3,4];
120 push @segments, \@new_segment;
124 Imager::i_new_fill_fount($hsh{xa}, $hsh{ya}, $hsh{xb}, $hsh{yb},
125 $hsh{ftype}, $hsh{repeat}, $hsh{combine}, $hsh{super_sample},
126 $hsh{ssample_param}, \@segments);
128 elsif (defined $hsh{image}) {
132 Imager::i_new_fill_image($hsh{image}{IMG}, $hsh{matrix}, $hsh{xoff},
133 $hsh{yoff}, $hsh{combine});
134 $self->{DEPS} = [ $hsh{image}{IMG} ];
137 $Imager::ERRSTR = "No fill type specified";
138 warn "No fill type!";
150 return @combine_types;
157 Imager::Fill - general fill types
161 my $fill1 = Imager::Fill->new(solid=>$color, combine=>$combine);
162 my $fill2 = Imager::Fill->new(hatch=>'vline2', fg=>$color1, bg=>$color2,
164 my $fill3 = Imager::Fill->new(fountain=>$type, ...);
165 my $fill4 = Imager::Fill->new(image=>$img, ...);
169 Creates fill objects for use by some drawing functions, currently just
170 the Imager box() method.
172 The currently available fills are:
186 fountain (similar to gradients in paint software)
190 =head1 Common options
196 The way in which the fill data is combined with the underlying image,
197 possible values include:
203 The fill pixel replaces the target pixel.
207 The fill pixels alpha value is used to combine it with the target pixel.
213 Each channel of fill and target is multiplied, and the result is
214 combined using the alpha channel of the fill pixel.
218 If the alpha of the fill pixel is greater than a random number, the
219 fill pixel is alpha combined with the target pixel.
223 The channels of the fill and target are added together, clamped to the range of the samples and alpha combined with the target.
227 The channels of the fill are subtracted from the target, clamped to be
228 >= 0, and alpha combined with the target.
232 The channels of the fill are subtracted from the target and the
233 absolute value taken this is alpha combined with the target.
237 The higher value is taken from each channel of the fill and target
238 pixels, which is then alpha combined with the target.
242 The higher value is taken from each channel of the fill and target
243 pixels, which is then alpha combined with the target.
247 The combination of the saturation and value of the target is combined
248 with the hue of the fill pixel, and is then alpha combined with the
253 The combination of the hue and value of the target is combined
254 with the saturation of the fill pixel, and is then alpha combined with the
259 The combination of the hue and value of the target is combined
260 with the value of the fill pixel, and is then alpha combined with the
265 The combination of the value of the target is combined with the hue
266 and saturation of the fill pixel, and is then alpha combined with the
273 In general colors can be specified as Imager::Color or
274 Imager::Color::Float objects. The fill object will typically store
275 both types and convert from one to the other. If a fill takes 2 color
276 objects they should have the same type.
280 my $fill = Imager::Fill->new(solid=>$color, $combine =>$combine)
282 Creates a solid fill, the only required parameter is C<solid> which
283 should be the color to fill with.
287 my $fill = Imager::Fill->new(hatch=>$type, fg=>$fgcolor, bg=>$bgcolor,
290 Creates a hatched fill. You can specify the following keywords:
296 The type of hatch to perform, this can either be the numeric index of
297 the hatch (not recommended), the symbolic name of the hatch, or an
298 array of 8 integers which specify the pattern of the hatch.
300 Hatches are represented as cells 8x8 arrays of bits, which limits their
303 Current hatch names are:
307 =item check1x1, check2x2, check4x4
309 checkerboards at varios sizes
311 =item vline1, vline2, vline4
313 1, 2, or 4 vertical lines per cell
315 =item hline1, hline2, hline4
317 1, 2, or 4 horizontal lines per cell
321 1 or 2 / lines per cell.
325 1 or 2 \ lines per cell
327 =item grid1, grid2, grid4
329 1, 2, or 4 vertical and horizontal lines per cell
331 =item dots1, dots4, dots16
333 1, 4 or 16 dots per cell
335 =item stipple, stipple2
341 I hope this one is obvious.
345 2 densities of crosshatch
347 =item vlozenge, hlozenge
349 something like lozenge tiles
351 =item scalesdown, scalesup, scalesleft, scalesright
353 Vaguely like fish scales in each direction.
365 The fg color is rendered where bits are set in the hatch, and the bg
366 where they are clear. If you use a transparent fg or bg, and set
367 combine, you can overlay the hatch onto an existing image.
369 fg defaults to black, bg to white.
375 An offset into the hatch cell. Both default to zero.
379 You can call Imager::Fill->hatches for a list of hatch names.
381 =head2 Fountain fills
383 my $fill = Imager::Fill->new(fountain=>$ftype,
384 xa=>$xa, ya=>$ya, xb=>$xb, yb=>$yb,
385 segment=>$segments, repeat=>$repeat, combine=>$combine,
386 super_sample=>$super_sample, ssample_param=>$ssample_param);
388 This fills the given region with a fountain fill. This is exactly the
389 same fill as the C<fountain> filter, but is restricted to the shape
390 you are drawing, and the fountain parameter supplies the fill type,
395 my $fill = Imager::Fill->new(image=>$src, xoff=>$xoff, yoff=>$yoff,
396 matrix=>$matrix, $combine);
398 Fills the given image with a tiled version of the given image. The
399 first non-zero value of xoff or yoff will provide an offset along the
400 given axis between rows or columns of tiles respectively.
402 The matrix parameter performs a co-ordinate transformation from the
403 co-ordinates in the target image to the fill image co-ordinates.
404 Linear interpolation is used to determine the fill pixel. You can use
405 the L<Imager::Matrix2d> class to create transformation matrices.
407 The matrix parameter will significantly slow down the fill.
413 =item Imager::Fill->hatches
415 A list of all defined hatch names.
417 =item Imager::Fill->combines
419 A list of all combine types.
425 I'm planning on adding the following types of fills:
431 combines 2 other fills in a checkerboard
435 combines 2 other fills using the levels of an image
439 uses the transform2() register machine to create fills
445 Tony Cook <tony@develop-help.com>