termcol: define palette label in rgb value subroutine
[sheet.git] / termcol.plp
index 18593abd76f84aca8323ab7685accd2cfbd7ccd0..04b6e6e64533f5cad2adedee7531950fd0fff397 100644 (file)
@@ -32,7 +32,7 @@ use Shiar_Sheet::Colour '1.03';
 use List::Util qw( min max );
 
 sub colcell {
-       my $name = shift or return "<td colspan=3>\n";
+       my $name = shift // return "<td colspan=3>\n";
        my $col = Shiar_Sheet::Colour->new(@_);
        my $minhex = $col->rgb24;
        my $css     = '#' . $col->rgb48;
@@ -226,26 +226,15 @@ for my $term (@termlist) {
 <hr>
 
 <div class="section">
-<h2>88-colour space</h2>
-<:
-print coltable_hsv(4,
-       sub {(0, 139, 205, 255)[$_]},
-       [map { ($_ + 2 + ($_>0)) * 255/11 } 0 .. 7],
-);
-
-:></div>
-<: if ($ENV{PATH_INFO} =~ /256/) { :>
-<hr>
-
-<div class="section">
-<h2>256-colour space</h2>
 <:
 sub coltable_hsv {
        my ($dim, $rgbval, $greyramp) = @_;
 
-       my $hmax = ($dim + $dim - 1) * 2 + ($dim - 2) * 2 + 3;
+       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]
@@ -254,55 +243,73 @@ sub coltable_hsv {
        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 = map { &$rgbval } $r, $g, $b;
+                               my @rgb = ($r, $g, $b);
 
                                my $h = Shiar_Sheet::Colour->new(@rgb)->hue * $hmax;
-                               my $v = int(max(@rgb) / 255 * $vmax);
-                               my $s = abs(min(@rgb) - max(@rgb)) / 255 * $smax;
+                               my $v = max(@rgb);
+                               my $s = abs(min(@rgb) - max(@rgb));
 
                                if (!$s) {
-                                       $greymap{$index} = $rgb[0];
+                                       my ($index, $l) = $rgbval->(@rgb);
+                                       $greymap{$index} = $l;
                                        next;
                                }
 
                                $v = $vmax - $v;
                                $s = $smax - $s - $v;
 
-                               $colmap[$s][$v][$h] = [$index, @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}) {
                $out .= '<tr>';
                $out .= colcell(@$_) for map { $_->[$h] } map { @{$_} } @colmap;
        }
-       $out .= "</table>\n\n";
 
        $offset += $dim ** 3;
        $greymap{$offset++} = $_ for @{$greyramp};
 
-       $out .= '<table>';
+       $out .= '<tbody>';
        my $col = 0;
-       my $colbreak = keys(%greymap) / 2;  # split into 2 rows
+       my $colbreak = scalar map { @$_ } @colmap;  # same width as hue rows
        for my $num (sort { $greymap{$a} <=> $greymap{$b} } keys %greymap) {
                $out .= '<tr>' unless $col++ % $colbreak;
                $out .= colcell($num, ($greymap{$num}) x 3);
        }
-       $out .= "</table>\n\n";
 
        return $out;
 }
 
-print coltable_hsv(6,
-       sub { $_ && $_*40 + 55 },
-       [ map { $_ * 10 + 8 } 0 .. 23 ],
-);
+{
+       print "<h2>88-colour space</h2>\n";
+       print "<table class=mapped>\n";
+       print coltable_hsv(4,
+               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 {
+                       $_[2] + 6 * ($_[1] + 6 * $_[0]) + 16,
+                       map { $_ && $_*40 + 55 } @_
+               },
+               [ map { $_ * 10 + 8 } 0 .. 23 ],
+       );
+       print "</table>\n";
+}
 :></div>
-<: } :>
 <hr>