From 16fb9fc2233f3a3c0b61aee034da4c49979e30c2 Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Mon, 2 Nov 2015 05:48:59 +0100 Subject: [PATCH] termcol: ?img option to draw one image with each palette MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Manipulate source PNG file at data/indi.png to use palette colours in order to compare look of an EGA game for each. Quickly implemented by overwriting data at certain positions, which will break with unexpected data. Inline encoding increases page size by 6⋅5kB ≈ 30kB. --- termcol.plp | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/termcol.plp b/termcol.plp index 0b3d236..04157ef 100644 --- a/termcol.plp +++ b/termcol.plp @@ -14,6 +14,8 @@ Html({ stylesheet => [qw'light dark'], }); +my $imgfile = exists $get{img} && 'indi.png'; + my @termlist; push @termlist, split /\W+/, $ENV{PATH_INFO} || 'default'; @@ -42,6 +44,7 @@ print
<: +use 5.010; use Shiar_Sheet::Colour '1.03'; use List::Util qw( min max ); @@ -69,6 +72,28 @@ sub colcell { return "$out\n"; } +sub img_egapal { + my ($palette) = @_; + return eval { + require MIME::Base64; + require Digest::CRC; + + local $/; + open my $img, '<:bytes', "data/$imgfile" or die "$!\n"; + my $imgdata = readline $img; + + my $offset = 0x29 - 4; + my $len = 16 * 3; + my $chunklen = $len + 4; + substr($imgdata, $offset+4, $len) = pack 'H*', join '', @{$palette}; + my ($p, $crc) = unpack "x${offset}a${chunklen}N", $imgdata; + substr($imgdata, $offset+4+$len, 4) = pack 'N', Digest::CRC::crc32($p); + + return sprintf '', + MIME::Base64::encode_base64($imgdata); + } || $@; +} + for my $term (@termlist) { my $info = $palettes->{$term}; ref $info eq 'HASH' or next; @@ -104,6 +129,8 @@ for my $term (@termlist) { $name = [ $name, ['#333'] ] if $term eq 'xkcd'; print '', colcell($name, $rgb); } + + print '', img_egapal(\@{$colours}) if $imgfile; print "\n\n"; } } @@ -168,10 +195,17 @@ sub coltable_hsv { my $colbreak = scalar map { @$_ } @colmap; # same width as hue rows for my $num (sort { $greymap{$a} <=> $greymap{$b} } keys %greymap) { $out .= '' unless $col++ % $colbreak; - $out .= colcell($num, ($greymap{$num}) x 3); + $out .= colcell($num, ($greymap{$num})); } } + if ($imgfile) { + my @palette = map { [ @{$_}[1 .. 3] ] } map {@$_} map {@$_} @colmap; + my $imgdata = img_egapal(\@palette); + my $tablespan = scalar map { @$_ } @colmap; + $out .= "$imgdata"; + } + return $out; } -- 2.30.0