termcol: avoid plp code breaks for hsv tables
[sheet.git] / termcol.plp
index 16b3dfeb77d4ee8e5ac8f2b594659f40b6d300ca..9262bf35c64a090ccf9e1c8b32c70540060c9893 100644 (file)
@@ -28,11 +28,11 @@ print
 
 <div class="section">
 <:
-use Shiar_Sheet::Colour '1.02';
+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,79 +226,79 @@ for my $term (@termlist) {
 <hr>
 
 <div class="section">
-<h2>88-colour space</h2>
 <:
-for my $r (0 .. 3) {
-       print '<table>';
-       for my $g (0 .. 3) {
-               print '<tr>';
-               for my $b (0 .. 3) {
-                       my $index = $r*4*4 + $g*4 + $b + 16;
-                       print colcell($index, map {(0, 139, 205, 255)[$_]} $r, $g, $b);
-               }
-       }
-       print "</table>\n";
-}
-print "\n";
+sub coltable_hsv {
+       my ($dim, $rgbval, $greyramp) = @_;
 
-print '<table><tr>';
-print colcell(80+$_, (($_ + 2 + ($_>0)) * 255/11) x 3) for 0 .. 7;
-print "</table>\n\n";
+       my $hmax = 2 * $dim * 3;  # each face of the rgb cube
+       my $vmax = $dim - 1;
+       my $smax = $dim - 1;
 
-:></div>
-<: if ($ENV{PATH_INFO} =~ /256/) { :>
-<hr>
+       my %greymap;  # name => value
+       my @colmap;  # saturation => value => hue => [name, r,g,b]
+       my $offset = 16;
 
-<div class="section">
-<h2>256-colour space</h2>
-<:
-my @colmap;  # saturation => value => hue => colcell
-for my $r (0 .. 5) {
-       for my $g (0 .. 5) {
-               for my $b (0 .. 5) {
-                       my $index = $r*6*6 + $g*6 + $b + 16;
-                       my @rgb = map { $_ && $_*40 + 55 } $r, $g, $b;
+       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 * 35;
-                       my $v = int(max(@rgb) / 255 * 5);
-                       my $s = abs(min(@rgb) - max(@rgb)) / 255 * 7;
-                       my $grey = !$s;
+                               my $h = Shiar_Sheet::Colour->new(@rgb)->hue * $hmax;
+                               my $v = max(@rgb);
+                               my $s = abs(min(@rgb) - max(@rgb));
 
-                       $h-- for grep {$h >= $_} 4, 6, 16, 19, 28, 31;
-                       $v = 5 - $v;
-                       $s = 7 - $s - $v;
-                       $s-- if $s;
+                               if (!$s) {
+                                       $greymap{$index} = $rgbval->($r);
+                                       next;
+                               }
 
-                       if ($grey) {
-                               $h = 30;  # greyscale hue
-                               $s -= 2;  # lowest saturation for other hues
-                               $v = $s = 0 if $s < 0;  # black at full saturation
-                       }
+                               $v = $vmax - $v;
+                               $s = $smax - $s - $v;
 
-                       $colmap[$s][$v][$h] = [$index, @rgb];
+                               $colmap[$s][$v][$h] = [$index, map { $rgbval->($_) } @rgb];
+                       }
                }
        }
-}
 
-{
-       print '<table>';
-       for my $h (0 .. 30) {
-               print '<tr>';
-               print colcell(@$_) for map { $_->[$h] } map { @{$_} } @colmap;
+       my $out = '<table class=mapped>';
+       $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;
        }
-       print "</table>\n";
+
+       $offset += $dim ** 3;
+       $greymap{$offset++} = $_ for @{$greyramp};
+
+       $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) {
+               $out .= '<tr>' unless $col++ % $colbreak;
+               $out .= colcell($num, ($greymap{$num}) x 3);
+       }
+       $out .= "</table>\n\n";
+
+       return $out;
 }
-print "\n";
 
-       print '<table>';
-for my $row (0, 12) {
-       print '<tr>';
-       print colcell(232+$_, ($_*10 + 8) x 3) for $row .. $row+11;
+{
+       print "<h2>88-colour space</h2>\n";
+       print coltable_hsv(4,
+               sub {(0, 139, 205, 255)[$_[0]]},
+               [map { ($_ + 2 + ($_>0)) * 255/11 } 0 .. 7],
+       );
 }
-       print "</table>\n";
-print "\n";
 
+if ($ENV{PATH_INFO} =~ /256/) {
+       print "<h2>256-colour space</h2>\n";
+       print coltable_hsv(6,
+               sub { $_[0] && $_[0]*40 + 55 },
+               [ map { $_ * 10 + 8 } 0 .. 23 ],
+       );
+}
 :></div>
-<: } :>
 <hr>