termcol: float text samples in cells
[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 @termlist;
18 push @termlist, split /\W+/, $ENV{PATH_INFO} || 'default';
19
20 my %termgroup = (
21         default => [qw( ansi xkcd ansi88 )],
22         more    => [qw( ansi legacy ansi256 )],
23         msx     => [qw( msx1 msx2 arnejmp )],
24         ansi    => [qw( cga xterm tango app html )],
25         legacy  => [qw( c64 msx2 mac2 risc arnegame cpc )],
26 );
27 @{$_} = map { $termgroup{$_} ? @{ $termgroup{$_} } : $_ } @{$_}
28         for values %termgroup, \@termlist;
29
30 :>
31 <h1>Terminal colours</h1>
32
33 <p>
34 <span title="ECMA-48">ANSI</span> (VT100, ISO-6429) 16-colour text palette
35 as implemented by various systems and programs.
36 <:
37 print
38         "@termlist" ne "@{ $termgroup{default} }" ? 'Additional palettes are included as specified.' :
39         'Also see <a href="/termcol/more">8-bit legacy hardware</a> palettes.';
40 :>
41 </p>
42
43 <div class="section">
44 <:
45 use Shiar_Sheet::Colour '1.03';
46 use List::Util qw( min max );
47
48 my $palettes = do 'termcol.inc.pl';
49 die "Cannot open palette data: $_\n" for $@ || $! || ();
50
51 sub colcell {
52         my $name = shift // return "<td>\n";
53         my $col = Shiar_Sheet::Colour->new(@_);
54         my $minhex = $col->rgb24;
55         my $css     = '#' . $col->rgb48;
56         my $inverse = '#' . sprintf('%X', $col->luminance/255 < .3 ? 12 : 0) x 3;
57
58         my $sample = [ qw(#000 #FFF) ];
59         ($name, $sample) = @$name if ref $name eq 'ARRAY';
60
61         my $out = sprintf('<td title="%s" style="%s">%s',
62                 join(',', map { int } @$col),
63                 "background:$css; color:$inverse",
64                 $name,
65         );
66         $out .= sprintf('<samp style="%s"><small>%s</small></samp>',
67                 "background:$_; color:$css", $minhex
68         ) for @$sample;
69         return "$out\n";
70 }
71
72 for my $term (@termlist) {
73         my $info = $palettes->{$term};
74         ref $info eq 'HASH' or next;
75         my $caption = $info->{name} // $term;
76         $caption = sprintf('<%s %s>%s</%1$s>',
77                 $info->{href} ? 'a' : 'span',
78                 join(' ',
79                         map { sprintf '%s="%s"', $_, $info->{$_} }
80                         grep { defined $info->{$_} }
81                         qw( href title )
82                 ),
83                 $caption,
84         ) if $info->{href} or $info->{title};
85
86         if (my $mapinfo = $info->{rgbmap}) {
87                 print '<table class="color mapped">'."\n";
88                 printf "<caption>%s</caption>\n", $caption;
89                 print coltable_hsv(@{$mapinfo});
90                 print "</table>\n\n";
91         }
92
93         if (my $colours = $info->{list}) {
94                 if (my $reorder = $info->{ansiorder} and $get{v}) {
95                         $colours = [ map { $colours->[$_] =~ s/:|$/:$_/r } @{$reorder} ];
96                 }
97
98                 print '<table class=color>', "\n";
99                 printf "<caption>%s</caption>\n", $caption;
100                 for my $num (0 .. $#{$colours}) {
101                         my ($rgb, $name) = split /:/, $colours->[$num], 3;
102                         $name ||= $num;
103                         $name = [ $name, [] ] if $term =~ /^msx/ and !$num;
104                         $name = [ $name, ['#333'] ] if $term eq 'xkcd';
105                         print '<tr>', colcell($name, $rgb);
106                 }
107                 print "</table>\n\n";
108         }
109 }
110
111 sub coltable_hsv {
112         my ($dim, $rgbval, $greyramp) = @_;
113
114         my $hmax = 2 * $dim * 3;  # each face of the rgb cube
115         my $vmax = $dim - 1;
116         my $smax = $dim - 1;
117         $rgbval ||= sub { join('', @_), map { int $_ * 255 / $vmax } @_ };
118
119         my %greymap;  # name => value
120         my @colmap;  # saturation => value => hue => [name, r,g,b]
121         my $offset = 16 * ($dim > 3);
122
123         for my $r (0 .. $dim - 1) {
124                 for my $g (0 .. $dim - 1) {
125                         for my $b (0 .. $dim - 1) {
126                                 my @rgb = ($r, $g, $b);
127
128                                 my $h = Shiar_Sheet::Colour->new(@rgb)->hue * $hmax;
129                                 my $v = max(@rgb);
130                                 my $s = abs(min(@rgb) - max(@rgb));
131
132                                 if (!$s) {
133                                         if ($greyramp) {
134                                                 my ($index, $l) = $rgbval->(@rgb);
135                                                 $greymap{$index} = $l;
136                                                 next;
137                                         }
138
139                                         $h = $hmax;  # greyscale hue
140                                         $s = 1;  # lowest saturation for other hues
141                                         $v = $s = $vmax if !$v;  # black at full saturation
142                                 }
143
144                                 $v = $vmax - $v;
145                                 $s = $smax - $s - $v;
146
147                                 $colmap[$s][$v][$h] = [ $rgbval->(@rgb) ];
148                         }
149                 }
150         }
151
152         my $out = '';
153         $out .= sprintf '<colgroup span=%d>', scalar @{$_} for @colmap;
154         my $huerow = $colmap[0][0]; # first {$_} map { @{$_} } @colmap;
155         for my $h (grep { $huerow->[$_] } 0 .. $#{$huerow}) {
156                 $out .= '<tr>';
157                 $out .= colcell(@$_) for map { $_->[$h] } map { @{$_} } @colmap;
158         }
159
160         if ($greyramp) {
161                 $offset += $dim ** 3;
162                 $greymap{$offset++} = $_ for @{$greyramp};
163         }
164
165         if (%greymap) {
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
175         return $out;
176 }
177
178 :></div>
179 <hr>
180