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+/, $Request || '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.';
38 use Shiar_Sheet::Colour 1.04;
39 use List::Util qw( min max );
42 my $palettes = do 'termcol.inc.pl';
43 die "Cannot open palette data: $_\n" for $@ || $! || ();
44 my $more = do 'termcol-xcolor.inc.pl' || {};
45 $palettes = {%$palettes, %$more};
48 my $name = shift // return "<td>\n";
49 my $col = Shiar_Sheet::Colour->new(@_);
50 my $minhex = $col->rgb24;
51 my $css = '#' . $col->rgb48;
52 my $inverse = '#' . sprintf('%X', $col->luminance/255 < .3 ? 12 : 0) x 3;
54 my $sample = [ qw(#000 #FFF) ];
55 ($name, $sample) = @$name if ref $name eq 'ARRAY';
57 my $out = sprintf('<td title="%s" style="%s">%s',
58 join(',', map { int } @$col),
59 "background:$css; color:$inverse",
62 $out .= sprintf('<samp style="%s"><small>%s</small></samp>',
63 "background:$_; color:$css", $minhex
69 my ($palette, $imgfile, $reindex) = @_;
74 my @imgpal = map { Imager::Color->new(ref $_ ? @$_ : $_) } @{$palette};
76 my $img = $imgcache->{$imgfile}
77 //= Imager->new(file => "data/palimage/$imgfile")
78 or die Imager->errstr.$/;
83 make_colors => 'none',
85 translate => 'closest',
89 @{[ $img->getcolors ]} == @imgpal
90 or die "incompatible palette size\n";
91 $img->setcolors(colors => \@imgpal);
94 }->write(data => \my $imgdata, type => 'png');
95 return sprintf '<img src="data:image/png;base64,%s">',
96 MIME::Base64::encode_base64($imgdata);
102 my $info = $palettes->{$term};
104 if (ref $info eq 'ARRAY') {
105 coltable($_) for @{$info};
108 ref $info eq 'HASH' or next;
110 my $caption = $info->{name} // $term;
111 $caption = sprintf('<%s %s>%s</%1$s>',
112 $info->{href} ? 'a' : 'span',
114 map { sprintf '%s="%s"', $_, $info->{$_} }
115 grep { defined $info->{$_} }
119 ) if $info->{href} or $info->{title};
121 if (my $mapinfo = $info->{rgbmap}) {
122 say '<table class="color mapped">';
123 say sprintf '<caption>%s</caption>', $caption;
124 print coltable_hsv(@{$mapinfo});
128 if (my $table = $info->{table}) {
129 say '<table class="color mapped">';
130 say sprintf '<caption>%s</caption>', $caption;
131 for my $row (@$table) {
133 print colcell(ref $_ ? @$_ : $_ ? reverse split /:/ : undef) for @$row;
137 my $width = scalar @{ $table->[0] };
139 [ ref $_ ? @{$_}[1 .. 3] : map {hex} /(..)(..)(..)/ ]
140 } map { @{$_} } @{$table};
142 print "<tr><td colspan=$width>", img_egapal(\@imgpal, @{$_});
148 if (my $palette = $info->{list}) {
149 my $order = $get{order} && $get{order}.'order';
150 my $colours = colorder($palette,
151 $info->{$order} // $palettes->{ $info->{parent} }->{$order}
155 my $columns = ceil(@{$palette} / $rows);
157 say '<table class=color>';
158 say sprintf '<caption>%s</caption>', $caption;
159 for my $row (0 .. $rows - 1) {
161 for my $col (0 .. $columns - 1) {
162 my $num = $row + $col * $rows;
163 my ($rgb, $name) = split /:/, $colours->[$num], 3;
164 $name //= $rgb && $num;
165 $name = [ $name, [] ] if $term =~ /^msx/ and !$num;
166 $name = [ $name, ['#333'] ] if $term eq 'xkcd';
167 print colcell($name, $rgb);
172 my $imgpal = colorder($palette,
173 $info->{ansiorder} // $palettes->{ $info->{parent} }->{ansiorder}
175 print "<tr><td colspan=$columns>", img_egapal($imgpal, @{$_});
182 my ($palette, $reorder) = @_;
183 return [ map { $palette->[$_] =~ s/:(?![^:])|$/:$_/r } @{$reorder} ]
189 my ($dim, $rgbval, $greyramp) = @_;
191 my $hmax = 2 * $dim * 3; # each face of the rgb cube
194 $rgbval ||= sub { join('', @_), map { int $_ * 255 / $vmax } @_ };
196 my @greymap = @{$greyramp || []}; # [name, r, g=l, b]
197 my @colmap; # saturation => value => hue => [name, r,g,b]
199 for my $r (0 .. $dim - 1) {
200 for my $g (0 .. $dim - 1) {
201 for my $b (0 .. $dim - 1) {
202 my @rgb = ($r, $g, $b);
204 my ($h, $s, $v) = Shiar_Sheet::Colour->new(@rgb)->hsv;
208 push @greymap, [ $rgbval->(@rgb) ];
212 $h = 1; # greyscale hue
213 $s = $smax - $v + 1; # spread brightness over saturation groups
214 $v &&= $smax # highest saturation
215 or $v = $s = 1; # black at initial column
220 $s = $smax - $s - $v;
222 $colmap[$s][$v][$h] = [ $rgbval->(@rgb) ];
228 $out .= sprintf '<colgroup span=%d>', scalar @{$_} for @colmap;
229 my $huerow = $colmap[0][0]; # first {$_} map { @{$_} } @colmap;
230 for my $h (grep { $huerow->[$_] } 0 .. $#{$huerow}) {
232 $out .= colcell(@$_) for map { $_->[$h] } map { reverse @{$_} } @colmap;
238 my $colbreak = scalar map { @$_ } @colmap; # same width as hue rows
239 for my $cell (sort { $a->[1] <=> $b->[1] || $a->[0] <=> $b->[0] } @greymap) {
240 $out .= '<tr>' unless $col++ % $colbreak;
241 $out .= colcell(@{$cell});
246 my @palette = map { [ @{$_}[1 .. 3] ] } @greymap, map {@$_} map {@$_} @colmap;
247 my $tablespan = scalar map { @$_ } @colmap;
248 my $imgdata = img_egapal(\@palette, @{ $draw[0] });
249 $out .= "<tr><td colspan=$tablespan>$imgdata";
255 coltable($_) for @termlist;