termcol: amstrad cpc palette (27-colour hsv)
[sheet.git] / termcol.plp
index 9262bf35c64a090ccf9e1c8b32c70540060c9893..4f97abae5cbdb0bc44bc0807c62e6ba5d9818b08 100644 (file)
@@ -221,6 +221,16 @@ for my $term (@termlist) {
        }
        print "</table>\n\n";
 }
+
+if (exists $get{v}) {
+       print "<table class=mapped>\n";
+       print "<caption>Amstrad CPC</caption>\n";
+       print coltable_hsv(3, sub {
+               $_[2] + 3 * ($_[0] + 3 * $_[1]),
+               map { $_ && $_ * 127 + 1 } @_
+       });
+       print "</table>\n\n";
+}
 :></div>
 
 <hr>
@@ -233,15 +243,16 @@ sub coltable_hsv {
        my $hmax = 2 * $dim * 3;  # each face of the rgb cube
        my $vmax = $dim - 1;
        my $smax = $dim - 1;
+       $rgbval ||= sub { join('', @_), map { int $_ * 255 / $vmax } @_ };
+       $greyramp ||= [];
 
        my %greymap;  # name => value
        my @colmap;  # saturation => value => hue => [name, r,g,b]
-       my $offset = 16;
+       my $offset = 16 * ($dim > 3);
 
        for my $r (0 .. $dim - 1) {
                for my $g (0 .. $dim - 1) {
                        for my $b (0 .. $dim - 1) {
-                               my $index = $r*$dim*$dim + $g*$dim + $b + $offset;
                                my @rgb = ($r, $g, $b);
 
                                my $h = Shiar_Sheet::Colour->new(@rgb)->hue * $hmax;
@@ -249,19 +260,20 @@ sub coltable_hsv {
                                my $s = abs(min(@rgb) - max(@rgb));
 
                                if (!$s) {
-                                       $greymap{$index} = $rgbval->($r);
+                                       my ($index, $l) = $rgbval->(@rgb);
+                                       $greymap{$index} = $l;
                                        next;
                                }
 
                                $v = $vmax - $v;
                                $s = $smax - $s - $v;
 
-                               $colmap[$s][$v][$h] = [$index, map { $rgbval->($_) } @rgb];
+                               $colmap[$s][$v][$h] = [ $rgbval->(@rgb) ];
                        }
                }
        }
 
-       my $out = '<table class=mapped>';
+       my $out = '';
        $out .= sprintf '<colgroup span=%d>', 3 * @{$_} for @colmap;
        my $huerow = $colmap[0][0]; # first {$_} map { @{$_} } @colmap;
        for my $h (grep { $huerow->[$_] } 0 .. $#{$huerow}) {
@@ -279,25 +291,34 @@ sub coltable_hsv {
                $out .= '<tr>' unless $col++ % $colbreak;
                $out .= colcell($num, ($greymap{$num}) x 3);
        }
-       $out .= "</table>\n\n";
 
        return $out;
 }
 
 {
        print "<h2>88-colour space</h2>\n";
+       print "<table class=mapped>\n";
        print coltable_hsv(4,
-               sub {(0, 139, 205, 255)[$_[0]]},
+               sub {
+                       $_[2] + 4 * ($_[1] + 4 * $_[0]) + 16,
+                       map { (0, 139, 205, 255)[$_] } @_
+               },
                [map { ($_ + 2 + ($_>0)) * 255/11 } 0 .. 7],
        );
+       print "</table>\n";
 }
 
 if ($ENV{PATH_INFO} =~ /256/) {
        print "<h2>256-colour space</h2>\n";
+       print "<table class=mapped>\n";
        print coltable_hsv(6,
-               sub { $_[0] && $_[0]*40 + 55 },
+               sub {
+                       $_[2] + 6 * ($_[1] + 6 * $_[0]) + 16,
+                       map { $_ && $_*40 + 55 } @_
+               },
                [ map { $_ * 10 + 8 } 0 .. 23 ],
        );
+       print "</table>\n";
 }
 :></div>
 <hr>