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});
153 $Imager::ERRSTR = "No fill type specified";
154 warn "No fill type!";
166 return @combine_types;
173 Imager::Fill - general fill types
177 my $fill1 = Imager::Fill->new(solid=>$color, combine=>$combine);
178 my $fill2 = Imager::Fill->new(hatch=>'vline2', fg=>$color1, bg=>$color2,
183 Creates fill objects for use by some drawing functions, currently just
184 the Imager box() method.
186 The currently available fills are:
200 fountain (similar to gradients in paint software)
204 =head1 Common options
210 The way in which the fill data is combined with the underlying image,
211 possible values include:
217 The fill pixel replaces the target pixel.
221 The fill pixels alpha value is used to combine it with the target pixel.
227 Each channel of fill and target is multiplied, and the result is
228 combined using the alpha channel of the fill pixel.
232 If the alpha of the fill pixel is greater than a random number, the
233 fill pixel is alpha combined with the target pixel.
237 The channels of the fill and target are added together, clamped to the range of the samples and alpha combined with the target.
241 The channels of the fill are subtracted from the target, clamped to be
242 >= 0, and alpha combined with the target.
246 The channels of the fill are subtracted from the target and the
247 absolute value taken this is alpha combined with the target.
251 The higher value is taken from each channel of the fill and target pixels, which is then alpha combined with the target.
255 The higher value is taken from each channel of the fill and target pixels, which is then alpha combined with the target.
259 The combination of the saturation and value of the target is combined
260 with the hue of the fill pixel, and is then alpha combined with the
265 The combination of the hue and value of the target is combined
266 with the saturation of the fill pixel, and is then alpha combined with the
271 The combination of the hue and value of the target is combined
272 with the value of the fill pixel, and is then alpha combined with the
277 The combination of the value of the target is combined with the hue
278 and saturation of the fill pixel, and is then alpha combined with the
285 In general colors can be specified as Imager::Color or
286 Imager::Color::Float objects. The fill object will typically store
287 both types and convert from one to the other. If a fill takes 2 color
288 objects they should have the same type.
292 my $fill = Imager::Fill->new(solid=>$color, $combine =>$combine)
294 Creates a solid fill, the only required parameter is C<solid> which
295 should be the color to fill with.
299 my $fill = Imager::Fill->new(hatch=>$type, fg=>$fgcolor, bg=>$bgcolor,
302 Creates a hatched fill. You can specify the following keywords:
308 The type of hatch to perform, this can either be the numeric index of
309 the hatch (not recommended), the symbolic name of the hatch, or an
310 array of 8 integers which specify the pattern of the hatch.
312 Hatches are represented as cells 8x8 arrays of bits, which limits their
315 Current hatch names are:
319 =item check1x1, check2x2, check4x4
321 checkerboards at varios sizes
323 =item vline1, vline2, vline4
325 1, 2, or 4 vertical lines per cell
327 =item hline1, hline2, hline4
329 1, 2, or 4 horizontal lines per cell
333 1 or 2 / lines per cell.
337 1 or 2 \ lines per cell
339 =item grid1, grid2, grid4
341 1, 2, or 4 vertical and horizontal lines per cell
343 =item dots1, dots4, dots16
345 1, 4 or 16 dots per cell
347 =item stipple, stipple2
353 I hope this one is obvious.
357 2 densities of crosshatch
359 =item vlozenge, hlozenge
361 something like lozenge tiles
363 =item scalesdown, scalesup, scalesleft, scalesright
365 Vaguely like fish scales in each direction.
377 The fg color is rendered where bits are set in the hatch, and the bg
378 where they are clear. If you use a transparent fg or bg, and set
379 combine, you can overlay the hatch onto an existing image.
381 fg defaults to black, bg to white.
387 An offset into the hatch cell. Both default to zero.
391 You can call Imager::Fill->hatches for a list of hatch names.
393 =head2 Fountain fills
395 my $fill = Imager::Fill->new(fountain=>$ftype,
396 xa=>$xa, ya=>$ya, xb=>$xb, yb=>$yb,
397 segment=>$segments, repeat=>$repeat, combine=>$combine,
398 super_sample=>$super_sample, ssample_param=>$ssample_param);
400 This fills the given region with a fountain fill. This is exactly the
401 same fill as the C<fountain> filter, but is restricted to the shape
402 you are drawing, and the fountain parameter supplies the fill type,
409 =item Imager::Fill->hatches
411 A list of all defined hatch names.
413 =item Imager::Fill->combines
415 A list of all combine types.
421 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>