termcol: omit backgrounded sample cells for msx transparancy
[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 my @termlist = qw( cga xterm tango app html xkcd );
59 push @termlist, qw( c64 msx2 mac2 risc arnegame cpc ) if exists $get{v};
60 push @termlist, qw( ansi88 );
61 push @termlist, qw( ansi256 ) if $ENV{PATH_INFO} =~ /256/;
62
63 for my $term (@termlist) {
64         my $info = $palettes->{$term};
65         ref $info eq 'HASH' or next;
66         my $caption = $info->{name} // $term;
67         $caption = sprintf('<%s %s>%s</%1$s>',
68                 $info->{href} ? 'a' : 'span',
69                 join(' ',
70                         map { sprintf '%s="%s"', $_, $info->{$_} }
71                         grep { defined $info->{$_} }
72                         qw( href title )
73                 ),
74                 $caption,
75         ) if $info->{href} or $info->{title};
76
77         if (my $mapinfo = $info->{rgbmap}) {
78                 print '<table class="mapped">'."\n";
79                 printf "<caption>%s</caption>\n", $caption;
80                 print coltable_hsv(@{$mapinfo});
81                 print "</table>\n\n";
82         }
83
84         if (my $colours = $info->{list}) {
85                 if (my $reorder = $info->{ansiorder} and $get{v}) {
86                         $colours = [ map { $colours->[$_] =~ s/:|$/:$_/r } @{$reorder} ];
87                 }
88
89                 print '<table>', "\n";
90                 printf "<caption>%s</caption>\n", $caption;
91                 for my $num (0 .. $#{$colours}) {
92                         my ($rgb, $name) = split /:/, $colours->[$num], 3;
93                         $name ||= $num;
94                         $name = [ $name, [] ] if $term =~ /^msx/ and !$num;
95                         $name = [ $name, ['#333'] ] if $term eq 'xkcd';
96                         print '<tr>', colcell($name, $rgb);
97                 }
98                 print "</table>\n\n";
99         }
100 }
101
102 sub coltable_hsv {
103         my ($dim, $rgbval, $greyramp) = @_;
104
105         my $hmax = 2 * $dim * 3;  # each face of the rgb cube
106         my $vmax = $dim - 1;
107         my $smax = $dim - 1;
108         $rgbval ||= sub { join('', @_), map { int $_ * 255 / $vmax } @_ };
109         $greyramp ||= [];
110
111         my %greymap;  # name => value
112         my @colmap;  # saturation => value => hue => [name, r,g,b]
113         my $offset = 16 * ($dim > 3);
114
115         for my $r (0 .. $dim - 1) {
116                 for my $g (0 .. $dim - 1) {
117                         for my $b (0 .. $dim - 1) {
118                                 my @rgb = ($r, $g, $b);
119
120                                 my $h = Shiar_Sheet::Colour->new(@rgb)->hue * $hmax;
121                                 my $v = max(@rgb);
122                                 my $s = abs(min(@rgb) - max(@rgb));
123
124                                 if (!$s) {
125                                         my ($index, $l) = $rgbval->(@rgb);
126                                         $greymap{$index} = $l;
127                                         next;
128                                 }
129
130                                 $v = $vmax - $v;
131                                 $s = $smax - $s - $v;
132
133                                 $colmap[$s][$v][$h] = [ $rgbval->(@rgb) ];
134                         }
135                 }
136         }
137
138         my $out = '';
139         $out .= sprintf '<colgroup span=%d>', 3 * @{$_} for @colmap;
140         my $huerow = $colmap[0][0]; # first {$_} map { @{$_} } @colmap;
141         for my $h (grep { $huerow->[$_] } 0 .. $#{$huerow}) {
142                 $out .= '<tr>';
143                 $out .= colcell(@$_) for map { $_->[$h] } map { @{$_} } @colmap;
144         }
145
146         $offset += $dim ** 3;
147         $greymap{$offset++} = $_ for @{$greyramp};
148
149         $out .= '<tbody>';
150         my $col = 0;
151         my $colbreak = scalar map { @$_ } @colmap;  # same width as hue rows
152         for my $num (sort { $greymap{$a} <=> $greymap{$b} } keys %greymap) {
153                 $out .= '<tr>' unless $col++ % $colbreak;
154                 $out .= colcell($num, ($greymap{$num}) x 3);
155         }
156
157         return $out;
158 }
159
160 :></div>
161 <hr>
162