44ee75bcd53179846ee0f15233cb42509ea5f33a
[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 :>
18 <h1>Terminal colours</h1>
19
20 <p>
21 <span title="ECMA-48">ANSI</span> (VT100, ISO-6429) 16-colour text palette
22 as implemented by various systems and programs.
23 <:
24 print
25         !exists $get{v} ? 'Also see <a href="?v">8-bit legacy hardware</a> palettes.' :
26         'Also included are 8-bit legacy hardware palettes.';
27 :>
28 </p>
29
30 <div class="section">
31 <:
32 use Shiar_Sheet::Colour '1.03';
33 use List::Util qw( min max );
34
35 my $palettes = do 'termcol.inc.pl';
36 die "Cannot open palette data: $_\n" for $@ || $! || ();
37
38 sub colcell {
39         my $name = shift // return "<td colspan=3>\n";
40         my $col = Shiar_Sheet::Colour->new(@_);
41         my $minhex = $col->rgb24;
42         my $css     = '#' . $col->rgb48;
43         my $inverse = '#' . sprintf('%X', $col->luminance/255 < .3 ? 12 : 0) x 3;
44
45         my $sample = [ qw(#000 #FFF) ];
46         ($name, $sample) = @$name if ref $name eq 'ARRAY';
47
48         my $out = sprintf('<td title="%s" style="%s">%s',
49                 join(',', map { int } @$col),
50                 "background:$css; color:$inverse; padding:0 1ex",
51                 $name,
52         );
53         $out .= sprintf '<td style="%s"><code>%s</code>', "background:$_; color:$css", $minhex
54                 for @$sample;
55         return "$out\n";
56 }
57
58 if ($get{v}) {
59         my %reorder = (
60                 arnegame => [ 0,5,9,12 , 3,6,10,13,1 , 4,7,8,11,14,15,2 ],
61                 arnegame => [ 0,3,10,6,12,9,13,1 , 5,7,11,8,14,4,15,2 ],
62                 c64 => [ 0,2,5,9,6,4,3,15 , 11,10,13,7,14,8,12,1 ],
63                 msx2 => [ 0,6,2,10,4,13,7,14 , 1,8,3,11,5,9,12,15 ],
64                 risc => [ 7,11,13,14,8,12,15,1, 6,5,10,9,4,3,2,0],
65                 mac2 => [ 15,3,9,11,6,5,7,12 , 14,2,8,1,13,4,10,0 ],
66         );
67         $reorder{$_} = $reorder{msx2} for qw( msx1 arnejmp );
68         while (my ($name, $order) = each %reorder) {
69                 for my $pal ( $palettes->{$name}) {
70                         $pal = [ map { $pal->[$_ + 1] =~ s/:|$/:$_/r } -1, @{$order} ];
71                 }
72         }
73 }
74
75 my @termlist = qw( cga xterm tango app html xkcd );
76 push @termlist, qw( c64 msx2 mac2 risc arnegame ) if exists $get{v};
77 for my $term (@termlist) {
78         my $info = $palettes->{$term};
79         ref $info eq 'HASH' or next;
80         my $caption = $info->{name} // $term;
81         $caption = sprintf('<%s %s>%s</%1$s>',
82                 $info->{href} ? 'a' : 'span',
83                 join(' ',
84                         map { sprintf '%s="%s"', $_, $info->{$_} }
85                         grep { defined $info->{$_} }
86                         qw( href title )
87                 ),
88                 $caption,
89         ) if $info->{href} or $info->{title};
90
91         if (my $colours = $info->{list}) {
92                 print '<table>', "\n";
93                 printf "<caption>%s</caption>\n", $caption;
94                 for my $num (0 .. $#{$colours}) {
95                         my ($rgb, $name) = split /:/, $colours->[$num], 3;
96                         $name ||= $num;
97                         $name = [ $name, ['#333'] ] if $term eq 'xkcd';
98                         print '<tr>', colcell($name, $rgb);
99                 }
100                 print "</table>\n\n";
101         }
102 }
103
104 if (exists $get{v}) {
105         print "<table class=mapped>\n";
106         print "<caption>Amstrad CPC</caption>\n";
107         print coltable_hsv(3, sub {
108                 $_[2] + 3 * ($_[0] + 3 * $_[1]),
109                 map { $_ && $_ * 127 + 1 } @_
110         });
111         print "</table>\n\n";
112 }
113 :></div>
114
115 <hr>
116
117 <div class="section">
118 <:
119 sub coltable_hsv {
120         my ($dim, $rgbval, $greyramp) = @_;
121
122         my $hmax = 2 * $dim * 3;  # each face of the rgb cube
123         my $vmax = $dim - 1;
124         my $smax = $dim - 1;
125         $rgbval ||= sub { join('', @_), map { int $_ * 255 / $vmax } @_ };
126         $greyramp ||= [];
127
128         my %greymap;  # name => value
129         my @colmap;  # saturation => value => hue => [name, r,g,b]
130         my $offset = 16 * ($dim > 3);
131
132         for my $r (0 .. $dim - 1) {
133                 for my $g (0 .. $dim - 1) {
134                         for my $b (0 .. $dim - 1) {
135                                 my @rgb = ($r, $g, $b);
136
137                                 my $h = Shiar_Sheet::Colour->new(@rgb)->hue * $hmax;
138                                 my $v = max(@rgb);
139                                 my $s = abs(min(@rgb) - max(@rgb));
140
141                                 if (!$s) {
142                                         my ($index, $l) = $rgbval->(@rgb);
143                                         $greymap{$index} = $l;
144                                         next;
145                                 }
146
147                                 $v = $vmax - $v;
148                                 $s = $smax - $s - $v;
149
150                                 $colmap[$s][$v][$h] = [ $rgbval->(@rgb) ];
151                         }
152                 }
153         }
154
155         my $out = '';
156         $out .= sprintf '<colgroup span=%d>', 3 * @{$_} for @colmap;
157         my $huerow = $colmap[0][0]; # first {$_} map { @{$_} } @colmap;
158         for my $h (grep { $huerow->[$_] } 0 .. $#{$huerow}) {
159                 $out .= '<tr>';
160                 $out .= colcell(@$_) for map { $_->[$h] } map { @{$_} } @colmap;
161         }
162
163         $offset += $dim ** 3;
164         $greymap{$offset++} = $_ for @{$greyramp};
165
166         $out .= '<tbody>';
167         my $col = 0;
168         my $colbreak = scalar map { @$_ } @colmap;  # same width as hue rows
169         for my $num (sort { $greymap{$a} <=> $greymap{$b} } keys %greymap) {
170                 $out .= '<tr>' unless $col++ % $colbreak;
171                 $out .= colcell($num, ($greymap{$num}) x 3);
172         }
173
174         return $out;
175 }
176
177 {
178         print "<h2>88-colour space</h2>\n";
179         print "<table class=mapped>\n";
180         print coltable_hsv(4,
181                 sub {
182                         $_[2] + 4 * ($_[1] + 4 * $_[0]) + 16,
183                         map { (0, 139, 205, 255)[$_] } @_
184                 },
185                 [map { ($_ + 2 + ($_>0)) * 255/11 } 0 .. 7],
186         );
187         print "</table>\n";
188 }
189
190 if ($ENV{PATH_INFO} =~ /256/) {
191         print "<h2>256-colour space</h2>\n";
192         print "<table class=mapped>\n";
193         print coltable_hsv(6,
194                 sub {
195                         $_[2] + 6 * ($_[1] + 6 * $_[0]) + 16,
196                         map { $_ && $_*40 + 55 } @_
197                 },
198                 [ map { $_ * 10 + 8 } 0 .. 23 ],
199         );
200         print "</table>\n";
201 }
202 :></div>
203 <hr>
204