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 $@ || $! || ();
47 my $name = shift // return "<td>\n";
48 my $col = Shiar_Sheet::Colour->new(@_);
49 my $minhex = $col->rgb24;
50 my $css = '#' . $col->rgb48;
51 my $inverse = '#' . sprintf('%X', $col->luminance/255 < .3 ? 12 : 0) x 3;
53 my $sample = [ qw(#000 #FFF) ];
54 ($name, $sample) = @$name if ref $name eq 'ARRAY';
56 my $out = sprintf('<td title="%s" style="%s">%s',
57 join(',', map { int } @$col),
58 "background:$css; color:$inverse",
61 $out .= sprintf('<samp style="%s"><small>%s</small></samp>',
62 "background:$_; color:$css", $minhex
68 my ($palette, $imgfile, $reindex) = @_;
73 my @imgpal = map { Imager::Color->new(ref $_ ? @$_ : $_) } @{$palette};
75 my $img = $imgcache->{$imgfile}
76 //= Imager->new(file => "data/palimage/$imgfile")
77 or die Imager->errstr.$/;
82 make_colors => 'none',
84 translate => 'closest',
88 @{[ $img->getcolors ]} == @imgpal
89 or die "incompatible palette size\n";
90 $img->setcolors(colors => \@imgpal);
93 }->write(data => \my $imgdata, type => 'png');
94 return sprintf '<img src="data:image/png;base64,%s">',
95 MIME::Base64::encode_base64($imgdata);
101 my $info = $palettes->{$term};
103 if (ref $info eq 'ARRAY') {
104 coltable($_) for @{$info};
107 ref $info eq 'HASH' or next;
109 my $caption = $info->{name} // $term;
110 $caption = sprintf('<%s %s>%s</%1$s>',
111 $info->{href} ? 'a' : 'span',
113 map { sprintf '%s="%s"', $_, $info->{$_} }
114 grep { defined $info->{$_} }
118 ) if $info->{href} or $info->{title};
120 if (my $mapinfo = $info->{rgbmap}) {
121 print '<table class="color mapped">'."\n";
122 printf "<caption>%s</caption>\n", $caption;
123 print coltable_hsv(@{$mapinfo});
124 print "</table>\n\n";
127 if (my $table = $info->{table}) {
128 print '<table class="color mapped">'."\n";
129 printf "<caption>%s</caption>\n", $caption;
130 for my $row (@$table) {
132 print colcell(@$_) for @$row;
135 my $width = scalar @{ $table->[0] };
136 my @imgpal = map { [ @{$_}[1 .. 3] ] } map { @{$_} } @{$table};
138 print "<tr><td colspan=$width>", img_egapal(\@imgpal, @{$_});
140 print "</table>\n\n";
143 if (my $palette = $info->{list}) {
144 my $order = $get{order} && $get{order}.'order';
145 my $colours = colorder($palette,
146 $info->{$order} // $palettes->{ $info->{parent} }->{$order}
150 my $columns = ceil(@{$palette} / $rows);
152 print '<table class=color>', "\n";
153 printf "<caption>%s</caption>\n", $caption;
154 for my $row (0 .. $rows - 1) {
156 for my $col (0 .. $columns - 1) {
157 my $num = $row + $col * $rows;
158 my ($rgb, $name) = split /:/, $colours->[$num], 3;
159 $name //= $rgb && $num;
160 $name = [ $name, [] ] if $term =~ /^msx/ and !$num;
161 $name = [ $name, ['#333'] ] if $term eq 'xkcd';
162 print colcell($name, $rgb);
167 my $imgpal = colorder($palette,
168 $info->{ansiorder} // $palettes->{ $info->{parent} }->{ansiorder}
170 print "<tr><td colspan=$columns>", img_egapal($imgpal, @{$_});
172 print "</table>\n\n";
177 my ($palette, $reorder) = @_;
178 return [ map { $palette->[$_] =~ s/:(?![^:])|$/:$_/r } @{$reorder} ]
184 my ($dim, $rgbval, $greyramp) = @_;
186 my $hmax = 2 * $dim * 3; # each face of the rgb cube
189 $rgbval ||= sub { join('', @_), map { int $_ * 255 / $vmax } @_ };
191 my @greymap = @{$greyramp || []}; # [name, r, g=l, b]
192 my @colmap; # saturation => value => hue => [name, r,g,b]
194 for my $r (0 .. $dim - 1) {
195 for my $g (0 .. $dim - 1) {
196 for my $b (0 .. $dim - 1) {
197 my @rgb = ($r, $g, $b);
199 my ($h, $s, $v) = Shiar_Sheet::Colour->new(@rgb)->hsv;
203 push @greymap, [ $rgbval->(@rgb) ];
207 $h = 1; # greyscale hue
208 $s = $smax - $v + 1; # spread brightness over saturation groups
209 $v &&= $smax # highest saturation
210 or $v = $s = 1; # black at initial column
215 $s = $smax - $s - $v;
217 $colmap[$s][$v][$h] = [ $rgbval->(@rgb) ];
223 $out .= sprintf '<colgroup span=%d>', scalar @{$_} for @colmap;
224 my $huerow = $colmap[0][0]; # first {$_} map { @{$_} } @colmap;
225 for my $h (grep { $huerow->[$_] } 0 .. $#{$huerow}) {
227 $out .= colcell(@$_) for map { $_->[$h] } map { reverse @{$_} } @colmap;
233 my $colbreak = scalar map { @$_ } @colmap; # same width as hue rows
234 for my $cell (sort { $a->[1] <=> $b->[1] || $a->[0] <=> $b->[0] } @greymap) {
235 $out .= '<tr>' unless $col++ % $colbreak;
236 $out .= colcell(@{$cell});
241 my @palette = map { [ @{$_}[1 .. 3] ] } @greymap, map {@$_} map {@$_} @colmap;
242 my $tablespan = scalar map { @$_ } @colmap;
243 my $imgdata = img_egapal(\@palette, @{ $draw[0] });
244 $out .= "<tr><td colspan=$tablespan>$imgdata";
250 coltable($_) for @termlist;