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 <h1>Terminal colours</h1>
27 <span title="ECMA-48">ANSI</span> (VT100, ISO-6429) 16-colour text palette
28 as implemented by various systems and programs.
31 "@termlist" ne 'default' ? 'Additional palettes are included as specified.' :
32 'Also see <a href="/termcol/more">8-bit legacy hardware</a> palettes.';
39 use Shiar_Sheet::Colour 1.04;
40 use List::Util qw( min max );
43 my $palettes = do 'termcol.inc.pl';
44 die "Cannot open palette data: $_\n" for $@ || $! || ();
45 my $more = do 'termcol-xcolor.inc.pl';
46 $palettes = {%$palettes, %$more};
49 my $name = shift // return "<td>\n";
50 my $col = Shiar_Sheet::Colour->new(@_);
51 my $minhex = $col->rgb24;
52 my $css = '#' . $col->rgb48;
53 my $inverse = '#' . sprintf('%X', $col->luminance/255 < .3 ? 12 : 0) x 3;
55 my $sample = [ qw(#000 #FFF) ];
56 ($name, $sample) = @$name if ref $name eq 'ARRAY';
58 my $out = sprintf('<td title="%s" style="%s">%s',
59 join(',', map { int } @$col),
60 "background:$css; color:$inverse",
63 $out .= sprintf('<samp style="%s"><small>%s</small></samp>',
64 "background:$_; color:$css", $minhex
70 my ($palette, $imgfile, $reindex) = @_;
75 my @imgpal = map { Imager::Color->new(ref $_ ? @$_ : $_) } @{$palette};
77 my $img = $imgcache->{$imgfile}
78 //= Imager->new(file => "data/palimage/$imgfile")
79 or die Imager->errstr.$/;
84 make_colors => 'none',
86 translate => 'closest',
90 @{[ $img->getcolors ]} == @imgpal
91 or die "incompatible palette size\n";
92 $img->setcolors(colors => \@imgpal);
95 }->write(data => \my $imgdata, type => 'png');
96 return sprintf '<img src="data:image/png;base64,%s">',
97 MIME::Base64::encode_base64($imgdata);
103 my $info = $palettes->{$term};
105 if (ref $info eq 'ARRAY') {
106 coltable($_) for @{$info};
109 ref $info eq 'HASH' or next;
111 my $caption = $info->{name} // $term;
112 $caption = sprintf('<%s %s>%s</%1$s>',
113 $info->{href} ? 'a' : 'span',
115 map { sprintf '%s="%s"', $_, $info->{$_} }
116 grep { defined $info->{$_} }
120 ) if $info->{href} or $info->{title};
122 if (my $mapinfo = $info->{rgbmap}) {
123 print '<table class="color mapped">'."\n";
124 printf "<caption>%s</caption>\n", $caption;
125 print coltable_hsv(@{$mapinfo});
126 print "</table>\n\n";
129 if (my $table = $info->{table}) {
130 print '<table class="color mapped">'."\n";
131 printf "<caption>%s</caption>\n", $caption;
132 for my $row (@$table) {
134 print colcell(ref $_ ? @$_ : $_ ? reverse split /:/ : undef) for @$row;
138 my $width = scalar @{ $table->[0] };
140 [ ref $_ ? @{$_}[1 .. 3] : map {hex} /(..)(..)(..)/ ]
141 } map { @{$_} } @{$table};
143 print "<tr><td colspan=$width>", img_egapal(\@imgpal, @{$_});
146 print "</table>\n\n";
149 if (my $palette = $info->{list}) {
150 my $order = $get{order} && $get{order}.'order';
151 my $colours = colorder($palette,
152 $info->{$order} // $palettes->{ $info->{parent} }->{$order}
156 my $columns = ceil(@{$palette} / $rows);
158 print '<table class=color>', "\n";
159 printf "<caption>%s</caption>\n", $caption;
160 for my $row (0 .. $rows - 1) {
162 for my $col (0 .. $columns - 1) {
163 my $num = $row + $col * $rows;
164 my ($rgb, $name) = split /:/, $colours->[$num], 3;
165 $name //= $rgb && $num;
166 $name = [ $name, [] ] if $term =~ /^msx/ and !$num;
167 $name = [ $name, ['#333'] ] if $term eq 'xkcd';
168 print colcell($name, $rgb);
173 my $imgpal = colorder($palette,
174 $info->{ansiorder} // $palettes->{ $info->{parent} }->{ansiorder}
176 print "<tr><td colspan=$columns>", img_egapal($imgpal, @{$_});
178 print "</table>\n\n";
183 my ($palette, $reorder) = @_;
184 return [ map { $palette->[$_] =~ s/:(?![^:])|$/:$_/r } @{$reorder} ]
190 my ($dim, $rgbval, $greyramp) = @_;
192 my $hmax = 2 * $dim * 3; # each face of the rgb cube
195 $rgbval ||= sub { join('', @_), map { int $_ * 255 / $vmax } @_ };
197 my @greymap = @{$greyramp || []}; # [name, r, g=l, b]
198 my @colmap; # saturation => value => hue => [name, r,g,b]
200 for my $r (0 .. $dim - 1) {
201 for my $g (0 .. $dim - 1) {
202 for my $b (0 .. $dim - 1) {
203 my @rgb = ($r, $g, $b);
205 my ($h, $s, $v) = Shiar_Sheet::Colour->new(@rgb)->hsv;
209 push @greymap, [ $rgbval->(@rgb) ];
213 $h = 1; # greyscale hue
214 $s = $smax - $v + 1; # spread brightness over saturation groups
215 $v &&= $smax # highest saturation
216 or $v = $s = 1; # black at initial column
221 $s = $smax - $s - $v;
223 $colmap[$s][$v][$h] = [ $rgbval->(@rgb) ];
229 $out .= sprintf '<colgroup span=%d>', scalar @{$_} for @colmap;
230 my $huerow = $colmap[0][0]; # first {$_} map { @{$_} } @colmap;
231 for my $h (grep { $huerow->[$_] } 0 .. $#{$huerow}) {
233 $out .= colcell(@$_) for map { $_->[$h] } map { reverse @{$_} } @colmap;
239 my $colbreak = scalar map { @$_ } @colmap; # same width as hue rows
240 for my $cell (sort { $a->[1] <=> $b->[1] || $a->[0] <=> $b->[0] } @greymap) {
241 $out .= '<tr>' unless $col++ % $colbreak;
242 $out .= colcell(@{$cell});
247 my @palette = map { [ @{$_}[1 .. 3] ] } @greymap, map {@$_} map {@$_} @colmap;
248 my $tablespan = scalar map { @$_ } @colmap;
249 my $imgdata = img_egapal(\@palette, @{ $draw[0] });
250 $out .= "<tr><td colspan=$tablespan>$imgdata";
256 coltable($_) for @termlist;