1 package Imager::Fountain;
3 use Imager::Color::Float;
7 Imager::Fountain - a class for building fountain fills suitable for use by
13 my $f1 = Imager::Fountain->read(gimp=>$filename);
14 $f->write(gimp=>$filename);
15 my $f1 = Imager::Fountain->new;
16 $f1->add(start=>0, middle=>0.5, end=>1.0,
17 c0=>Imager::Color->new(...),
18 c1=>Imager::Color->new(...),
19 type=>$trans_type, color=>$color_trans_type);
23 Provide an interface to build arrays suitable for use by the Imager
24 fountain filter. These can be loaded from or saved to a GIMP gradient
25 file or you can build them from scratch.
29 =item read(gimp=>$filename)
31 Loads a gradient from the given GIMP gradient file, and returns a
32 new Imager::Fountain object.
37 my ($class, %opts) = @_;
41 $fh = ref($opts{gimp}) ? $opts{gimp} : IO::File->new($opts{gimp});
43 $Imager::ERRSTR = "Cannot open $opts{gimp}: $!";
47 return $class->_load_gimp_gradient($fh, $opts{gimp});
50 warn "$class::read: Nothing to do!";
55 =item write(gimp=>$filename)
57 Save the gradient to a GIMP gradient file.
62 my ($self, %opts) = @_;
66 $fh = ref($opts{gimp}) ? $opts{gimp} : IO::File->new("> ".$opts{gimp});
68 $Imager::ERRSTR = "Cannot open $opts{gimp}: $!";
72 return $self->_save_gimp_gradient($fh, $opts{gimp});
75 warn "Nothing to do\n";
82 Create an empty fountain fill description.
89 return bless [], $class;
99 =item add(start=>$start, middle=>$middle, end=>1.0, c0=>$start_color, c1=>$end_color, type=>$trans_type, color=>$color_trans_type)
101 Adds a new segment to the fountain fill, the possible options are:
107 The start position in the gradient where this segment
108 takes effect between 0 and 1. Default: 0.
112 The mid-point of the transition between the 2
113 colors, between 0 and 1. Default: average of I<start> and I<end>.
117 The end of the gradient, from 0 to 1. Default: 1.
121 The color of the fountain fill where the fill parameter is equal
122 to I<start>. Default: opaque black.
126 The color of the fountain fill where the fill parameter is equal to
127 I<end>. Default: opaque black.
131 The type of segment, controls the way in which the fill parameter
132 moves from 0 to 1. Default: linear.
134 This can take any of the following values:
142 Unimplemented so far.
154 The way in which the color transitions between I<c0> and I<c1>.
157 This can take any of the following values:
163 Each channel is simple scaled between c0 and c1.
167 The color is converted to a HSV value and the scaling is done such
168 that the hue increases as the fill parameter increases.
172 The color is converted to a HSV value and the scaling is done such
173 that the hue decreases as the fill parameter increases.
179 In most cases you can ignore some of the arguments, eg.
181 # assuming $f is a new Imager::Fountain in each case here
183 # simple transition from red to blue
184 $f->add(c0=>NC('#FF0000), c1=>NC('#0000FF'));
185 # simple 2 stages from red to green to blue
186 $f->add(end=>0.5, c0=>NC('#FF0000'), c1=>NC('#00FF00'))
187 $f->add(start=>0.5, c0=>NC('#00FF00'), c1->NC('#0000FF'));
191 # used to translate segment types and color transition types to numbers
209 my ($self, %opts) = @_;
211 my $start = _first($opts{start}, 0);
212 my $end = _first($opts{end}, 1);
213 my $middle = _first($opts{middle}, ($start+$end)/2);
216 $start, $middle, $end,
217 _first($opts{c0}, Imager::Color::Float->new(0,0,0,1)),
218 _first($opts{c1}, Imager::Color::Float->new(1,1,1,0)),
219 _first($opts{type} && $type_names{$opts{type}}, $opts{type}, 0),
220 _first($opts{color} && $color_names{$opts{color}}, $opts{color}, 0)
227 =item simple(positions=>[ ... ], colors=>[...])
229 Creates a simple fountain fill object consisting of linear segments.
231 The arrayrefs passed as positions and colors must have the same number
232 of elements. They must have at least 2 elements each.
234 colors must contain Imager::Color or Imager::Color::Float objects.
238 my $f = Imager::Fountain->simple(positions=>[0, 0.2, 1.0],
239 colors=>[ NC(255,0,0), NC(0,255,0),
245 my ($class, %opts) = @_;
247 if ($opts{positions} && $opts{colors}) {
248 my $positions = $opts{positions};
249 my $colors = $opts{colors};
250 unless (@$positions == @$colors) {
251 $Imager::ERRSTR = "positions and colors must be the same size";
254 unless (@$positions >= 2) {
255 $Imager::ERRSTR = "not enough segments";
259 for my $i (0.. $#$colors-1) {
260 $f->add(start=>$positions->[$i], end=>$positions->[$i+1],
261 c0 => $colors->[$i], c1=>$colors->[$i+1]);
265 warn "Nothing to do";
272 =head2 Implementation Functions
274 Documented for internal use.
278 =item _load_gimp_gradient($class, $fh, $name)
280 Does the work of loading a GIMP gradient file.
284 sub _load_gimp_gradient {
285 my ($class, $fh, $name) = @_;
289 unless ($head eq 'GIMP Gradient') {
290 $Imager::ERRSTR = "$name is not a GIMP gradient file";
295 unless ($count =~ /^\d$/) {
296 $Imager::ERRSTR = "$name is missing the segment count";
300 for my $i (1..$count) {
303 my @row = split ' ', $row;
304 unless (@row == 13) {
305 $Imager::ERRSTR = "Bad segment definition";
308 my ($start, $middle, $end) = splice(@row, 0, 3);
309 my $c0 = Imager::Color::Float->new(splice(@row, 0, 4));
310 my $c1 = Imager::Color::Float->new(splice(@row, 0, 4));
311 my ($type, $color) = @row;
312 push(@result, [ $start, $middle, $end, $c0, $c1, $type, $color ]);
314 return bless \@result,
317 =item _save_gimp_gradient($self, $fh, $name)
319 Does the work of saving to a GIMP gradient file.
323 sub _save_gimp_gradient {
324 my ($self, $fh, $name) = @_;
326 print $fh "GIMP Gradient\n";
327 print $fh scalar(@$self),"\n";
328 for my $row (@$self) {
329 printf $fh "%.6f %.6f %.6f ",@{$row}[0..2];
331 for ($row->[3+$i]->rgba) {
332 printf $fh, "%.6f ", $_;
335 print $fh @{$row}[5,6];
336 unless (print $fh "\n") {
337 $Imager::ERRSTR = "write error: $!";
347 =head1 FILL PARAMETER
349 The add() documentation mentions a fill parameter in a few places,
350 this is as good a place as any to discuss it.
352 The process of deciding the color produced by the gradient works
353 through the following steps:
359 calculate the base value, which is typically a distance or an angle of
360 some sort. This can be positive or occasinally negative, depending on
361 the type of fill being performed (linear, radial, etc).
365 clamp or convert the base value to the range 0 through 1, how this is
366 done depends on the repeat parameter. I'm calling this result the
371 the appropriate segment is found. This is currently done with a
372 linear search, and the first matching segment is used. If there is no
373 matching segment the pixel is not touched.
377 the fill parameter is scaled from 0 to 1 depending on the segment type.
381 the color produced, depending on the segment color type.
387 Tony Cook <tony@develop-help.com>