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 @draw = map { [$_, s/\W+\z//] } grep { $_ } split m(/),
18 $get{img} // exists $get{img} && 'indi.png';
21 push @termlist, split /\W+/, $ENV{PATH_INFO} || 'default';
24 default => [qw( ansi xkcd ansi88 )],
25 more => [qw( ansi mirc legacy ansi256 )],
26 msx => [qw( msx1 msx2 arnejmp )],
27 ansi => [qw( cga xterm tango app html )],
28 legacy => [qw( c64 msx2 mac2 risc arnegame cpc )],
30 @{$_} = map { $termgroup{$_} ? @{ $termgroup{$_} } : $_ } @{$_}
31 for values %termgroup, \@termlist;
34 <h1>Terminal colours</h1>
37 <span title="ECMA-48">ANSI</span> (VT100, ISO-6429) 16-colour text palette
38 as implemented by various systems and programs.
41 "@termlist" ne "@{ $termgroup{default} }" ? 'Additional palettes are included as specified.' :
42 'Also see <a href="/termcol/more">8-bit legacy hardware</a> palettes.';
49 use Shiar_Sheet::Colour '1.04';
50 use List::Util qw( min max );
53 my $palettes = do 'termcol.inc.pl';
54 die "Cannot open palette data: $_\n" for $@ || $! || ();
57 my $name = shift // return "<td>\n";
58 my $col = Shiar_Sheet::Colour->new(@_);
59 my $minhex = $col->rgb24;
60 my $css = '#' . $col->rgb48;
61 my $inverse = '#' . sprintf('%X', $col->luminance/255 < .3 ? 12 : 0) x 3;
63 my $sample = [ qw(#000 #FFF) ];
64 ($name, $sample) = @$name if ref $name eq 'ARRAY';
66 my $out = sprintf('<td title="%s" style="%s">%s',
67 join(',', map { int } @$col),
68 "background:$css; color:$inverse",
71 $out .= sprintf('<samp style="%s"><small>%s</small></samp>',
72 "background:$_; color:$css", $minhex
78 my ($palette, $imgfile, $reindex) = @_;
83 my @imgpal = map { Imager::Color->new(ref $_ ? @$_ : $_) } @{$palette};
85 my $img = $imgcache->{$imgfile}
86 //= Imager->new(file => "data/palimage/$imgfile")
87 or die Imager->errstr.$/;
92 make_colors => 'none',
94 translate => 'closest',
98 @{[ $img->getcolors ]} == @imgpal
99 or die "incompatible palette size\n";
100 $img->setcolors(colors => \@imgpal);
103 }->write(data => \my $imgdata, type => 'png');
104 return sprintf '<img src="data:image/png;base64,%s">',
105 MIME::Base64::encode_base64($imgdata);
109 for my $term (@termlist) {
110 my $info = $palettes->{$term};
111 ref $info eq 'HASH' or next;
112 my $caption = $info->{name} // $term;
113 $caption = sprintf('<%s %s>%s</%1$s>',
114 $info->{href} ? 'a' : 'span',
116 map { sprintf '%s="%s"', $_, $info->{$_} }
117 grep { defined $info->{$_} }
121 ) if $info->{href} or $info->{title};
123 if (my $mapinfo = $info->{rgbmap}) {
124 print '<table class="color mapped">'."\n";
125 printf "<caption>%s</caption>\n", $caption;
126 print coltable_hsv(@{$mapinfo});
127 print "</table>\n\n";
130 if (my $colours = $info->{list}) {
131 if (my $reorder = $info->{ansiorder} and $get{v}) {
132 $colours = [ map { $colours->[$_] =~ s/:|$/:$_/r } @{$reorder} ];
136 my $columns = ceil(@{$colours} / $rows);
138 print '<table class=color>', "\n";
139 printf "<caption>%s</caption>\n", $caption;
140 for my $row (0 .. $rows - 1) {
142 for my $col (0 .. $columns - 1) {
143 my $num = $row + $col * $rows;
144 my ($rgb, $name) = split /:/, $colours->[$num], 3;
145 $name //= $rgb && $num;
146 $name = [ $name, [] ] if $term =~ /^msx/ and !$num;
147 $name = [ $name, ['#333'] ] if $term eq 'xkcd';
148 print colcell($name, $rgb);
152 print "<tr><td colspan=$columns>", img_egapal(\@{$colours}, @{$_}) for @draw;
153 print "</table>\n\n";
158 my ($dim, $rgbval, $greyramp) = @_;
160 my $hmax = 2 * $dim * 3; # each face of the rgb cube
163 $rgbval ||= sub { join('', @_), map { int $_ * 255 / $vmax } @_ };
165 my @greymap = @{$greyramp || []}; # [name, r, g=l, b]
166 my @colmap; # saturation => value => hue => [name, r,g,b]
168 for my $r (0 .. $dim - 1) {
169 for my $g (0 .. $dim - 1) {
170 for my $b (0 .. $dim - 1) {
171 my @rgb = ($r, $g, $b);
173 my ($h, $s, $v) = Shiar_Sheet::Colour->new(@rgb)->hsv;
177 push @greymap, [ $rgbval->(@rgb) ];
181 $h = 1; # greyscale hue
182 $s = 1; # lowest saturation for other hues
183 $v = $s = $vmax if !$v; # black at full saturation
188 $s = $smax - $s - $v;
190 $colmap[$s][$v][$h] = [ $rgbval->(@rgb) ];
196 $out .= sprintf '<colgroup span=%d>', scalar @{$_} for @colmap;
197 my $huerow = $colmap[0][0]; # first {$_} map { @{$_} } @colmap;
198 for my $h (grep { $huerow->[$_] } 0 .. $#{$huerow}) {
200 $out .= colcell(@$_) for map { $_->[$h] } map { @{$_} } @colmap;
206 my $colbreak = scalar map { @$_ } @colmap; # same width as hue rows
207 for my $cell (sort { $a->[1] <=> $b->[1] || $a->[0] <=> $b->[0] } @greymap) {
208 $out .= '<tr>' unless $col++ % $colbreak;
209 $out .= colcell(@{$cell});
214 my @palette = map { [ @{$_}[1 .. 3] ] } @greymap, map {@$_} map {@$_} @colmap;
215 my $tablespan = scalar map { @$_ } @colmap;
216 my $imgdata = img_egapal(\@palette, @{ $draw[0] });
217 $out .= "<tr><td colspan=$tablespan>$imgdata";