+sub img_egapal {
+ my ($palette, $imgfile, $reindex) = @_;
+ return eval {
+ require Imager;
+ require MIME::Base64;
+
+ my @imgpal = map { Imager::Color->new(ref $_ ? @$_ : $_) } @{$palette};
+ state $imgcache = {};
+ my $img = $imgcache->{$imgfile}
+ //= Imager->new(file => "data/palimage/$imgfile")
+ or die Imager->errstr.$/;
+
+ do {
+ if ($reindex) {
+ $img->to_paletted({
+ make_colors => 'none',
+ colors => \@imgpal,
+ translate => 'closest',
+ });
+ }
+ else {
+ @{[ $img->getcolors ]} == @imgpal
+ or die "incompatible palette size\n";
+ $img->setcolors(colors => \@imgpal);
+ $img;
+ }
+ }->write(data => \my $imgdata, type => 'png');
+ return sprintf '<img src="data:image/png;base64,%s">',
+ MIME::Base64::encode_base64($imgdata);
+ } || $@;
+}
+
+sub coltable {
+ my ($term) = @_;
+ my $info = $palettes->{$term};
+
+ if (ref $info eq 'ARRAY') {
+ coltable($_) for @{$info};
+ return;
+ }
+
+ if (ref $info eq 'CODE') {
+ coltable($_) for $info->($palettes);
+ return;
+ }
+
+ ref $info eq 'HASH' or return;
+ my $order = $get{order} && $get{order}.'order';
+ my $reorder = $info->{$order} // $palettes->{ $info->{parent} }->{$order};
+
+ my $caption = $info->{name} // $term;
+ $caption = sprintf('<%s %s>%s</%1$s>',
+ $info->{href} ? 'a' : 'span',
+ join(' ',
+ map { sprintf '%s="%s"', $_, $info->{$_} }
+ grep { defined $info->{$_} }
+ qw( href title )
+ ),
+ $caption,
+ ) if $info->{href} or $info->{title};
+
+ if ($info->{table} or $info->{rgbmap}) {
+ say '<table class="color mapped">';
+ say sprintf '<caption>%s</caption>', $caption;
+
+ print coltable_hsv(@{$_}) for $info->{rgbmap} || ();
+
+ if (my $table = $info->{table}) {
+ $table = [ @{$table}[@{$reorder}] ] if $reorder;
+
+ for my $row (@$table) {
+ if (!$row) {
+ say '<tbody>';
+ next;
+ }
+ print '<tr>';
+ print colcell(ref $_ ? @$_ : $_ ? reverse split /:/ : undef) for @$row;
+ }
+
+ if (@draw) {
+ my $width = scalar @{ $table->[0] };
+ my @imgpal = map {
+ [ ref $_ ? @{$_}[1 .. 3] : map {hex} /(..)(..)(..)/ ]
+ } map { @{$_} } @{$table};
+ for (@draw) {
+ print "<tr><td colspan=$width>", img_egapal(\@imgpal, @{$_});
+ }
+ }