termcol: show reordered cell title "0"
[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
52 my $palettes = do 'termcol.inc.pl';
53 die "Cannot open palette data: $_\n" for $@ || $! || ();
54
55 sub colcell {
56         my $name = shift // return "<td>\n";
57         my $col = Shiar_Sheet::Colour->new(@_);
58         my $minhex = $col->rgb24;
59         my $css     = '#' . $col->rgb48;
60         my $inverse = '#' . sprintf('%X', $col->luminance/255 < .3 ? 12 : 0) x 3;
61
62         my $sample = [ qw(#000 #FFF) ];
63         ($name, $sample) = @$name if ref $name eq 'ARRAY';
64
65         my $out = sprintf('<td title="%s" style="%s">%s',
66                 join(',', map { int } @$col),
67                 "background:$css; color:$inverse",
68                 $name,
69         );
70         $out .= sprintf('<samp style="%s"><small>%s</small></samp>',
71                 "background:$_; color:$css", $minhex
72         ) for @$sample;
73         return "$out\n";
74 }
75
76 sub img_egapal {
77         my ($palette, $imgfile, $reindex) = @_;
78         return eval {
79                 require Imager;
80                 require MIME::Base64;
81
82                 my @imgpal = map { Imager::Color->new(ref $_ ? @$_ : $_) } @{$palette};
83                 state $imgcache = {};
84                 my $img = $imgcache->{$imgfile}
85                         //= Imager->new(file => "data/palimage/$imgfile")
86                         or die Imager->errstr.$/;
87
88                 do {
89                         if ($reindex) {
90                                 $img->to_paletted({
91                                         make_colors => 'none',
92                                         colors => \@imgpal,
93                                         translate => 'closest',
94                                 });
95                         }
96                         else {
97                                 @{[ $img->getcolors ]} == @imgpal
98                                         or die "incompatible palette size\n";
99                                 $img->setcolors(colors => \@imgpal);
100                                 $img;
101                         }
102                 }->write(data => \my $imgdata, type => 'png');
103                 return sprintf '<img src="data:image/png;base64,%s">',
104                         MIME::Base64::encode_base64($imgdata);
105         } || $@;
106 }
107
108 for my $term (@termlist) {
109         my $info = $palettes->{$term};
110         ref $info eq 'HASH' or next;
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 $colours = $info->{list}) {
130                 if (my $reorder = $info->{ansiorder} and $get{v}) {
131                         $colours = [ map { $colours->[$_] =~ s/:|$/:$_/r } @{$reorder} ];
132                 }
133
134                 print '<table class=color>', "\n";
135                 printf "<caption>%s</caption>\n", $caption;
136                 for my $num (0 .. $#{$colours}) {
137                         my ($rgb, $name) = split /:/, $colours->[$num], 3;
138                         $name //= $num;
139                         $name = [ $name, [] ] if $term =~ /^msx/ and !$num;
140                         $name = [ $name, ['#333'] ] if $term eq 'xkcd';
141                         print '<tr>', colcell($name, $rgb);
142                 }
143
144                 print '<tr><td>', img_egapal(\@{$colours}, @{$_}) for @draw;
145                 print "</table>\n\n";
146         }
147 }
148
149 sub coltable_hsv {
150         my ($dim, $rgbval, $greyramp) = @_;
151
152         my $hmax = 2 * $dim * 3;  # each face of the rgb cube
153         my $vmax = $dim - 1;
154         my $smax = $dim - 1;
155         $rgbval ||= sub { join('', @_), map { int $_ * 255 / $vmax } @_ };
156
157         my @greymap = @{$greyramp || []};  # [name, r, g=l, b]
158         my @colmap;  # saturation => value => hue => [name, r,g,b]
159
160         for my $r (0 .. $dim - 1) {
161                 for my $g (0 .. $dim - 1) {
162                         for my $b (0 .. $dim - 1) {
163                                 my @rgb = ($r, $g, $b);
164
165                                 my ($h, $s, $v) = Shiar_Sheet::Colour->new(@rgb)->hsv;
166
167                                 if (!$s) {
168                                         if (@greymap) {
169                                                 push @greymap, [ $rgbval->(@rgb) ];
170                                                 next;
171                                         }
172
173                                         $h = 1;  # greyscale hue
174                                         $s = 1;  # lowest saturation for other hues
175                                         $v = $s = $vmax if !$v;  # black at full saturation
176                                 }
177
178                                 $h *= $hmax;
179                                 $v = $vmax - $v;
180                                 $s = $smax - $s - $v;
181
182                                 $colmap[$s][$v][$h] = [ $rgbval->(@rgb) ];
183                         }
184                 }
185         }
186
187         my $out = '';
188         $out .= sprintf '<colgroup span=%d>', scalar @{$_} for @colmap;
189         my $huerow = $colmap[0][0]; # first {$_} map { @{$_} } @colmap;
190         for my $h (grep { $huerow->[$_] } 0 .. $#{$huerow}) {
191                 $out .= '<tr>';
192                 $out .= colcell(@$_) for map { $_->[$h] } map { @{$_} } @colmap;
193         }
194
195         if (@greymap) {
196                 $out .= '<tbody>';
197                 my $col = 0;
198                 my $colbreak = scalar map { @$_ } @colmap;  # same width as hue rows
199                 for my $cell (sort { $a->[1] <=> $b->[1] || $a->[0] <=> $b->[0] } @greymap) {
200                         $out .= '<tr>' unless $col++ % $colbreak;
201                         $out .= colcell(@{$cell});
202                 }
203         }
204
205         if (@draw) {
206                 my @palette = map { [ @{$_}[1 .. 3] ] } @greymap, map {@$_} map {@$_} @colmap;
207                 my $tablespan = scalar map { @$_ } @colmap;
208                 my $imgdata = img_egapal(\@palette, @{ $draw[0] });
209                 $out .= "<tr><td colspan=$tablespan>$imgdata";
210         }
211
212         return $out;
213 }
214
215 :></div>
216 <hr>
217