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