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