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