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;
<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 => [name, r,g,b]
-my %greymap; # name => value
-$greymap{232 + $_} = $_*10 + 8 for 0 .. 23;
+ 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);
-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 * $hmax;
+ my $v = max(@rgb);
+ my $s = abs(min(@rgb) - max(@rgb));
- 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;
+ if (!$s) {
+ $greymap{$index} = $rgbval->($r);
+ next;
+ }
- if (!$s) {
- $greymap{$index} = $rgb[0];
- next;
+ $v = $vmax - $v;
+ $s = $smax - $s - $v;
+
+ $colmap[$s][$v][$h] = [$index, map { $rgbval->($_) } @rgb];
}
+ }
+ }
- $v = 5 - $v;
- $s = 7 - $s - $v;
- $s-- if $s;
+ 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;
+ }
- $colmap[$s][$v][$h] = [$index, @rgb];
- }
+ $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 '<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;
- }
- print "</table>\n";
+ 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 "\n";
- print '<table>';
-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);
+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 ],
+ );
}
- print "</table>\n";
-print "\n";
-
:></div>
-<: } :>
<hr>