termcol: float text samples in cells
[sheet.git] / termcol.plp
index e544bf700262ceb10e263a0d11f5d52be024f9f6..0b3d2364a11ed39824afd06ca979833a12ea0f1f 100644 (file)
@@ -49,7 +49,7 @@ my $palettes = do 'termcol.inc.pl';
 die "Cannot open palette data: $_\n" for $@ || $! || ();
 
 sub colcell {
-       my $name = shift // return "<td colspan=3>\n";
+       my $name = shift // return "<td>\n";
        my $col = Shiar_Sheet::Colour->new(@_);
        my $minhex = $col->rgb24;
        my $css     = '#' . $col->rgb48;
@@ -58,13 +58,14 @@ sub colcell {
        my $sample = [ qw(#000 #FFF) ];
        ($name, $sample) = @$name if ref $name eq 'ARRAY';
 
-       my $out = sprintf('<th title="%s" style="%s">%s',
+       my $out = sprintf('<td title="%s" style="%s">%s',
                join(',', map { int } @$col),
                "background:$css; color:$inverse",
                $name,
        );
-       $out .= sprintf '<td style="%s">%s', "background:$_; color:$css", $minhex
-               for @$sample;
+       $out .= sprintf('<samp style="%s"><small>%s</small></samp>',
+               "background:$_; color:$css", $minhex
+       ) for @$sample;
        return "$out\n";
 }
 
@@ -114,7 +115,6 @@ sub coltable_hsv {
        my $vmax = $dim - 1;
        my $smax = $dim - 1;
        $rgbval ||= sub { join('', @_), map { int $_ * 255 / $vmax } @_ };
-       $greyramp ||= [];
 
        my %greymap;  # name => value
        my @colmap;  # saturation => value => hue => [name, r,g,b]
@@ -130,9 +130,15 @@ sub coltable_hsv {
                                my $s = abs(min(@rgb) - max(@rgb));
 
                                if (!$s) {
-                                       my ($index, $l) = $rgbval->(@rgb);
-                                       $greymap{$index} = $l;
-                                       next;
+                                       if ($greyramp) {
+                                               my ($index, $l) = $rgbval->(@rgb);
+                                               $greymap{$index} = $l;
+                                               next;
+                                       }
+
+                                       $h = $hmax;  # greyscale hue
+                                       $s = 1;  # lowest saturation for other hues
+                                       $v = $s = $vmax if !$v;  # black at full saturation
                                }
 
                                $v = $vmax - $v;
@@ -144,22 +150,26 @@ sub coltable_hsv {
        }
 
        my $out = '';
-       $out .= sprintf '<colgroup span=%d>', 3 * @{$_} for @colmap;
+       $out .= sprintf '<colgroup span=%d>', scalar @{$_} 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;
        }
 
-       $offset += $dim ** 3;
-       $greymap{$offset++} = $_ for @{$greyramp};
+       if ($greyramp) {
+               $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);
+       if (%greymap) {
+               $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);
+               }
        }
 
        return $out;