termcol: reindex option to replace colours
authorMischa POSLAWSKY <perl@shiar.org>
Mon, 2 Nov 2015 06:02:56 +0000 (07:02 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Tue, 5 Jan 2016 00:05:07 +0000 (01:05 +0100)
Append exclamation mark to img name to compare incompatible palette( size)s.

termcol.plp

index 73bf0d2..7a51fbd 100644 (file)
@@ -79,12 +79,24 @@ sub img_egapal {
                require MIME::Base64;
 
                my @imgpal = map { Imager::Color->new(ref $_ ? @$_ : $_) } @{$palette};
-               state $img = Imager->new(file => "data/$imgfile")
+               state $reindex = $imgfile =~ s/!$//;
+               state $img = Imager->new(file => "data/palimage/$imgfile")
                        or die Imager->errstr.$/;
-               @{[ $img->getcolors ]} == @imgpal
-                       or die "incompatible palette size\n";
-               $img->setcolors(colors => \@imgpal);
-               $img->write(data => \my $imgdata, type => 'png');
+               do {
+                       if ($reindex) {
+                               $img->to_paletted({
+                                       make_colors => 'none',
+                                       colors => \@imgpal,
+                                       translate => 'closest',
+                               });
+                       }
+                       else {
+                               @{[ $img->getcolors ]} == @imgpal
+                                       or die "incompatible palette size\n";
+                               $img->setcolors(colors => \@imgpal);
+                               $img;
+                       }
+               }->write(data => \my $imgdata, type => 'png');
                return sprintf '<img src="data:image/png;base64,%s">',
                        MIME::Base64::encode_base64($imgdata);
        } || $@;