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