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 = $get{img} // 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
81 my @imgpal = map { Imager::Color->new(ref $_ ? @$_ : $_) } @{$palette};
82 state $img = Imager->new(file => "data/$imgfile")
83 or die Imager->errstr.$/;
84 @{[ $img->getcolors ]} == @imgpal
85 or die "incompatible palette size\n";
86 $img->setcolors(colors => \@imgpal);
87 $img->write(data => \my $imgdata, type => 'png');
88 return sprintf '<img src="data:image/png;base64,%s">',
89 MIME::Base64::encode_base64($imgdata);
93 for my $term (@termlist) {
94 my $info = $palettes->{$term};
95 ref $info eq 'HASH' or next;
96 my $caption = $info->{name} // $term;
97 $caption = sprintf('<%s %s>%s</%1$s>',
98 $info->{href} ? 'a' : 'span',
100 map { sprintf '%s="%s"', $_, $info->{$_} }
101 grep { defined $info->{$_} }
105 ) if $info->{href} or $info->{title};
107 if (my $mapinfo = $info->{rgbmap}) {
108 print '<table class="color mapped">'."\n";
109 printf "<caption>%s</caption>\n", $caption;
110 print coltable_hsv(@{$mapinfo});
111 print "</table>\n\n";
114 if (my $colours = $info->{list}) {
115 if (my $reorder = $info->{ansiorder} and $get{v}) {
116 $colours = [ map { $colours->[$_] =~ s/:|$/:$_/r } @{$reorder} ];
119 print '<table class=color>', "\n";
120 printf "<caption>%s</caption>\n", $caption;
121 for my $num (0 .. $#{$colours}) {
122 my ($rgb, $name) = split /:/, $colours->[$num], 3;
124 $name = [ $name, [] ] if $term =~ /^msx/ and !$num;
125 $name = [ $name, ['#333'] ] if $term eq 'xkcd';
126 print '<tr>', colcell($name, $rgb);
129 print '<tr><td>', img_egapal(\@{$colours}) if $imgfile;
130 print "</table>\n\n";
135 my ($dim, $rgbval, $greyramp) = @_;
137 my $hmax = 2 * $dim * 3; # each face of the rgb cube
140 $rgbval ||= sub { join('', @_), map { int $_ * 255 / $vmax } @_ };
142 my %greymap; # name => value
143 my @colmap; # saturation => value => hue => [name, r,g,b]
144 my $offset = 16 * ($dim > 3);
146 for my $r (0 .. $dim - 1) {
147 for my $g (0 .. $dim - 1) {
148 for my $b (0 .. $dim - 1) {
149 my @rgb = ($r, $g, $b);
151 my $h = Shiar_Sheet::Colour->new(@rgb)->hue * $hmax;
153 my $s = abs(min(@rgb) - max(@rgb));
157 my ($index, $l) = $rgbval->(@rgb);
158 $greymap{$index} = $l;
162 $h = $hmax; # greyscale hue
163 $s = 1; # lowest saturation for other hues
164 $v = $s = $vmax if !$v; # black at full saturation
168 $s = $smax - $s - $v;
170 $colmap[$s][$v][$h] = [ $rgbval->(@rgb) ];
176 $out .= sprintf '<colgroup span=%d>', scalar @{$_} for @colmap;
177 my $huerow = $colmap[0][0]; # first {$_} map { @{$_} } @colmap;
178 for my $h (grep { $huerow->[$_] } 0 .. $#{$huerow}) {
180 $out .= colcell(@$_) for map { $_->[$h] } map { @{$_} } @colmap;
184 $offset += $dim ** 3;
185 $greymap{$offset++} = $_ for @{$greyramp};
191 my $colbreak = scalar map { @$_ } @colmap; # same width as hue rows
192 for my $num (sort { $greymap{$a} <=> $greymap{$b} } keys %greymap) {
193 $out .= '<tr>' unless $col++ % $colbreak;
194 $out .= colcell($num, ($greymap{$num}));
199 my @palette = map { [ @{$_}[1 .. 3] ] } map {@$_} map {@$_} @colmap;
200 my $imgdata = img_egapal(\@palette);
201 my $tablespan = scalar map { @$_ } @colmap;
202 $out .= "<tr><td colspan=$tablespan>$imgdata";