1 package Imager::Fountain;
3 use Imager::Color::Float;
10 Imager::Fountain - a class for building fountain fills suitable for use by
16 my $f1 = Imager::Fountain->read(gimp=>$filename);
17 $f->write(gimp=>$filename);
18 my $f1 = Imager::Fountain->new;
19 $f1->add(start=>0, middle=>0.5, end=>1.0,
20 c0=>Imager::Color->new(...),
21 c1=>Imager::Color->new(...),
22 type=>$trans_type, color=>$color_trans_type);
26 Provide an interface to build arrays suitable for use by the Imager
27 fountain filter. These can be loaded from or saved to a GIMP gradient
28 file or you can build them from scratch.
32 =item read(gimp=>$filename)
34 =item read(gimp=>$filename, name=>\$name)
36 Loads a gradient from the given GIMP gradient file, and returns a
37 new Imager::Fountain object.
39 If the name parameter is supplied as a scalar reference then any name
40 field from newer GIMP gradient files will be returned in it.
42 my $gradient = Imager::Fountain->read(gimp=>'foo.ggr');
44 my $gradient2 = Imager::Fountain->read(gimp=>'bar.ggr', name=>\$name);
49 my ($class, %opts) = @_;
53 $fh = ref($opts{gimp}) ? $opts{gimp} : IO::File->new($opts{gimp});
55 $Imager::ERRSTR = "Cannot open $opts{gimp}: $!";
60 my $name_ref = $opts{name} && ref $opts{name} ? $opts{name} : \$trash_name;
62 return $class->_load_gimp_gradient($fh, $opts{gimp}, $name_ref);
65 warn "${class}::read: Nothing to do!";
70 =item write(gimp=>$filename)
72 =item write(gimp=>$filename, name=>$name)
74 Save the gradient to a GIMP gradient file.
76 The second variant allows the gradient name to be set (for newer
77 versions of the GIMP).
79 $gradient->write(gimp=>'foo.ggr')
80 or die Imager->errstr;
81 $gradient->write(gimp=>'bar.ggr', name=>'the bar gradient')
82 or die Imager->errstr;
87 my ($self, %opts) = @_;
91 $fh = ref($opts{gimp}) ? $opts{gimp} : IO::File->new("> ".$opts{gimp});
93 $Imager::ERRSTR = "Cannot open $opts{gimp}: $!";
97 return $self->_save_gimp_gradient($fh, $opts{gimp}, $opts{name});
100 warn "Nothing to do\n";
107 Create an empty fountain fill description.
114 return bless [], $class;
119 return $_ if defined;
124 =item add(start=>$start, middle=>$middle, end=>1.0, c0=>$start_color, c1=>$end_color, type=>$trans_type, color=>$color_trans_type)
126 Adds a new segment to the fountain fill, the possible options are:
132 C<start> - the start position in the gradient where this segment takes
133 effect between 0 and 1. Default: 0.
137 C<middle> - the mid-point of the transition between the 2
138 colors, between 0 and 1. Default: average of C<start> and C<end>.
142 C<end> - the end of the gradient, from 0 to 1. Default: 1.
146 C<c0> - the color of the fountain fill where the fill parameter is
147 equal to I<start>. Default: opaque black.
151 C<c1> - the color of the fountain fill where the fill parameter is
152 equal to I<end>. Default: opaque black.
156 C<type> - the type of segment, controls the way in which the fill parameter
157 moves from 0 to 1. Default: linear.
159 This can take any of the following values:
169 C<curved> - unimplemented so far.
187 C<color> - the way in which the color transitions between C<c0> and C<c1>.
190 This can take any of the following values:
196 C<direct> - each channel is simple scaled between c0 and c1.
200 C<hueup> - the color is converted to a HSV value and the scaling is
201 done such that the hue increases as the fill parameter increases.
205 C<huedown> - the color is converted to a HSV value and the scaling is
206 done such that the hue decreases as the fill parameter increases.
212 In most cases you can ignore some of the arguments, eg.
214 # assuming $f is a new Imager::Fountain in each case here
216 # simple transition from red to blue
217 $f->add(c0=>NC('#FF0000'), c1=>NC('#0000FF'));
218 # simple 2 stages from red to green to blue
219 $f->add(end=>0.5, c0=>NC('#FF0000'), c1=>NC('#00FF00'))
220 $f->add(start=>0.5, c0=>NC('#00FF00'), c1=>NC('#0000FF'));
224 # used to translate segment types and color transition types to numbers
242 my ($self, %opts) = @_;
244 my $start = _first($opts{start}, 0);
245 my $end = _first($opts{end}, 1);
246 my $middle = _first($opts{middle}, ($start+$end)/2);
249 $start, $middle, $end,
250 _first($opts{c0}, Imager::Color::Float->new(0,0,0,1)),
251 _first($opts{c1}, Imager::Color::Float->new(1,1,1,0)),
252 _first($opts{type} && $type_names{$opts{type}}, $opts{type}, 0),
253 _first($opts{color} && $color_names{$opts{color}}, $opts{color}, 0)
260 =item simple(positions=>[ ... ], colors=>[...])
262 Creates a simple fountain fill object consisting of linear segments.
264 The array references passed as positions and colors must have the same
265 number of elements. They must have at least 2 elements each.
267 colors must contain Imager::Color or Imager::Color::Float objects.
271 my $f = Imager::Fountain->simple(positions=>[0, 0.2, 1.0],
272 colors=>[ NC(255,0,0), NC(0,255,0),
278 my ($class, %opts) = @_;
280 if ($opts{positions} && $opts{colors}) {
281 my $positions = $opts{positions};
282 my $colors = $opts{colors};
283 unless (@$positions == @$colors) {
284 $Imager::ERRSTR = "positions and colors must be the same size";
287 unless (@$positions >= 2) {
288 $Imager::ERRSTR = "not enough segments";
292 for my $i (0.. $#$colors-1) {
293 $f->add(start=>$positions->[$i], end=>$positions->[$i+1],
294 c0 => $colors->[$i], c1=>$colors->[$i+1]);
299 warn "Nothing to do";
306 =head2 Implementation Functions
308 Documented for internal use.
312 =item _load_gimp_gradient($class, $fh, $name)
314 Does the work of loading a GIMP gradient file.
318 sub _load_gimp_gradient {
319 my ($class, $fh, $filename, $name) = @_;
323 unless ($head eq 'GIMP Gradient') {
324 $Imager::ERRSTR = "$filename is not a GIMP gradient file";
329 if ($count =~ /^name:\s?(.*)/i) {
330 ref $name and $$name = $1;
331 $count = <$fh>; # try again
334 unless ($count =~ /^\d+$/) {
335 $Imager::ERRSTR = "$filename is missing the segment count";
339 for my $i (1..$count) {
342 my @row = split ' ', $row;
343 unless (@row == 13) {
344 $Imager::ERRSTR = "Bad segment definition";
347 my ($start, $middle, $end) = splice(@row, 0, 3);
348 my $c0 = Imager::Color::Float->new(splice(@row, 0, 4));
349 my $c1 = Imager::Color::Float->new(splice(@row, 0, 4));
350 my ($type, $color) = @row;
351 push(@result, [ $start, $middle, $end, $c0, $c1, $type, $color ]);
353 return bless \@result,
356 =item _save_gimp_gradient($self, $fh, $name)
358 Does the work of saving to a GIMP gradient file.
362 sub _save_gimp_gradient {
363 my ($self, $fh, $filename, $name) = @_;
365 print $fh "GIMP Gradient\n";
366 defined $name or $name = '';
367 $name =~ tr/ -~/ /cds;
369 print $fh "Name: $name\n";
371 print $fh scalar(@$self),"\n";
372 for my $row (@$self) {
373 printf $fh "%.6f %.6f %.6f ",@{$row}[0..2];
375 for ($row->[3+$i]->rgba) {
376 printf $fh "%.6f ", $_/255.0;
379 print $fh "@{$row}[5,6]";
380 unless (print $fh "\n") {
381 $Imager::ERRSTR = "write error: $!";
391 =head1 FILL PARAMETER
393 The add() documentation mentions a fill parameter in a few places,
394 this is as good a place as any to discuss it.
396 The process of deciding the color produced by the gradient works
397 through the following steps:
403 calculate the base value, which is typically a distance or an angle of
404 some sort. This can be positive or occasionally negative, depending on
405 the type of fill being performed (linear, radial, etc).
409 clamp or convert the base value to the range 0 through 1, how this is
410 done depends on the repeat parameter. I'm calling this result the
415 the appropriate segment is found. This is currently done with a
416 linear search, and the first matching segment is used. If there is no
417 matching segment the pixel is not touched.
421 the fill parameter is scaled from 0 to 1 depending on the segment type.
425 the color produced, depending on the segment color type.
431 Tony Cook <tony@develop-help.com>