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'],
18 push @termlist, split /\W+/, $ENV{PATH_INFO} || 'default';
21 default => [qw( ansi xkcd ansi88 )],
22 more => [qw( ansi legacy ansi256 )],
23 msx => [qw( msx1 msx2 arnejmp )],
24 ansi => [qw( cga xterm tango app html )],
25 legacy => [qw( c64 msx2 mac2 risc arnegame cpc )],
27 @{$_} = map { $termgroup{$_} ? @{ $termgroup{$_} } : $_ } @{$_}
28 for values %termgroup, \@termlist;
31 <h1>Terminal colours</h1>
34 <span title="ECMA-48">ANSI</span> (VT100, ISO-6429) 16-colour text palette
35 as implemented by various systems and programs.
38 "@termlist" ne "@{ $termgroup{default} }" ? 'Additional palettes are included as specified.' :
39 'Also see <a href="/termcol/more">8-bit legacy hardware</a> palettes.';
45 use Shiar_Sheet::Colour '1.03';
46 use List::Util qw( min max );
48 my $palettes = do 'termcol.inc.pl';
49 die "Cannot open palette data: $_\n" for $@ || $! || ();
52 my $name = shift // return "<td colspan=3>\n";
53 my $col = Shiar_Sheet::Colour->new(@_);
54 my $minhex = $col->rgb24;
55 my $css = '#' . $col->rgb48;
56 my $inverse = '#' . sprintf('%X', $col->luminance/255 < .3 ? 12 : 0) x 3;
58 my $sample = [ qw(#000 #FFF) ];
59 ($name, $sample) = @$name if ref $name eq 'ARRAY';
61 my $out = sprintf('<th title="%s" style="%s">%s',
62 join(',', map { int } @$col),
63 "background:$css; color:$inverse",
66 $out .= sprintf '<td style="%s">%s', "background:$_; color:$css", $minhex
71 for my $term (@termlist) {
72 my $info = $palettes->{$term};
73 ref $info eq 'HASH' or next;
74 my $caption = $info->{name} // $term;
75 $caption = sprintf('<%s %s>%s</%1$s>',
76 $info->{href} ? 'a' : 'span',
78 map { sprintf '%s="%s"', $_, $info->{$_} }
79 grep { defined $info->{$_} }
83 ) if $info->{href} or $info->{title};
85 if (my $mapinfo = $info->{rgbmap}) {
86 print '<table class="color mapped">'."\n";
87 printf "<caption>%s</caption>\n", $caption;
88 print coltable_hsv(@{$mapinfo});
92 if (my $colours = $info->{list}) {
93 if (my $reorder = $info->{ansiorder} and $get{v}) {
94 $colours = [ map { $colours->[$_] =~ s/:|$/:$_/r } @{$reorder} ];
97 print '<table class=color>', "\n";
98 printf "<caption>%s</caption>\n", $caption;
99 for my $num (0 .. $#{$colours}) {
100 my ($rgb, $name) = split /:/, $colours->[$num], 3;
102 $name = [ $name, [] ] if $term =~ /^msx/ and !$num;
103 $name = [ $name, ['#333'] ] if $term eq 'xkcd';
104 print '<tr>', colcell($name, $rgb);
106 print "</table>\n\n";
111 my ($dim, $rgbval, $greyramp) = @_;
113 my $hmax = 2 * $dim * 3; # each face of the rgb cube
116 $rgbval ||= sub { join('', @_), map { int $_ * 255 / $vmax } @_ };
119 my %greymap; # name => value
120 my @colmap; # saturation => value => hue => [name, r,g,b]
121 my $offset = 16 * ($dim > 3);
123 for my $r (0 .. $dim - 1) {
124 for my $g (0 .. $dim - 1) {
125 for my $b (0 .. $dim - 1) {
126 my @rgb = ($r, $g, $b);
128 my $h = Shiar_Sheet::Colour->new(@rgb)->hue * $hmax;
130 my $s = abs(min(@rgb) - max(@rgb));
133 my ($index, $l) = $rgbval->(@rgb);
134 $greymap{$index} = $l;
139 $s = $smax - $s - $v;
141 $colmap[$s][$v][$h] = [ $rgbval->(@rgb) ];
147 $out .= sprintf '<colgroup span=%d>', 3 * @{$_} for @colmap;
148 my $huerow = $colmap[0][0]; # first {$_} map { @{$_} } @colmap;
149 for my $h (grep { $huerow->[$_] } 0 .. $#{$huerow}) {
151 $out .= colcell(@$_) for map { $_->[$h] } map { @{$_} } @colmap;
154 $offset += $dim ** 3;
155 $greymap{$offset++} = $_ for @{$greyramp};
159 my $colbreak = scalar map { @$_ } @colmap; # same width as hue rows
160 for my $num (sort { $greymap{$a} <=> $greymap{$b} } keys %greymap) {
161 $out .= '<tr>' unless $col++ % $colbreak;
162 $out .= colcell($num, ($greymap{$num}) x 3);