termcol: index greyscale ramp, include in image palette
[sheet.git] / termcol.plp
index 7a51fbd001b2f1dbcd8a11a1eb9cc55be2d9b395..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 )],
@@ -151,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) {
@@ -165,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;
                                        }
 
@@ -192,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";