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 =item read(gimp=>$filename, name=>\$name)
33 Loads a gradient from the given GIMP gradient file, and returns a
34 new Imager::Fountain object.
36 If the name parameter is supplied as a scalar reference then any name
37 field from newer GIMP gradient files will be returned in it.
39 my $gradient = Imager::Fountain->read(gimp=>'foo.ggr');
41 my $gradient2 = Imager::Fountain->read(gimp=>'bar.ggr', name=>\$name);
46 my ($class, %opts) = @_;
50 $fh = ref($opts{gimp}) ? $opts{gimp} : IO::File->new($opts{gimp});
52 $Imager::ERRSTR = "Cannot open $opts{gimp}: $!";
57 my $name_ref = $opts{name} && ref $opts{name} ? $opts{name} : \$trash_name;
59 return $class->_load_gimp_gradient($fh, $opts{gimp}, $name_ref);
62 warn "$class::read: Nothing to do!";
67 =item write(gimp=>$filename)
69 =item write(gimp=>$filename, name=>$name)
71 Save the gradient to a GIMP gradient file.
73 The second variant allows the gradient name to be set (for newer
74 versions of the GIMP).
76 $gradient->write(gimp=>'foo.ggr')
77 or die Imager->errstr;
78 $gradient->write(gimp=>'bar.ggr', name=>'the bar gradient')
79 or die Imager->errstr;
84 my ($self, %opts) = @_;
88 $fh = ref($opts{gimp}) ? $opts{gimp} : IO::File->new("> ".$opts{gimp});
90 $Imager::ERRSTR = "Cannot open $opts{gimp}: $!";
94 return $self->_save_gimp_gradient($fh, $opts{gimp}, $opts{name});
97 warn "Nothing to do\n";
104 Create an empty fountain fill description.
111 return bless [], $class;
116 return $_ if defined;
121 =item add(start=>$start, middle=>$middle, end=>1.0, c0=>$start_color, c1=>$end_color, type=>$trans_type, color=>$color_trans_type)
123 Adds a new segment to the fountain fill, the possible options are:
129 The start position in the gradient where this segment
130 takes effect between 0 and 1. Default: 0.
134 The mid-point of the transition between the 2
135 colors, between 0 and 1. Default: average of I<start> and I<end>.
139 The end of the gradient, from 0 to 1. Default: 1.
143 The color of the fountain fill where the fill parameter is equal
144 to I<start>. Default: opaque black.
148 The color of the fountain fill where the fill parameter is equal to
149 I<end>. Default: opaque black.
153 The type of segment, controls the way in which the fill parameter
154 moves from 0 to 1. Default: linear.
156 This can take any of the following values:
164 Unimplemented so far.
176 The way in which the color transitions between I<c0> and I<c1>.
179 This can take any of the following values:
185 Each channel is simple scaled between c0 and c1.
189 The color is converted to a HSV value and the scaling is done such
190 that the hue increases as the fill parameter increases.
194 The color is converted to a HSV value and the scaling is done such
195 that the hue decreases as the fill parameter increases.
201 In most cases you can ignore some of the arguments, eg.
203 # assuming $f is a new Imager::Fountain in each case here
205 # simple transition from red to blue
206 $f->add(c0=>NC('#FF0000), c1=>NC('#0000FF'));
207 # simple 2 stages from red to green to blue
208 $f->add(end=>0.5, c0=>NC('#FF0000'), c1=>NC('#00FF00'))
209 $f->add(start=>0.5, c0=>NC('#00FF00'), c1->NC('#0000FF'));
213 # used to translate segment types and color transition types to numbers
231 my ($self, %opts) = @_;
233 my $start = _first($opts{start}, 0);
234 my $end = _first($opts{end}, 1);
235 my $middle = _first($opts{middle}, ($start+$end)/2);
238 $start, $middle, $end,
239 _first($opts{c0}, Imager::Color::Float->new(0,0,0,1)),
240 _first($opts{c1}, Imager::Color::Float->new(1,1,1,0)),
241 _first($opts{type} && $type_names{$opts{type}}, $opts{type}, 0),
242 _first($opts{color} && $color_names{$opts{color}}, $opts{color}, 0)
249 =item simple(positions=>[ ... ], colors=>[...])
251 Creates a simple fountain fill object consisting of linear segments.
253 The arrayrefs passed as positions and colors must have the same number
254 of elements. They must have at least 2 elements each.
256 colors must contain Imager::Color or Imager::Color::Float objects.
260 my $f = Imager::Fountain->simple(positions=>[0, 0.2, 1.0],
261 colors=>[ NC(255,0,0), NC(0,255,0),
267 my ($class, %opts) = @_;
269 if ($opts{positions} && $opts{colors}) {
270 my $positions = $opts{positions};
271 my $colors = $opts{colors};
272 unless (@$positions == @$colors) {
273 $Imager::ERRSTR = "positions and colors must be the same size";
276 unless (@$positions >= 2) {
277 $Imager::ERRSTR = "not enough segments";
281 for my $i (0.. $#$colors-1) {
282 $f->add(start=>$positions->[$i], end=>$positions->[$i+1],
283 c0 => $colors->[$i], c1=>$colors->[$i+1]);
288 warn "Nothing to do";
295 =head2 Implementation Functions
297 Documented for internal use.
301 =item _load_gimp_gradient($class, $fh, $name)
303 Does the work of loading a GIMP gradient file.
307 sub _load_gimp_gradient {
308 my ($class, $fh, $filename, $name) = @_;
312 unless ($head eq 'GIMP Gradient') {
313 $Imager::ERRSTR = "$filename is not a GIMP gradient file";
318 if ($count =~ /^name:\s?(.*)/i) {
319 ref $name and $$name = $1;
320 $count = <$fh>; # try again
322 unless ($count =~ /^\d$/) {
323 $Imager::ERRSTR = "$filename is missing the segment count";
327 for my $i (1..$count) {
330 my @row = split ' ', $row;
331 unless (@row == 13) {
332 $Imager::ERRSTR = "Bad segment definition";
335 my ($start, $middle, $end) = splice(@row, 0, 3);
336 my $c0 = Imager::Color::Float->new(splice(@row, 0, 4));
337 my $c1 = Imager::Color::Float->new(splice(@row, 0, 4));
338 my ($type, $color) = @row;
339 push(@result, [ $start, $middle, $end, $c0, $c1, $type, $color ]);
341 return bless \@result,
344 =item _save_gimp_gradient($self, $fh, $name)
346 Does the work of saving to a GIMP gradient file.
350 sub _save_gimp_gradient {
351 my ($self, $fh, $filename, $name) = @_;
353 print $fh "GIMP Gradient\n";
354 defined $name or $name = '';
355 $name =~ tr/ -~/ /cds;
357 print $fh "Name: $name\n";
359 print $fh scalar(@$self),"\n";
360 for my $row (@$self) {
361 printf $fh "%.6f %.6f %.6f ",@{$row}[0..2];
363 for ($row->[3+$i]->rgba) {
364 printf $fh "%.6f ", $_/255.0;
367 print $fh "@{$row}[5,6]";
368 unless (print $fh "\n") {
369 $Imager::ERRSTR = "write error: $!";
379 =head1 FILL PARAMETER
381 The add() documentation mentions a fill parameter in a few places,
382 this is as good a place as any to discuss it.
384 The process of deciding the color produced by the gradient works
385 through the following steps:
391 calculate the base value, which is typically a distance or an angle of
392 some sort. This can be positive or occasinally negative, depending on
393 the type of fill being performed (linear, radial, etc).
397 clamp or convert the base value to the range 0 through 1, how this is
398 done depends on the repeat parameter. I'm calling this result the
403 the appropriate segment is found. This is currently done with a
404 linear search, and the first matching segment is used. If there is no
405 matching segment the pixel is not touched.
409 the fill parameter is scaled from 0 to 1 depending on the segment type.
413 the color produced, depending on the segment color type.
419 Tony Cook <tony@develop-help.com>