termcol: unsaturated colours from h/s/v table in greyscale ramp
[sheet.git] / termcol.plp
index 16b3dfeb77d4ee8e5ac8f2b594659f40b6d300ca..b5f08b250be523a47c0551acdf258d1bb62fec10 100644 (file)
@@ -28,7 +28,7 @@ print
 
 <div class="section">
 <:
-use Shiar_Sheet::Colour '1.02';
+use Shiar_Sheet::Colour '1.03';
 use List::Util qw( min max );
 
 sub colcell {
@@ -252,37 +252,38 @@ print "</table>\n\n";
 <div class="section">
 <h2>256-colour space</h2>
 <:
-my @colmap;  # saturation => value => hue => colcell
+my @colmap;  # saturation => value => hue => [name, r,g,b]
+my %greymap;  # name => value
+$greymap{232 + $_} = $_*10 + 8 for 0 .. 23;
+
 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;
 
-                       my $h = Shiar_Sheet::Colour->new(@rgb)->hue * 35;
+                       my $h = Shiar_Sheet::Colour->new(@rgb)->hue * 33;
                        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;
+                       if (!$s) {
+                               $greymap{$index} = $rgb[0];
+                               next;
+                       }
+
                        $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 '<table class=mapped>';
+       printf '<colgroup span=%d>', 3 * @{$_} for @colmap;
+       for my $h (grep { $colmap[0][0][$_] } 0 .. $#{ $colmap[0][0] }) {
                print '<tr>';
                print colcell(@$_) for map { $_->[$h] } map { @{$_} } @colmap;
        }
@@ -291,9 +292,11 @@ for my $r (0 .. 5) {
 print "\n";
 
        print '<table>';
-for my $row (0, 12) {
-       print '<tr>';
-       print colcell(232+$_, ($_*10 + 8) x 3) for $row .. $row+11;
+my $col = 0;
+my $colbreak = keys(%greymap) / 2;  # split into 2 rows
+for my $num (sort { $greymap{$a} <=> $greymap{$b} } keys %greymap) {
+       print '<tr>' unless $col++ % $colbreak;
+       print colcell($num, ($greymap{$num}) x 3);
 }
        print "</table>\n";
 print "\n";