termcol: define mirc palette (included in more)
[sheet.git] / termcol.plp
index 73bf0d2190d5f3132b8ecd3e1ff70cdc65cf7642..0068accf7edb69860d668058c3354980733a2819 100644 (file)
@@ -21,7 +21,7 @@ push @termlist, split /\W+/, $ENV{PATH_INFO} || 'default';
 
 my %termgroup = (
        default => [qw( ansi xkcd ansi88 )],
-       more    => [qw( ansi legacy ansi256 )],
+       more    => [qw( ansi mirc legacy ansi256 )],
        msx     => [qw( msx1 msx2 arnejmp )],
        ansi    => [qw( cga xterm tango app html )],
        legacy  => [qw( c64 msx2 mac2 risc arnegame cpc )],
@@ -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);
        } || $@;