termcol: ?img option to draw one image with each palette
[sheet.git] / termcol.plp
1 <(common.inc.plp)><:
2
3 Html({
4         title => 'terminal colour cheat sheet',
5         version => '1.0',
6         description => [
7                 "Index of all terminal/console colour codes,",
8                 "with an example result of various environments.",
9         ],
10         keywords => [qw'
11                 color code terminal console escape table xterm rxvt
12         '],
13         data => ['termcol.inc.pl'],
14         stylesheet => [qw'light dark'],
15 });
16
17 my $imgfile = exists $get{img} && 'indi.png';
18
19 my @termlist;
20 push @termlist, split /\W+/, $ENV{PATH_INFO} || 'default';
21
22 my %termgroup = (
23         default => [qw( ansi xkcd ansi88 )],
24         more    => [qw( ansi legacy ansi256 )],
25         msx     => [qw( msx1 msx2 arnejmp )],
26         ansi    => [qw( cga xterm tango app html )],
27         legacy  => [qw( c64 msx2 mac2 risc arnegame cpc )],
28 );
29 @{$_} = map { $termgroup{$_} ? @{ $termgroup{$_} } : $_ } @{$_}
30         for values %termgroup, \@termlist;
31
32 :>
33 <h1>Terminal colours</h1>
34
35 <p>
36 <span title="ECMA-48">ANSI</span> (VT100, ISO-6429) 16-colour text palette
37 as implemented by various systems and programs.
38 <:
39 print
40         "@termlist" ne "@{ $termgroup{default} }" ? 'Additional palettes are included as specified.' :
41         'Also see <a href="/termcol/more">8-bit legacy hardware</a> palettes.';
42 :>
43 </p>
44
45 <div class="section">
46 <:
47 use 5.010;
48 use Shiar_Sheet::Colour '1.03';
49 use List::Util qw( min max );
50
51 my $palettes = do 'termcol.inc.pl';
52 die "Cannot open palette data: $_\n" for $@ || $! || ();
53
54 sub colcell {
55         my $name = shift // return "<td>\n";
56         my $col = Shiar_Sheet::Colour->new(@_);
57         my $minhex = $col->rgb24;
58         my $css     = '#' . $col->rgb48;
59         my $inverse = '#' . sprintf('%X', $col->luminance/255 < .3 ? 12 : 0) x 3;
60
61         my $sample = [ qw(#000 #FFF) ];
62         ($name, $sample) = @$name if ref $name eq 'ARRAY';
63
64         my $out = sprintf('<td title="%s" style="%s">%s',
65                 join(',', map { int } @$col),
66                 "background:$css; color:$inverse",
67                 $name,
68         );
69         $out .= sprintf('<samp style="%s"><small>%s</small></samp>',
70                 "background:$_; color:$css", $minhex
71         ) for @$sample;
72         return "$out\n";
73 }
74
75 sub img_egapal {
76         my ($palette) = @_;
77         return eval {
78                 require MIME::Base64;
79                 require Digest::CRC;
80
81                 local $/;
82                 open my $img, '<:bytes', "data/$imgfile" or die "$!\n";
83                 my $imgdata = readline $img;
84
85                 my $offset = 0x29 - 4;
86                 my $len = 16 * 3;
87                 my $chunklen = $len + 4;
88                 substr($imgdata, $offset+4, $len) = pack 'H*', join '', @{$palette};
89                 my ($p, $crc) = unpack "x${offset}a${chunklen}N", $imgdata;
90                 substr($imgdata, $offset+4+$len, 4) = pack 'N', Digest::CRC::crc32($p);
91
92                 return sprintf '<img src="data:image/png;base64,%s">',
93                         MIME::Base64::encode_base64($imgdata);
94         } || $@;
95 }
96
97 for my $term (@termlist) {
98         my $info = $palettes->{$term};
99         ref $info eq 'HASH' or next;
100         my $caption = $info->{name} // $term;
101         $caption = sprintf('<%s %s>%s</%1$s>',
102                 $info->{href} ? 'a' : 'span',
103                 join(' ',
104                         map { sprintf '%s="%s"', $_, $info->{$_} }
105                         grep { defined $info->{$_} }
106                         qw( href title )
107                 ),
108                 $caption,
109         ) if $info->{href} or $info->{title};
110
111         if (my $mapinfo = $info->{rgbmap}) {
112                 print '<table class="color mapped">'."\n";
113                 printf "<caption>%s</caption>\n", $caption;
114                 print coltable_hsv(@{$mapinfo});
115                 print "</table>\n\n";
116         }
117
118         if (my $colours = $info->{list}) {
119                 if (my $reorder = $info->{ansiorder} and $get{v}) {
120                         $colours = [ map { $colours->[$_] =~ s/:|$/:$_/r } @{$reorder} ];
121                 }
122
123                 print '<table class=color>', "\n";
124                 printf "<caption>%s</caption>\n", $caption;
125                 for my $num (0 .. $#{$colours}) {
126                         my ($rgb, $name) = split /:/, $colours->[$num], 3;
127                         $name ||= $num;
128                         $name = [ $name, [] ] if $term =~ /^msx/ and !$num;
129                         $name = [ $name, ['#333'] ] if $term eq 'xkcd';
130                         print '<tr>', colcell($name, $rgb);
131                 }
132
133                 print '<tr><td>', img_egapal(\@{$colours}) if $imgfile;
134                 print "</table>\n\n";
135         }
136 }
137
138 sub coltable_hsv {
139         my ($dim, $rgbval, $greyramp) = @_;
140
141         my $hmax = 2 * $dim * 3;  # each face of the rgb cube
142         my $vmax = $dim - 1;
143         my $smax = $dim - 1;
144         $rgbval ||= sub { join('', @_), map { int $_ * 255 / $vmax } @_ };
145
146         my %greymap;  # name => value
147         my @colmap;  # saturation => value => hue => [name, r,g,b]
148         my $offset = 16 * ($dim > 3);
149
150         for my $r (0 .. $dim - 1) {
151                 for my $g (0 .. $dim - 1) {
152                         for my $b (0 .. $dim - 1) {
153                                 my @rgb = ($r, $g, $b);
154
155                                 my $h = Shiar_Sheet::Colour->new(@rgb)->hue * $hmax;
156                                 my $v = max(@rgb);
157                                 my $s = abs(min(@rgb) - max(@rgb));
158
159                                 if (!$s) {
160                                         if ($greyramp) {
161                                                 my ($index, $l) = $rgbval->(@rgb);
162                                                 $greymap{$index} = $l;
163                                                 next;
164                                         }
165
166                                         $h = $hmax;  # greyscale hue
167                                         $s = 1;  # lowest saturation for other hues
168                                         $v = $s = $vmax if !$v;  # black at full saturation
169                                 }
170
171                                 $v = $vmax - $v;
172                                 $s = $smax - $s - $v;
173
174                                 $colmap[$s][$v][$h] = [ $rgbval->(@rgb) ];
175                         }
176                 }
177         }
178
179         my $out = '';
180         $out .= sprintf '<colgroup span=%d>', scalar @{$_} for @colmap;
181         my $huerow = $colmap[0][0]; # first {$_} map { @{$_} } @colmap;
182         for my $h (grep { $huerow->[$_] } 0 .. $#{$huerow}) {
183                 $out .= '<tr>';
184                 $out .= colcell(@$_) for map { $_->[$h] } map { @{$_} } @colmap;
185         }
186
187         if ($greyramp) {
188                 $offset += $dim ** 3;
189                 $greymap{$offset++} = $_ for @{$greyramp};
190         }
191
192         if (%greymap) {
193                 $out .= '<tbody>';
194                 my $col = 0;
195                 my $colbreak = scalar map { @$_ } @colmap;  # same width as hue rows
196                 for my $num (sort { $greymap{$a} <=> $greymap{$b} } keys %greymap) {
197                         $out .= '<tr>' unless $col++ % $colbreak;
198                         $out .= colcell($num, ($greymap{$num}));
199                 }
200         }
201
202         if ($imgfile) {
203                 my @palette = map { [ @{$_}[1 .. 3] ] } map {@$_} map {@$_} @colmap;
204                 my $imgdata = img_egapal(\@palette);
205                 my $tablespan = scalar map { @$_ } @colmap;
206                 $out .= "<tr><td colspan=$tablespan>$imgdata";
207         }
208
209         return $out;
210 }
211
212 :></div>
213 <hr>
214