termcol: distinct image palette in cga order
[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 $palette = $info->{list}) {
131                 my $colours = colorder($palette, $get{v} && $info->{ansiorder});
132
133                 my $rows = 8;
134                 my $columns = ceil(@{$palette} / $rows);
135
136                 print '<table class=color>', "\n";
137                 printf "<caption>%s</caption>\n", $caption;
138                 for my $row (0 .. $rows - 1) {
139                         print '<tr>';
140                         for my $col (0 .. $columns - 1) {
141                                 my $num = $row + $col * $rows;
142                                 my ($rgb, $name) = split /:/, $colours->[$num], 3;
143                                 $name //= $rgb && $num;
144                                 $name = [ $name, [] ] if $term =~ /^msx/ and !$num;
145                                 $name = [ $name, ['#333'] ] if $term eq 'xkcd';
146                                 print colcell($name, $rgb);
147                         }
148                 }
149
150                 for (@draw) {
151                         my $imgpal = colorder($palette, $info->{ansiorder});
152                         print "<tr><td colspan=$columns>", img_egapal($imgpal, @{$_});
153                 }
154                 print "</table>\n\n";
155         }
156 }
157
158 sub colorder {
159         my ($palette, $reorder) = @_;
160         return [ map { $palette->[$_] =~ s/:|$/:$_/r } @{$reorder} ];
161                 if $reorder;
162         return $palette;
163 }
164
165 sub coltable_hsv {
166         my ($dim, $rgbval, $greyramp) = @_;
167
168         my $hmax = 2 * $dim * 3;  # each face of the rgb cube
169         my $vmax = $dim - 1;
170         my $smax = $dim - 1;
171         $rgbval ||= sub { join('', @_), map { int $_ * 255 / $vmax } @_ };
172
173         my @greymap = @{$greyramp || []};  # [name, r, g=l, b]
174         my @colmap;  # saturation => value => hue => [name, r,g,b]
175
176         for my $r (0 .. $dim - 1) {
177                 for my $g (0 .. $dim - 1) {
178                         for my $b (0 .. $dim - 1) {
179                                 my @rgb = ($r, $g, $b);
180
181                                 my ($h, $s, $v) = Shiar_Sheet::Colour->new(@rgb)->hsv;
182
183                                 if (!$s) {
184                                         if (@greymap) {
185                                                 push @greymap, [ $rgbval->(@rgb) ];
186                                                 next;
187                                         }
188
189                                         $h = 1;  # greyscale hue
190                                         $s = $smax - $v + 1;  # spread brightness over saturation groups
191                                         $v &&= $smax  # highest saturation
192                                                 or $v = $s = 1;  # black at initial column
193                                 }
194
195                                 $h *= $hmax;
196                                 $v = $vmax - $v;
197                                 $s = $smax - $s - $v;
198
199                                 $colmap[$s][$v][$h] = [ $rgbval->(@rgb) ];
200                         }
201                 }
202         }
203
204         my $out = '';
205         $out .= sprintf '<colgroup span=%d>', scalar @{$_} for @colmap;
206         my $huerow = $colmap[0][0]; # first {$_} map { @{$_} } @colmap;
207         for my $h (grep { $huerow->[$_] } 0 .. $#{$huerow}) {
208                 $out .= '<tr>';
209                 $out .= colcell(@$_) for map { $_->[$h] } map { reverse @{$_} } @colmap;
210         }
211
212         if (@greymap) {
213                 $out .= '<tbody>';
214                 my $col = 0;
215                 my $colbreak = scalar map { @$_ } @colmap;  # same width as hue rows
216                 for my $cell (sort { $a->[1] <=> $b->[1] || $a->[0] <=> $b->[0] } @greymap) {
217                         $out .= '<tr>' unless $col++ % $colbreak;
218                         $out .= colcell(@{$cell});
219                 }
220         }
221
222         if (@draw) {
223                 my @palette = map { [ @{$_}[1 .. 3] ] } @greymap, map {@$_} map {@$_} @colmap;
224                 my $tablespan = scalar map { @$_ } @colmap;
225                 my $imgdata = img_egapal(\@palette, @{ $draw[0] });
226                 $out .= "<tr><td colspan=$tablespan>$imgdata";
227         }
228
229         return $out;
230 }
231
232 :></div>
233 <hr>
234