git.shiar.nl
/
sheet.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
termcol: support multiple images
[sheet.git]
/
termcol.plp
diff --git
a/termcol.plp
b/termcol.plp
index 0068accf7edb69860d668058c3354980733a2819..affceb936df03e1c53667f8e17b18c7ead2ec4cc 100644
(file)
--- a/
termcol.plp
+++ b/
termcol.plp
@@
-14,7
+14,8
@@
Html({
stylesheet => [qw'light dark'],
});
stylesheet => [qw'light dark'],
});
-my $imgfile = $get{img} // exists $get{img} && 'indi.png';
+my @draw = map { [$_, s/\W+\z//] } grep { $_ } split m(/),
+ $get{img} // exists $get{img} && 'indi.png';
my @termlist;
push @termlist, split /\W+/, $ENV{PATH_INFO} || 'default';
my @termlist;
push @termlist, split /\W+/, $ENV{PATH_INFO} || 'default';
@@
-45,7
+46,7
@@
print
<div class="section">
<:
use 5.010;
<div class="section">
<:
use 5.010;
-use Shiar_Sheet::Colour '1.0
3
';
+use Shiar_Sheet::Colour '1.0
4
';
use List::Util qw( min max );
my $palettes = do 'termcol.inc.pl';
use List::Util qw( min max );
my $palettes = do 'termcol.inc.pl';
@@
-73,15
+74,17
@@
sub colcell {
}
sub img_egapal {
}
sub img_egapal {
- my ($palette) = @_;
+ my ($palette
, $imgfile, $reindex
) = @_;
return eval {
require Imager;
require MIME::Base64;
my @imgpal = map { Imager::Color->new(ref $_ ? @$_ : $_) } @{$palette};
return eval {
require Imager;
require MIME::Base64;
my @imgpal = map { Imager::Color->new(ref $_ ? @$_ : $_) } @{$palette};
- state $reindex = $imgfile =~ s/!$//;
- state $img = Imager->new(file => "data/palimage/$imgfile")
+ state $imgcache = {};
+ my $img = $imgcache->{$imgfile}
+ //= Imager->new(file => "data/palimage/$imgfile")
or die Imager->errstr.$/;
or die Imager->errstr.$/;
+
do {
if ($reindex) {
$img->to_paletted({
do {
if ($reindex) {
$img->to_paletted({
@@
-138,7
+141,7
@@
for my $term (@termlist) {
print '<tr>', colcell($name, $rgb);
}
print '<tr>', colcell($name, $rgb);
}
- print '<tr><td>', img_egapal(\@{$colours}
) if $imgfile
;
+ print '<tr><td>', img_egapal(\@{$colours}
, @{$_}) for @draw
;
print "</table>\n\n";
}
}
print "</table>\n\n";
}
}
@@
-151,31
+154,28
@@
sub coltable_hsv {
my $smax = $dim - 1;
$rgbval ||= sub { join('', @_), map { int $_ * 255 / $vmax } @_ };
my $smax = $dim - 1;
$rgbval ||= sub { join('', @_), map { int $_ * 255 / $vmax } @_ };
- my
%greymap; # name => value
+ my
@greymap = @{$greyramp || []}; # [name, r, g=l, b]
my @colmap; # saturation => value => hue => [name, r,g,b]
my @colmap; # saturation => value => hue => [name, r,g,b]
- my $offset = 16 * ($dim > 3);
for my $r (0 .. $dim - 1) {
for my $g (0 .. $dim - 1) {
for my $b (0 .. $dim - 1) {
my @rgb = ($r, $g, $b);
for my $r (0 .. $dim - 1) {
for my $g (0 .. $dim - 1) {
for my $b (0 .. $dim - 1) {
my @rgb = ($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, $s, $v) = Shiar_Sheet::Colour->new(@rgb)->hsv;
if (!$s) {
if (!$s) {
- if ($greyramp) {
- my ($index, $l) = $rgbval->(@rgb);
- $greymap{$index} = $l;
+ if (@greymap) {
+ push @greymap, [ $rgbval->(@rgb) ];
next;
}
next;
}
- $h =
$hmax
; # greyscale hue
+ $h =
1
; # greyscale hue
$s = 1; # lowest saturation for other hues
$v = $s = $vmax if !$v; # black at full saturation
}
$s = 1; # lowest saturation for other hues
$v = $s = $vmax if !$v; # black at full saturation
}
+ $h *= $hmax;
$v = $vmax - $v;
$s = $smax - $s - $v;
$v = $vmax - $v;
$s = $smax - $s - $v;
@@
-192,25
+192,20
@@
sub coltable_hsv {
$out .= colcell(@$_) for map { $_->[$h] } map { @{$_} } @colmap;
}
$out .= colcell(@$_) for map { $_->[$h] } map { @{$_} } @colmap;
}
- if ($greyramp) {
- $offset += $dim ** 3;
- $greymap{$offset++} = $_ for @{$greyramp};
- }
-
- if (%greymap) {
+ if (@greymap) {
$out .= '<tbody>';
my $col = 0;
my $colbreak = scalar map { @$_ } @colmap; # same width as hue rows
$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) {
+ for my $
cell (sort { $a->[1] <=> $b->[1] || $a->[0] <=> $b->[0] } @
greymap) {
$out .= '<tr>' unless $col++ % $colbreak;
$out .= '<tr>' unless $col++ % $colbreak;
- $out .= colcell(
$num, ($greymap{$num})
);
+ $out .= colcell(
@{$cell}
);
}
}
}
}
- if ($imgfile) {
- my @palette = map { [ @{$_}[1 .. 3] ] } map {@$_} map {@$_} @colmap;
- my $imgdata = img_egapal(\@palette);
+ if (@draw) {
+ my @palette = map { [ @{$_}[1 .. 3] ] } @greymap, map {@$_} map {@$_} @colmap;
my $tablespan = scalar map { @$_ } @colmap;
my $tablespan = scalar map { @$_ } @colmap;
+ my $imgdata = img_egapal(\@palette, @{ $draw[0] });
$out .= "<tr><td colspan=$tablespan>$imgdata";
}
$out .= "<tr><td colspan=$tablespan>$imgdata";
}