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 <h1>Terminal colours</h1>
21 <span title="ECMA-48">ANSI</span> (VT100, ISO-6429) 16-colour text palette
22 as implemented by various systems and programs.
25 !exists $get{v} ? 'Also see <a href="?v">8-bit legacy hardware</a> palettes.' :
26 'Also included are 8-bit legacy hardware palettes.';
32 use Shiar_Sheet::Colour '1.03';
33 use List::Util qw( min max );
35 my $palettes = do 'termcol.inc.pl';
36 die "Cannot open palette data: $_\n" for $@ || $! || ();
39 my $name = shift // return "<td colspan=3>\n";
40 my $col = Shiar_Sheet::Colour->new(@_);
41 my $minhex = $col->rgb24;
42 my $css = '#' . $col->rgb48;
43 my $inverse = '#' . sprintf('%X', $col->luminance/255 < .3 ? 12 : 0) x 3;
45 my $sample = [ qw(#000 #FFF) ];
46 ($name, $sample) = @$name if ref $name eq 'ARRAY';
48 my $out = sprintf('<td title="%s" style="%s">%s',
49 join(',', map { int } @$col),
50 "background:$css; color:$inverse; padding:0 1ex",
53 $out .= sprintf '<td style="%s"><code>%s</code>', "background:$_; color:$css", $minhex
58 my @termlist = qw( cga xterm tango app html xkcd );
59 push @termlist, qw( c64 msx2 mac2 risc arnegame cpc ) if exists $get{v};
60 push @termlist, qw( ansi88 );
61 push @termlist, qw( ansi256 ) if $ENV{PATH_INFO} =~ /256/;
63 for my $term (@termlist) {
64 my $info = $palettes->{$term};
65 ref $info eq 'HASH' or next;
66 my $caption = $info->{name} // $term;
67 $caption = sprintf('<%s %s>%s</%1$s>',
68 $info->{href} ? 'a' : 'span',
70 map { sprintf '%s="%s"', $_, $info->{$_} }
71 grep { defined $info->{$_} }
75 ) if $info->{href} or $info->{title};
77 if (my $mapinfo = $info->{rgbmap}) {
78 print '<table class="mapped">'."\n";
79 printf "<caption>%s</caption>\n", $caption;
80 print coltable_hsv(@{$mapinfo});
84 if (my $colours = $info->{list}) {
85 if (my $reorder = $info->{ansiorder} and $get{v}) {
86 $colours = [ map { $colours->[$_] =~ s/:|$/:$_/r } @{$reorder} ];
89 print '<table>', "\n";
90 printf "<caption>%s</caption>\n", $caption;
91 for my $num (0 .. $#{$colours}) {
92 my ($rgb, $name) = split /:/, $colours->[$num], 3;
94 $name = [ $name, [] ] if $term =~ /^msx/ and !$num;
95 $name = [ $name, ['#333'] ] if $term eq 'xkcd';
96 print '<tr>', colcell($name, $rgb);
103 my ($dim, $rgbval, $greyramp) = @_;
105 my $hmax = 2 * $dim * 3; # each face of the rgb cube
108 $rgbval ||= sub { join('', @_), map { int $_ * 255 / $vmax } @_ };
111 my %greymap; # name => value
112 my @colmap; # saturation => value => hue => [name, r,g,b]
113 my $offset = 16 * ($dim > 3);
115 for my $r (0 .. $dim - 1) {
116 for my $g (0 .. $dim - 1) {
117 for my $b (0 .. $dim - 1) {
118 my @rgb = ($r, $g, $b);
120 my $h = Shiar_Sheet::Colour->new(@rgb)->hue * $hmax;
122 my $s = abs(min(@rgb) - max(@rgb));
125 my ($index, $l) = $rgbval->(@rgb);
126 $greymap{$index} = $l;
131 $s = $smax - $s - $v;
133 $colmap[$s][$v][$h] = [ $rgbval->(@rgb) ];
139 $out .= sprintf '<colgroup span=%d>', 3 * @{$_} for @colmap;
140 my $huerow = $colmap[0][0]; # first {$_} map { @{$_} } @colmap;
141 for my $h (grep { $huerow->[$_] } 0 .. $#{$huerow}) {
143 $out .= colcell(@$_) for map { $_->[$h] } map { @{$_} } @colmap;
146 $offset += $dim ** 3;
147 $greymap{$offset++} = $_ for @{$greyramp};
151 my $colbreak = scalar map { @$_ } @colmap; # same width as hue rows
152 for my $num (sort { $greymap{$a} <=> $greymap{$b} } keys %greymap) {
153 $out .= '<tr>' unless $col++ % $colbreak;
154 $out .= colcell($num, ($greymap{$num}) x 3);