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