<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Imager::Fill;
use strict;
use vars qw($VERSION);

$VERSION = "1.011";

# this needs to be kept in sync with the array of hatches in fills.c
my @hatch_types =
  qw/check1x1 check2x2 check4x4 vline1 vline2 vline4
     hline1 hline2 hline4 slash1 slosh1 slash2 slosh2
     grid1 grid2 grid4 dots1 dots4 dots16 stipple weave cross1 cross2
     vlozenge hlozenge scalesdown scalesup scalesleft scalesright stipple2
     tile_L stipple3/;
my %hatch_types;
@hatch_types{@hatch_types} = 0..$#hatch_types;

*_color = \&amp;Imager::_color;

sub new {
  my ($class, %hsh) = @_;

  my $self = bless { }, $class;
  $hsh{combine} = Imager-&gt;_combine($hsh{combine}, 0);
  if ($hsh{solid}) {
    my $solid = _color($hsh{solid});
    if (UNIVERSAL::isa($solid, 'Imager::Color')) {
      $self-&gt;{fill} = 
        Imager::i_new_fill_solid($solid, $hsh{combine});
    }
    elsif (UNIVERSAL::isa($solid, 'Imager::Color::Float')) {
      $self-&gt;{fill} = 
        Imager::i_new_fill_solidf($solid, $hsh{combine});
    }
    else {
      $Imager::ERRSTR = "solid isn't a color";
      return undef;
    }
  }
  elsif (defined $hsh{hatch}) {
    $hsh{dx} ||= 0;
    $hsh{dy} ||= 0;
    $hsh{fg} ||= Imager::Color-&gt;new(0, 0, 0);
    if (ref $hsh{hatch}) {
      $hsh{cust_hatch} = pack("C8", @{$hsh{hatch}});
      $hsh{hatch} = 0;
    }
    elsif ($hsh{hatch} =~ /\D/) {
      unless (exists($hatch_types{$hsh{hatch}})) {
        $Imager::ERRSTR = "Unknown hatch type $hsh{hatch}";
        return undef;
      }
      $hsh{hatch} = $hatch_types{$hsh{hatch}};
    }
    my $fg = _color($hsh{fg});
    if (UNIVERSAL::isa($fg, 'Imager::Color')) {
      my $bg = _color($hsh{bg} || Imager::Color-&gt;new(255, 255, 255));
      $self-&gt;{fill} = 
        Imager::i_new_fill_hatch($fg, $bg, $hsh{combine}, 
                                 $hsh{hatch}, $hsh{cust_hatch}, 
                                 $hsh{dx}, $hsh{dy});
    }
    elsif (UNIVERSAL::isa($fg, 'Imager::Color::Float')) {
      my $bg  = _color($hsh{bg} || Imager::Color::Float-&gt;new(1, 1, 1));
      $self-&gt;{fill} = 
        Imager::i_new_fill_hatchf($fg, $bg, $hsh{combine},
                                  $hsh{hatch}, $hsh{cust_hatch}, 
                                  $hsh{dx}, $hsh{dy});
    }
    else {
      $Imager::ERRSTR = "fg isn't a color";
      return undef;
    }
  }
  elsif (defined $hsh{fountain}) {
    # make sure we track the filter's defaults
    my $fount = $Imager::filters{fountain};
    my $def = $fount-&gt;{defaults};
    my $names = $fount-&gt;{names};
    
    $hsh{ftype} = $hsh{fountain};
    # process names of values
    for my $name (keys %$names) {
      if (defined $hsh{$name} &amp;&amp; exists $names-&gt;{$name}{$hsh{$name}}) {
        $hsh{$name} = $names-&gt;{$name}{$hsh{$name}};
      }
    }
    # process defaults
    %hsh = (%$def, %hsh);
    my @parms = @{$fount-&gt;{callseq}};
    shift @parms;
    for my $name (@parms) {
      unless (defined $hsh{$name}) {
        $Imager::ERRSTR = 
          "required parameter '$name' not set for fountain fill";
        return undef;
      }
    }

    # check that the segments supplied is an array ref
    unless (ref $hsh{segments} &amp;&amp; $hsh{segments} =~ /ARRAY/) {
      $Imager::ERRSTR =
        "segments must be an array reference or Imager::Fountain object";
      return;
    }

    # make sure the segments are specified with colors
    my @segments;
    for my $segment (@{$hsh{segments}}) {
      my @new_segment = @$segment;

      $_ = _color($_) or return for @new_segment[3,4];
      push @segments, \@new_segment;
    }

    $self-&gt;{fill} =
      Imager::i_new_fill_fount($hsh{xa}, $hsh{ya}, $hsh{xb}, $hsh{yb},
                  $hsh{ftype}, $hsh{repeat}, $hsh{combine}, $hsh{super_sample},
                  $hsh{ssample_param}, \@segments);
  }
  elsif (defined $hsh{image}) {
    $hsh{xoff} ||= 0;
    $hsh{yoff} ||= 0;
    $self-&gt;{fill} =
      Imager::i_new_fill_image($hsh{image}{IMG}, $hsh{matrix}, $hsh{xoff}, 
                               $hsh{yoff}, $hsh{combine});
    $self-&gt;{DEPS} = [ $hsh{image}{IMG} ];
  }
  elsif (defined $hsh{type} &amp;&amp; $hsh{type} eq "opacity") {
    my $other_fill = delete $hsh{other};
    unless (defined $other_fill) {
      Imager-&gt;_set_error("'other' parameter required to create opacity fill");
      return;
    }
    unless (ref $other_fill &amp;&amp;
	    eval { $other_fill-&gt;isa("Imager::Fill") }) {
      # try to auto convert to a fill object
      if (ref $other_fill &amp;&amp; $other_fill =~ /HASH/) {
	$other_fill = Imager::Fill-&gt;new(%$other_fill)
	  or return;
      }
      else {
	undef $other_fill;
      }
      unless ($other_fill) {
	Imager-&gt;_set_error("'other' parameter must be an Imager::Fill object to create an opacity fill");
	return;
      }
    }

    my $raw_fill = $other_fill-&gt;{fill};
    my $opacity = delete $hsh{opacity};
    defined $opacity or $opacity = 0.5; # some sort of default
    $self-&gt;{fill} = 
      Imager::i_new_fill_opacity($raw_fill, $opacity);
    $self-&gt;{DEPS} = [ $other_fill ]; # keep reference to old fill and its deps
  }
  else {
    $Imager::ERRSTR = "No fill type specified";
    warn "No fill type!";
    return undef;
  }

  $self;
}

sub hatches {
  return @hatch_types;
}

sub combines {
  return Imager-&gt;combines;
}

1;

=head1 NAME

  Imager::Fill - general fill types

=head1 SYNOPSIS

  use Imager;
  use Imager::Fill;

  my $fill1 = Imager::Fill-&gt;new(solid=&gt;$color, combine=&gt;$combine);
  my $fill2 = Imager::Fill-&gt;new(hatch=&gt;'vline2', fg=&gt;$color1, bg=&gt;$color2,
                                dx=&gt;$dx, dy=&gt;$dy);
  my $fill3 = Imager::Fill-&gt;new(fountain=&gt;$type, ...);
  my $fill4 = Imager::Fill-&gt;new(image=&gt;$img, ...);
  my $fill5 = Imager::Fill-&gt;new(type =&gt; "opacity", other =&gt; $fill,
                                opacity =&gt; ...);

=head1 DESCRIPTION 

Creates fill objects for use by most filled area drawing functions.

All fills are created with the new method.

=over

=item new

  my $fill = Imager::Fill-&gt;new(...);

The parameters depend on the type of fill being created.  See below
for details.

=back

The currently available fills are:

=over

=item *

solid

=item *

hatch

=item *

fountain (similar to gradients in paint software)

=back

=head1 Common options

=over

=item combine

The way in which the fill data is combined with the underlying image.
See L&lt;Imager::Draw/"Combine Types"&gt;.

=back

In general colors can be specified as Imager::Color or
Imager::Color::Float objects.  The fill object will typically store
both types and convert from one to the other.  If a fill takes 2 color
objects they should have the same type.

=head2 Solid fills

  my $fill = Imager::Fill-&gt;new(solid=&gt;$color, $combine =&gt;$combine)

Creates a solid fill, the only required parameter is C&lt;solid&gt; which
should be the color to fill with.

=head2 Hatched fills

  my $fill = Imager::Fill-&gt;new(hatch=&gt;$type, fg=&gt;$fgcolor, bg=&gt;$bgcolor,
                               dx=&gt;$dx, $dy=&gt;$dy);

Creates a hatched fill.  You can specify the following keywords:

=over

=item hatch

The type of hatch to perform, this can either be the numeric index of
the hatch (not recommended), the symbolic name of the hatch, or an
array of 8 integers which specify the pattern of the hatch.

Hatches are represented as cells 8x8 arrays of bits, which limits their
complexity.

Current hatch names are:

=over

=item *

C&lt;check1x1&gt;, C&lt;check2x2&gt;, C&lt;check4x4&gt; - checkerboards at various sizes

=item *

C&lt;vline1&gt;, C&lt;vline2&gt;, C&lt;vline4&gt; - 1, 2, or 4 vertical lines per cell

=item *

C&lt;hline1&gt;, C&lt;hline2&gt;, C&lt;hline4&gt; - 1, 2, or 4 horizontal lines per cell

=item *

C&lt;slash1&gt;, C&lt;slash2&gt; - 1 or 2 / lines per cell.

=item *

C&lt;slosh1&gt;, C&lt;slosh2&gt; - 1 or 2 \ lines per cell

=item *

C&lt;grid1&gt;, C&lt;grid2&gt;, C&lt;grid4&gt; - 1, 2, or 4 vertical and horizontal
lines per cell

=item *

C&lt;dots1&gt;, C&lt;dots4&gt;, C&lt;dots16&gt; - 1, 4 or 16 dots per cell

=item *

C&lt;stipple&gt;, C&lt;stipple2&gt; - see the samples

=item *

C&lt;weave&gt; - I hope this one is obvious.

=item *

C&lt;cross1&gt;, C&lt;cross2&gt; - 2 densities of crosshatch

=item *

C&lt;vlozenge&gt;, C&lt;hlozenge&gt; - something like lozenge tiles

=item *

C&lt;scalesdown&gt;, C&lt;scalesup&gt;, C&lt;scalesleft&gt;, C&lt;scalesright&gt; - Vaguely
like fish scales in each direction.

=item *

C&lt;tile_L&gt; - L-shaped tiles

=back

=item *

C&lt;fg&gt;, C&lt;bg&gt; - The C&lt;fg&gt; color is rendered where bits are set in the
hatch, and the C&lt;bg&gt; where they are clear.  If you use a transparent
C&lt;fg&gt; or C&lt;bg&gt;, and set combine, you can overlay the hatch onto an
existing image.

C&lt;fg&gt; defaults to black, C&lt;bg&gt; to white.

=item *

C&lt;dx&gt;, C&lt;dy&gt; - An offset into the hatch cell.  Both default to zero.

=back

You can call Imager::Fill-&gt;hatches for a list of hatch names.

=head2 Fountain fills

  my $fill = Imager::Fill-&gt;new(fountain=&gt;$ftype, 
       xa=&gt;$xa, ya=&gt;$ya, xb=&gt;$xb, yb=&gt;$yb, 
       segments=&gt;$segments, repeat=&gt;$repeat, combine=&gt;$combine, 
       super_sample=&gt;$super_sample, ssample_param=&gt;$ssample_param);

This fills the given region with a fountain fill.  This is exactly the
same fill as the C&lt;fountain&gt; filter, but is restricted to the shape
you are drawing, and the fountain parameter supplies the fill type,
and is required.

=head2 Image Fills

  my $fill = Imager::Fill-&gt;new(image=&gt;$src, xoff=&gt;$xoff, yoff=&gt;$yoff,
                               matrix=&gt;$matrix, $combine);

Fills the given image with a tiled version of the given image.  The
first non-zero value of C&lt;xoff&gt; or C&lt;yoff&gt; will provide an offset
along the given axis between rows or columns of tiles respectively.

The matrix parameter performs a co-ordinate transformation from the
co-ordinates in the target image to the fill image co-ordinates.
Linear interpolation is used to determine the fill pixel.  You can use
the L&lt;Imager::Matrix2d&gt; class to create transformation matrices.

The matrix parameter will significantly slow down the fill.

=head2 Opacity modification fill

  my $fill = Imager::Fill-&gt;new(type =&gt; "opacity",
      other =&gt; $fill, opacity =&gt; 0.25);

This can be used to make a fill that is a more translucent or opaque
version of an existing fill.  This is intended for use where you
receive a fill object as a parameter and need to change the opacity.

Parameters:

=over

=item *

type =&gt; "opacity" - Required

=item *

other - the fill to produce a modified version of.  This must be an
Imager::Fill object.  Required.

=item *

opacity - multiplier for the source fill opacity.  Default: 0.5.

=back

The source fills combine mode is used.

=head1 OTHER METHODS

=over

=item Imager::Fill-&gt;hatches

A list of all defined hatch names.

=item Imager::Fill-&gt;combines

A list of all combine types.

=back

=head1 FUTURE PLANS

I'm planning on adding the following types of fills:

=over

=item *

C&lt;checkerboard&gt; - combines 2 other fills in a checkerboard

=item *

C&lt;combine&gt; - combines 2 other fills using the levels of an image

=item *

C&lt;regmach&gt; - uses the transform2() register machine to create fills

=back

=head1 AUTHOR

Tony Cook &lt;tony@develop-help.com&gt;

=head1 SEE ALSO

Imager(3)

=cut
</pre></body></html>