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