termcol: move common styling to css include
[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 colspan=3>\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('<th title="%s" style="%s">%s',
62                 join(',', map { int } @$col),
63                 "background:$css; color:$inverse",
64                 $name,
65         );
66         $out .= sprintf '<td style="%s">%s', "background:$_; color:$css", $minhex
67                 for @$sample;
68         return "$out\n";
69 }
70
71 for my $term (@termlist) {
72         my $info = $palettes->{$term};
73         ref $info eq 'HASH' or next;
74         my $caption = $info->{name} // $term;
75         $caption = sprintf('<%s %s>%s</%1$s>',
76                 $info->{href} ? 'a' : 'span',
77                 join(' ',
78                         map { sprintf '%s="%s"', $_, $info->{$_} }
79                         grep { defined $info->{$_} }
80                         qw( href title )
81                 ),
82                 $caption,
83         ) if $info->{href} or $info->{title};
84
85         if (my $mapinfo = $info->{rgbmap}) {
86                 print '<table class="color mapped">'."\n";
87                 printf "<caption>%s</caption>\n", $caption;
88                 print coltable_hsv(@{$mapinfo});
89                 print "</table>\n\n";
90         }
91
92         if (my $colours = $info->{list}) {
93                 if (my $reorder = $info->{ansiorder} and $get{v}) {
94                         $colours = [ map { $colours->[$_] =~ s/:|$/:$_/r } @{$reorder} ];
95                 }
96
97                 print '<table class=color>', "\n";
98                 printf "<caption>%s</caption>\n", $caption;
99                 for my $num (0 .. $#{$colours}) {
100                         my ($rgb, $name) = split /:/, $colours->[$num], 3;
101                         $name ||= $num;
102                         $name = [ $name, [] ] if $term =~ /^msx/ and !$num;
103                         $name = [ $name, ['#333'] ] if $term eq 'xkcd';
104                         print '<tr>', colcell($name, $rgb);
105                 }
106                 print "</table>\n\n";
107         }
108 }
109
110 sub coltable_hsv {
111         my ($dim, $rgbval, $greyramp) = @_;
112
113         my $hmax = 2 * $dim * 3;  # each face of the rgb cube
114         my $vmax = $dim - 1;
115         my $smax = $dim - 1;
116         $rgbval ||= sub { join('', @_), map { int $_ * 255 / $vmax } @_ };
117         $greyramp ||= [];
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                                         my ($index, $l) = $rgbval->(@rgb);
134                                         $greymap{$index} = $l;
135                                         next;
136                                 }
137
138                                 $v = $vmax - $v;
139                                 $s = $smax - $s - $v;
140
141                                 $colmap[$s][$v][$h] = [ $rgbval->(@rgb) ];
142                         }
143                 }
144         }
145
146         my $out = '';
147         $out .= sprintf '<colgroup span=%d>', 3 * @{$_} for @colmap;
148         my $huerow = $colmap[0][0]; # first {$_} map { @{$_} } @colmap;
149         for my $h (grep { $huerow->[$_] } 0 .. $#{$huerow}) {
150                 $out .= '<tr>';
151                 $out .= colcell(@$_) for map { $_->[$h] } map { @{$_} } @colmap;
152         }
153
154         $offset += $dim ** 3;
155         $greymap{$offset++} = $_ for @{$greyramp};
156
157         $out .= '<tbody>';
158         my $col = 0;
159         my $colbreak = scalar map { @$_ } @colmap;  # same width as hue rows
160         for my $num (sort { $greymap{$a} <=> $greymap{$b} } keys %greymap) {
161                 $out .= '<tr>' unless $col++ % $colbreak;
162                 $out .= colcell($num, ($greymap{$num}) x 3);
163         }
164
165         return $out;
166 }
167
168 :></div>
169 <hr>
170