perl: missing minor v5.14 features
[sheet.git] / termcol.plp
1 <(common.inc.plp)><:
2
3 if (my ($name) = $Request =~ /(.+)\.gpl\z/) {
4         my $palettes = Data('termcol');
5         my $palette = $palettes->{$name}
6                 or Abort("Palette '$name' not found", 404);
7         ref $palette ne 'ARRAY'
8                 or Abort("Group contains multiple palettes: ".join(', ', @{$palette}));
9
10         $header{content_type} = 'text/x-gimp-gpl';
11         say 'GIMP Palette';
12         say 'Name: ', $palette->{name} // $name;
13         say 'Columns: 8';
14         say '#';
15         for (@{ $palette->{list} }) {
16                 my ($rgb, $name) = split /:/, $_, 3;
17                 say join ' ', unpack('C*', pack 'H6', $rgb), $name;
18         }
19         exit;
20 }
21
22 Html({
23         title => ($Request ? 'terminal colour' : 'colour palettes') . ' cheat sheet',
24         version => '1.4',
25         description => [!$Request ? "Comparison of various colour palettes." : (
26                 "Index of all terminal/console colour codes,",
27                 "with an example result of various environments.",
28         )],
29         keywords => [qw'
30                 color colour code terminal console escape table xterm rxvt
31                 ansi vt100 8bit 4bit cga ega vga rgb hsv game emulator display
32         '],
33         data => ['termcol.inc.pl'],
34 });
35
36 my @draw = map { [$_, s/\W+\z//] } grep { $_ } split m(/),
37         $get{img} // exists $get{img} && 'compile.png';
38
39 my @termlist;
40 push @termlist, split /\W+/, $Request || 'default';
41
42 say "<h1>$_</h1>\n" for $Request ? 'Colour palettes' : 'Terminal colours';
43
44 say '<p>';
45 if ("@termlist" eq 'default') {
46         say '<span title="ECMA-48">ANSI</span> (VT100, ISO-6429) 16-colour text palette';
47         say 'as implemented by various systems and programs.';
48         say 'Also see <a href="/termcol/legacy">8-bit legacy hardware</a> palettes.';
49 }
50 elsif ("@termlist" eq 'legacy') {
51         say 'Colour palettes of various 8-bit legacy systems and retro games.';
52         say 'Also see <a href="/termcol">ANSI console</a> palettes.';
53 }
54 else {
55         say 'Comparison of requested colour palettes.';
56 }
57 :>
58
59 <div class="section">
60 <:
61 use Shiar_Sheet::Colour 1.04;
62 use List::Util qw( min max );
63 use POSIX qw( ceil );
64
65 my $palettes = Data('termcol');
66
67 sub colcell {
68         my $name = shift // return "<td>\n";
69         my $col = Shiar_Sheet::Colour->new(@_);
70         my $minhex = $col->rgb24;
71         my $css     = '#' . $col->rgb48;
72         my $inverse = '#' . sprintf('%X', $col->luminance/255 < .3 ? 12 : 0) x 3;
73
74         my $sample = [ qw(#000 #FFF) ];
75         ($name, $sample) = @$name if ref $name eq 'ARRAY';
76
77         my $out = sprintf('<td title="%s" style="%s">%s',
78                 join(',', map { int } @$col),
79                 "background:$css; color:$inverse",
80                 $name,
81         );
82         $out .= sprintf('<samp style="%s"><small>%s</small></samp>',
83                 "background:$_; color:$css", $minhex
84         ) for @$sample;
85         return "$out\n";
86 }
87
88 sub img_egapal {
89         my ($palette, $imgfile, $reindex) = @_;
90         return eval {
91                 require Imager;
92                 require MIME::Base64;
93
94                 my @imgpal = map { Imager::Color->new(ref $_ ? @$_ : $_) } @{$palette};
95                 state $imgcache = {};
96                 my $img = $imgcache->{$imgfile}
97                         //= Imager->new(file => "data/palimage/$imgfile")
98                         or die Imager->errstr.$/;
99
100                 do {
101                         if ($reindex) {
102                                 $img->to_paletted({
103                                         make_colors => 'none',
104                                         colors => \@imgpal,
105                                         translate => 'closest',
106                                 });
107                         }
108                         else {
109                                 @{[ $img->getcolors ]} == @imgpal
110                                         or die "incompatible palette size\n";
111                                 $img->setcolors(colors => \@imgpal);
112                                 $img;
113                         }
114                 }->write(data => \my $imgdata, type => 'png');
115                 return sprintf '<img src="data:image/png;base64,%s">',
116                         MIME::Base64::encode_base64($imgdata);
117         } || $@;
118 }
119
120 sub coltable {
121         my ($term) = @_;
122         my $info = $palettes->{$term};
123
124         if (ref $info eq 'ARRAY') {
125                 coltable($_) for @{$info};
126                 return;
127         }
128
129         if (ref $info eq 'CODE') {
130                 coltable($_) for $info->($palettes);
131                 return;
132         }
133
134         ref $info eq 'HASH' or return;
135         my $order = $get{order} && $get{order}.'order';
136         my $reorder = $info->{$order} // $palettes->{ $info->{parent} }->{$order};
137
138         my $caption = $info->{name} // $term;
139         $caption = sprintf('<%s %s>%s</%1$s>',
140                 $info->{href} ? 'a' : 'span',
141                 join(' ',
142                         map { sprintf '%s="%s"', $_, $info->{$_} }
143                         grep { defined $info->{$_} }
144                         qw( href title )
145                 ),
146                 $caption,
147         ) if $info->{href} or $info->{title};
148
149         if ($info->{table} or $info->{rgbmap}) {
150                 say '<table class="color mapped">';
151                 say sprintf '<caption>%s</caption>', $caption;
152
153                 print coltable_hsv(@{$_}) for $info->{rgbmap} || ();
154
155                 if (my $table = $info->{table}) {
156                         $table = [ @{$table}[@{$reorder}] ] if $reorder;
157
158                         for my $row (@$table) {
159                                 if (!$row) {
160                                         say '<tbody>';
161                                         next;
162                                 }
163                                 print '<tr>';
164                                 print colcell(ref $_ ? @$_ : $_ ? reverse split /:/ : undef) for @$row;
165                         }
166
167                         if (@draw) {
168                                 my $width = scalar @{ $table->[0] };
169                                 my @imgpal = map {
170                                         [ ref $_ ? @{$_}[1 .. 3] : map {hex} /(..)(..)(..)/ ]
171                                 } map { @{$_} } @{$table};
172                                 for (@draw) {
173                                         print "<tr><td colspan=$width>", img_egapal(\@imgpal, @{$_});
174                                 }
175                         }
176                 }
177
178                 say "</table>\n";
179         }
180
181         if (my $palette = $info->{list}) {
182                 my $colours = colorder($palette, $reorder);
183
184                 my $rows = 8;
185                 my $columns = ceil(@{$palette} / $rows);
186
187                 say '<table class=color>';
188                 say sprintf '<caption>%s</caption>', $caption;
189                 for my $row (0 .. $rows - 1) {
190                         print '<tr>';
191                         for my $col (0 .. $columns - 1) {
192                                 my $num = $row + $col * $rows;
193                                 my ($rgb, $name) = split /:/, $colours->[$num], 3;
194                                 $name //= $rgb && $num;
195                                 $name = [ $name, [] ] if $term =~ /^msx/ and !$name;  # no bg for transparency
196                                 $name = [ $name, ['#333'] ] if $term eq 'xkcd';
197                                 print colcell($name, $rgb);
198                         }
199                 }
200
201                 for (@draw) {
202                         my $imgpal = colorder($palette,
203                                 $info->{ansiorder} // $palettes->{ $info->{parent} }->{ansiorder}
204                         );
205                         print "<tr><td colspan=$columns>", img_egapal($imgpal, @{$_});
206                 }
207                 say "</table>\n";
208         }
209 }
210
211 sub colorder {
212         my ($palette, $reorder) = @_;
213         return [ map { $palette->[$_] =~ s/:(?![^:])|$/:$_/r } @{$reorder} ]
214                 if $reorder;
215         return $palette;
216 }
217
218 sub coltable_hsv {
219         my ($dim, $rgbval, $greyramp) = @_;
220
221         my $hmax = 2 * $dim * 3;  # each face of the rgb cube
222         my $vmax = $dim - 1;
223         my $smax = $dim - 1;
224         $rgbval ||= sub { join('', @_), map { int $_ * 255 / $vmax } @_ };
225
226         my @greymap = @{$greyramp || []};  # [name, r, g=l, b]
227         my @colmap;  # saturation => value => hue => [name, r,g,b]
228
229         for my $r (0 .. $dim - 1) {
230                 for my $g (0 .. $dim - 1) {
231                         for my $b (0 .. $dim - 1) {
232                                 my @rgb = ($r, $g, $b);
233
234                                 my ($h, $s, $v) = Shiar_Sheet::Colour->new(@rgb)->hsv;
235
236                                 if (!$s) {
237                                         if (@greymap) {
238                                                 push @greymap, [ $rgbval->(@rgb) ];
239                                                 next;
240                                         }
241
242                                         $h = 1;  # greyscale hue
243                                         $s = $smax - $v + 1;  # spread brightness over saturation groups
244                                         $v &&= $smax  # highest saturation
245                                                 or $v = $s = 1;  # black at initial column
246                                 }
247
248                                 $h *= $hmax;
249                                 $v = $vmax - $v;
250                                 $s = $smax - $s - $v;
251
252                                 $colmap[$s][$v][$h] = [ $rgbval->(@rgb) ];
253                         }
254                 }
255         }
256
257         my $out = '';
258         $out .= sprintf '<colgroup span=%d>', scalar @{$_} for @colmap;
259         my $huerow = $colmap[0][0]; # first {$_} map { @{$_} } @colmap;
260         for my $h (grep { $huerow->[$_] } 0 .. $#{$huerow}) {
261                 $out .= '<tr>';
262                 $out .= colcell(@$_) for map { $_->[$h] } map { reverse @{$_} } @colmap;
263         }
264
265         if (@greymap) {
266                 $out .= '<tbody>';
267                 my $col = 0;
268                 my $colbreak = scalar map { @$_ } @colmap;  # same width as hue rows
269                 for my $cell (sort { $a->[1] <=> $b->[1] || $a->[0] <=> $b->[0] } @greymap) {
270                         $out .= '<tr>' unless $col++ % $colbreak;
271                         $out .= colcell(@{$cell});
272                 }
273         }
274
275         if (@draw) {
276                 my @palette = map { [ @{$_}[1 .. 3] ] } @greymap, map {@$_} map {@$_} @colmap;
277                 my $tablespan = scalar map { @$_ } @colmap;
278                 my $imgdata = img_egapal(\@palette, @{ $draw[0] });
279                 $out .= "<tr><td colspan=$tablespan>$imgdata";
280         }
281
282         return $out;
283 }
284
285 coltable($_) for @termlist;
286 :></div>
287 <hr>
288