termcol: support multiple images
[sheet.git] / termcol.plp
index 748625cae0636f5b67ec90a2405fb3b72a4873ae..affceb936df03e1c53667f8e17b18c7ead2ec4cc 100644 (file)
@@ -14,7 +14,8 @@ Html({
        stylesheet => [qw'light dark'],
 });
 
-my $imgfile = $get{img} // exists $get{img} && 'indi.png';
+my @draw = map { [$_, s/\W+\z//] } grep { $_ } split m(/),
+       $get{img} // exists $get{img} && 'indi.png';
 
 my @termlist;
 push @termlist, split /\W+/, $ENV{PATH_INFO} || 'default';
@@ -73,15 +74,17 @@ sub colcell {
 }
 
 sub img_egapal {
-       my ($palette) = @_;
+       my ($palette, $imgfile, $reindex) = @_;
        return eval {
                require Imager;
                require MIME::Base64;
 
                my @imgpal = map { Imager::Color->new(ref $_ ? @$_ : $_) } @{$palette};
-               state $reindex = $imgfile =~ s/!$//;
-               state $img = Imager->new(file => "data/palimage/$imgfile")
+               state $imgcache = {};
+               my $img = $imgcache->{$imgfile}
+                       //= Imager->new(file => "data/palimage/$imgfile")
                        or die Imager->errstr.$/;
+
                do {
                        if ($reindex) {
                                $img->to_paletted({
@@ -138,7 +141,7 @@ for my $term (@termlist) {
                        print '<tr>', colcell($name, $rgb);
                }
 
-               print '<tr><td>', img_egapal(\@{$colours}) if $imgfile;
+               print '<tr><td>', img_egapal(\@{$colours}, @{$_}) for @draw;
                print "</table>\n\n";
        }
 }
@@ -199,10 +202,10 @@ sub coltable_hsv {
                }
        }
 
-       if ($imgfile) {
+       if (@draw) {
                my @palette = map { [ @{$_}[1 .. 3] ] } @greymap, map {@$_} map {@$_} @colmap;
-               my $imgdata = img_egapal(\@palette);
                my $tablespan = scalar map { @$_ } @colmap;
+               my $imgdata = img_egapal(\@palette, @{ $draw[0] });
                $out .= "<tr><td colspan=$tablespan>$imgdata";
        }