auto-convert the "other" parameter for opacity fills
authorTony Cook <tony@develop=help.com>
Fri, 6 Nov 2009 10:00:31 +0000 (10:00 +0000)
committerTony Cook <tony@develop=help.com>
Fri, 6 Nov 2009 10:00:31 +0000 (10:00 +0000)
lib/Imager/Fill.pm
t/t20fill.t

index 5e898268586eae5d17e1baaddd1dec3d32c69591..aca9c56d89502772a572ac111e148df97e4c680f 100644 (file)
@@ -131,9 +131,20 @@ sub new {
       Imager->_set_error("'other' parameter required to create alpha fill");
       return;
     }
-    unless (eval { $other_fill->isa("Imager::Fill") }) {
-      Imager->_set_error("'other' parameter must be an Imager::Fill object to create an alpha fill");
-      return;
+    unless (ref $other_fill &&
+           eval { $other_fill->isa("Imager::Fill") }) {
+      # try to auto convert to a fill object
+      if (ref $other_fill && $other_fill =~ /HASH/) {
+       $other_fill = Imager::Fill->new(%$other_fill)
+         or return;
+      }
+      else {
+       undef $other_fill;
+      }
+      unless ($other_fill) {
+       Imager->_set_error("'other' parameter must be an Imager::Fill object to create an alpha fill");
+       return;
+      }
     }
 
     my $raw_fill = $other_fill->{fill};
index 0b2118d903011059ffb945eeb3d017decbcf800f..0eee844fc3ef06fd83e08a8e1dc082a0d595c319 100644 (file)
@@ -1,6 +1,6 @@
 #!perl -w
 use strict;
-use Test::More tests => 143;
+use Test::More tests => 148;
 
 use Imager ':handy';
 use Imager::Fill;
@@ -566,6 +566,19 @@ SKIP:
     is_fcolor4($out->getpixel(x => 6, y => 3, type => "float"),
              0, 0.25, 0, 1, "check drawn against background at 6,3");
   }
+  ok(!Imager::Fill->new(type => "opacity"),
+     "should fail to make an opacity fill with no other fill object");
+  is(Imager->errstr, "'other' parameter required to create alpha fill",
+     "check error message");
+  ok(!Imager::Fill->new(type => "opacity", other => "xx"),
+     "should fail to make an opacity fill with a bad other parameter");
+  is(Imager->errstr, "'other' parameter must be an Imager::Fill object to create an alpha fill", 
+        "check error message");
+
+  # check auto conversion of hashes
+  ok(Imager::Fill->new(type => "opacity", other => { solid => "FF0000" }),
+     "check we auto-create fills")
+    or print "# ", Imager->errstr, "\n";
 }
 
 sub color_close {