termcol: lay out 256-colour table by h/s/v
[sheet.git] / termcol.plp
index 1e5e2870cea46240c538919fe1416bf0222c6680..16b3dfeb77d4ee8e5ac8f2b594659f40b6d300ca 100644 (file)
@@ -28,10 +28,11 @@ print
 
 <div class="section">
 <:
-use Shiar_Sheet::Colour '1.01';
+use Shiar_Sheet::Colour '1.02';
+use List::Util qw( min max );
 
 sub colcell {
-       my $name = shift;
+       my $name = shift or return "<td colspan=3>\n";
        my $col = Shiar_Sheet::Colour->new(@_);
        my $minhex = $col->rgb24;
        my $css     = '#' . $col->rgb48;
@@ -251,15 +252,40 @@ print "</table>\n\n";
 <div class="section">
 <h2>256-colour space</h2>
 <:
+my @colmap;  # saturation => value => hue => colcell
 for my $r (0 .. 5) {
-       print '<table>';
        for my $g (0 .. 5) {
-               print '<tr>';
                for my $b (0 .. 5) {
                        my $index = $r*6*6 + $g*6 + $b + 16;
-                       print colcell($index, map { $_ && $_*40 + 55 } $r, $g, $b);
+                       my @rgb = map { $_ && $_*40 + 55 } $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;
+
+                       $h-- for grep {$h >= $_} 4, 6, 16, 19, 28, 31;
+                       $v = 5 - $v;
+                       $s = 7 - $s - $v;
+                       $s-- if $s;
+
+                       if ($grey) {
+                               $h = 30;  # greyscale hue
+                               $s -= 2;  # lowest saturation for other hues
+                               $v = $s = 0 if $s < 0;  # black at full saturation
+                       }
+
+                       $colmap[$s][$v][$h] = [$index, @rgb];
                }
        }
+}
+
+{
+       print '<table>';
+       for my $h (0 .. 30) {
+               print '<tr>';
+               print colcell(@$_) for map { $_->[$h] } map { @{$_} } @colmap;
+       }
        print "</table>\n";
 }
 print "\n";