termcol: define arne's cpc-derived palette "cpcboy"
[sheet.git] / termcol.plp
index 73bf0d2190d5f3132b8ecd3e1ff70cdc65cf7642..267d5a74a7821f4bfd5a54aba6c420a0c2b55589 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);
        } || $@;
@@ -139,9 +151,8 @@ sub coltable_hsv {
        my $smax = $dim - 1;
        $rgbval ||= sub { join('', @_), map { int $_ * 255 / $vmax } @_ };
 
-       my %greymap;  # name => value
+       my @greymap = @{$greyramp || []};  # [name, r, g=l, b]
        my @colmap;  # saturation => value => hue => [name, r,g,b]
-       my $offset = 16 * ($dim > 3);
 
        for my $r (0 .. $dim - 1) {
                for my $g (0 .. $dim - 1) {
@@ -153,9 +164,8 @@ sub coltable_hsv {
                                my $s = abs(min(@rgb) - max(@rgb));
 
                                if (!$s) {
-                                       if ($greyramp) {
-                                               my ($index, $l) = $rgbval->(@rgb);
-                                               $greymap{$index} = $l;
+                                       if (@greymap) {
+                                               push @greymap, [ $rgbval->(@rgb) ];
                                                next;
                                        }
 
@@ -180,23 +190,18 @@ sub coltable_hsv {
                $out .= colcell(@$_) for map { $_->[$h] } map { @{$_} } @colmap;
        }
 
-       if ($greyramp) {
-               $offset += $dim ** 3;
-               $greymap{$offset++} = $_ for @{$greyramp};
-       }
-
-       if (%greymap) {
+       if (@greymap) {
                $out .= '<tbody>';
                my $col = 0;
                my $colbreak = scalar map { @$_ } @colmap;  # same width as hue rows
-               for my $num (sort { $greymap{$a} <=> $greymap{$b} } keys %greymap) {
+               for my $cell (sort { $a->[1] <=> $b->[1] || $a->[0] <=> $b->[0] } @greymap) {
                        $out .= '<tr>' unless $col++ % $colbreak;
-                       $out .= colcell($num, ($greymap{$num}));
+                       $out .= colcell(@{$cell});
                }
        }
 
        if ($imgfile) {
-               my @palette = map { [ @{$_}[1 .. 3] ] } map {@$_} map {@$_} @colmap;
+               my @palette = map { [ @{$_}[1 .. 3] ] } @greymap, map {@$_} map {@$_} @colmap;
                my $imgdata = img_egapal(\@palette);
                my $tablespan = scalar map { @$_ } @colmap;
                $out .= "<tr><td colspan=$tablespan>$imgdata";