4 title => 'terminal colour cheat sheet',
7 "Index of all terminal/console colour codes,",
8 "with an example result of various environments.",
11 color code terminal console escape table xterm rxvt
13 data => ['termcol.inc.pl'],
14 stylesheet => [qw'light dark'],
17 my $imgfile = exists $get{img} && 'indi.png';
20 push @termlist, split /\W+/, $ENV{PATH_INFO} || 'default';
23 default => [qw( ansi xkcd ansi88 )],
24 more => [qw( ansi 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 )],
29 @{$_} = map { $termgroup{$_} ? @{ $termgroup{$_} } : $_ } @{$_}
30 for values %termgroup, \@termlist;
33 <h1>Terminal colours</h1>
36 <span title="ECMA-48">ANSI</span> (VT100, ISO-6429) 16-colour text palette
37 as implemented by various systems and programs.
40 "@termlist" ne "@{ $termgroup{default} }" ? 'Additional palettes are included as specified.' :
41 'Also see <a href="/termcol/more">8-bit legacy hardware</a> palettes.';
48 use Shiar_Sheet::Colour '1.03';
49 use List::Util qw( min max );
51 my $palettes = do 'termcol.inc.pl';
52 die "Cannot open palette data: $_\n" for $@ || $! || ();
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;
61 my $sample = [ qw(#000 #FFF) ];
62 ($name, $sample) = @$name if ref $name eq 'ARRAY';
64 my $out = sprintf('<td title="%s" style="%s">%s',
65 join(',', map { int } @$col),
66 "background:$css; color:$inverse",
69 $out .= sprintf('<samp style="%s"><small>%s</small></samp>',
70 "background:$_; color:$css", $minhex
82 open my $img, '<:bytes', "data/$imgfile" or die "$!\n";
83 my $imgdata = readline $img;
85 my $offset = 0x29 - 4;
87 my $chunklen = $len + 4;
88 substr($imgdata, $offset+4, $len) = pack 'H*', join '', @{$palette};
89 my ($p, $crc) = unpack "x${offset}a${chunklen}N", $imgdata;
90 substr($imgdata, $offset+4+$len, 4) = pack 'N', Digest::CRC::crc32($p);
92 return sprintf '<img src="data:image/png;base64,%s">',
93 MIME::Base64::encode_base64($imgdata);
97 for my $term (@termlist) {
98 my $info = $palettes->{$term};
99 ref $info eq 'HASH' or next;
100 my $caption = $info->{name} // $term;
101 $caption = sprintf('<%s %s>%s</%1$s>',
102 $info->{href} ? 'a' : 'span',
104 map { sprintf '%s="%s"', $_, $info->{$_} }
105 grep { defined $info->{$_} }
109 ) if $info->{href} or $info->{title};
111 if (my $mapinfo = $info->{rgbmap}) {
112 print '<table class="color mapped">'."\n";
113 printf "<caption>%s</caption>\n", $caption;
114 print coltable_hsv(@{$mapinfo});
115 print "</table>\n\n";
118 if (my $colours = $info->{list}) {
119 if (my $reorder = $info->{ansiorder} and $get{v}) {
120 $colours = [ map { $colours->[$_] =~ s/:|$/:$_/r } @{$reorder} ];
123 print '<table class=color>', "\n";
124 printf "<caption>%s</caption>\n", $caption;
125 for my $num (0 .. $#{$colours}) {
126 my ($rgb, $name) = split /:/, $colours->[$num], 3;
128 $name = [ $name, [] ] if $term =~ /^msx/ and !$num;
129 $name = [ $name, ['#333'] ] if $term eq 'xkcd';
130 print '<tr>', colcell($name, $rgb);
133 print '<tr><td>', img_egapal(\@{$colours}) if $imgfile;
134 print "</table>\n\n";
139 my ($dim, $rgbval, $greyramp) = @_;
141 my $hmax = 2 * $dim * 3; # each face of the rgb cube
144 $rgbval ||= sub { join('', @_), map { int $_ * 255 / $vmax } @_ };
146 my %greymap; # name => value
147 my @colmap; # saturation => value => hue => [name, r,g,b]
148 my $offset = 16 * ($dim > 3);
150 for my $r (0 .. $dim - 1) {
151 for my $g (0 .. $dim - 1) {
152 for my $b (0 .. $dim - 1) {
153 my @rgb = ($r, $g, $b);
155 my $h = Shiar_Sheet::Colour->new(@rgb)->hue * $hmax;
157 my $s = abs(min(@rgb) - max(@rgb));
161 my ($index, $l) = $rgbval->(@rgb);
162 $greymap{$index} = $l;
166 $h = $hmax; # greyscale hue
167 $s = 1; # lowest saturation for other hues
168 $v = $s = $vmax if !$v; # black at full saturation
172 $s = $smax - $s - $v;
174 $colmap[$s][$v][$h] = [ $rgbval->(@rgb) ];
180 $out .= sprintf '<colgroup span=%d>', scalar @{$_} for @colmap;
181 my $huerow = $colmap[0][0]; # first {$_} map { @{$_} } @colmap;
182 for my $h (grep { $huerow->[$_] } 0 .. $#{$huerow}) {
184 $out .= colcell(@$_) for map { $_->[$h] } map { @{$_} } @colmap;
188 $offset += $dim ** 3;
189 $greymap{$offset++} = $_ for @{$greyramp};
195 my $colbreak = scalar map { @$_ } @colmap; # same width as hue rows
196 for my $num (sort { $greymap{$a} <=> $greymap{$b} } keys %greymap) {
197 $out .= '<tr>' unless $col++ % $colbreak;
198 $out .= colcell($num, ($greymap{$num}));
203 my @palette = map { [ @{$_}[1 .. 3] ] } map {@$_} map {@$_} @colmap;
204 my $imgdata = img_egapal(\@palette);
205 my $tablespan = scalar map { @$_ } @colmap;
206 $out .= "<tr><td colspan=$tablespan>$imgdata";