From 1f3b44e51107dc97ce2bded07b93325e96835744 Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Sat, 31 Oct 2015 20:43:02 +0100 Subject: [PATCH] termcol: lay out 256-colour table by h/s/v --- Shiar_Sheet/Colour.pm | 10 +++++++++- termcol.plp | 36 +++++++++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/Shiar_Sheet/Colour.pm b/Shiar_Sheet/Colour.pm index 7ea68e9..cedfdfb 100644 --- a/Shiar_Sheet/Colour.pm +++ b/Shiar_Sheet/Colour.pm @@ -4,13 +4,15 @@ use strict; 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; +my $PI = 2 * atan2 1, 0; + 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; } +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] }; diff --git a/termcol.plp b/termcol.plp index 1e5e287..16b3dfe 100644 --- a/termcol.plp +++ b/termcol.plp @@ -28,10 +28,11 @@ print
<: -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 "\n"; my $col = Shiar_Sheet::Colour->new(@_); my $minhex = $col->rgb24; my $css = '#' . $col->rgb48; @@ -251,15 +252,40 @@ print "\n\n";

256-colour space

<: +my @colmap; # saturation => value => hue => colcell for my $r (0 .. 5) { - print ''; for my $g (0 .. 5) { - print ''; 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 '
'; + for my $h (0 .. 30) { + print ''; + print colcell(@$_) for map { $_->[$h] } map { @{$_} } @colmap; + } print "
\n"; } print "\n"; -- 2.30.0