termcol: lay out 256-colour table by h/s/v
authorMischa POSLAWSKY <perl@shiar.org>
Sat, 31 Oct 2015 19:43:02 +0000 (20:43 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Sat, 31 Oct 2015 21:01:32 +0000 (22:01 +0100)
Shiar_Sheet/Colour.pm
termcol.plp

index 7ea68e9dcf63bd3874facf8e8a89f068d52ba213..cedfdfb9e31aa722ff8027657fba1b97c455fb26 100644 (file)
@@ -4,13 +4,15 @@ use strict;
 use warnings;
 use List::Util qw( min max );
 
 use warnings;
 use List::Util qw( min max );
 
-our $VERSION = '1.01';
+our $VERSION = '1.02';
 
 # ITU-R recommendation 601 luma co-efficients
 our $kr = .299;
 our $kb = .114;
 our $kg = 1 - $kb - $kr;
 
 
 # ITU-R recommendation 601 luma co-efficients
 our $kr = .299;
 our $kb = .114;
 our $kg = 1 - $kb - $kr;
 
+my $PI = 2 * atan2 1, 0;
+
 sub new {
        my $class = shift;
        my @rgb = @_ >= 3 ? @_ : (map {hex} $_[0] =~ /(..)/g);
 sub new {
        my $class = shift;
        my @rgb = @_ >= 3 ? @_ : (map {hex} $_[0] =~ /(..)/g);
@@ -39,6 +41,12 @@ sub luminance {
        return $r*$kr + $g*$kg + $b*$kb;
 }
 
        return $r*$kr + $g*$kg + $b*$kb;
 }
 
+sub hue {
+       my ($r, $g, $b) = @{ $_[0] };
+       my $hue = atan2 sqrt(3) * ($g - $b), $r*2 - $g - $b;
+       return ($hue + $PI) / $PI / 2; # 0 .. 1
+}
+
 sub rgb24 {
        my $str = '';
        $str .= sprintf '%X', min($_ / 17 + .5, 15) for @{ $_[0] };
 sub rgb24 {
        my $str = '';
        $str .= sprintf '%X', min($_ / 17 + .5, 15) for @{ $_[0] };
index 1e5e2870cea46240c538919fe1416bf0222c6680..16b3dfeb77d4ee8e5ac8f2b594659f40b6d300ca 100644 (file)
@@ -28,10 +28,11 @@ print
 
 <div class="section">
 <:
 
 <div class="section">
 <:
-use Shiar_Sheet::Colour '1.01';
+use Shiar_Sheet::Colour '1.02';
+use List::Util qw( min max );
 
 sub colcell {
 
 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;
        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>
 <:
 <div class="section">
 <h2>256-colour space</h2>
 <:
+my @colmap;  # saturation => value => hue => colcell
 for my $r (0 .. 5) {
 for my $r (0 .. 5) {
-       print '<table>';
        for my $g (0 .. 5) {
        for my $g (0 .. 5) {
-               print '<tr>';
                for my $b (0 .. 5) {
                        my $index = $r*6*6 + $g*6 + $b + 16;
                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";
        print "</table>\n";
 }
 print "\n";