keyboard: fix empty key titles
[sheet.git] / tools / mktermcol-xcolor
index 59c4a259bba9eb630fc3bad646121725f286f09f..7f39c80c4ca6332805c108d80c0c362da8c24623 100755 (executable)
@@ -1,23 +1,31 @@
 #!/usr/bin/env perl
-use 5.010;
+use 5.014;
 
-use Shiar_Sheet::Colour '1.05';
+use Shiar_Sheet::Colour 1.05;
 
+say "# automatically generated by $0";
 say 'use strict;';
 say '+{';
 my @names;
+my %seen;
 
-for my $path (glob 'data/xcolors/themes/*') {
-       open my $theme, '<', $path;
+for my $path (@ARGV) {
+       open my $theme, '<', $path or do {
+               warn "could not open $path: $!\n";
+               next;
+       };
        (my $name = $path) =~ s{.*/}{};  # basename
 
-       my @pal;
+       my (%pal, @pal);
        while (readline $theme) {
-               m{ color(\d+) \h* : \h* ( \#(\S+) | rgb:(\S+) ) }x or next;
-               my ($idx, $val) = ($1, uc $+);
-               $idx < 16 or next;
+               m{
+                       (?: (foreground | background) | color(\d+) ) \h* : \h*
+                       (?: \#(\S+) | rgb:(\S+) )
+               }x or next;
+               my ($name, $idx, $val) = ($1, $2, uc $+);
+               $name or $idx < 16 or next;
                $val =~ s/[^0-9A-F]//g;
-               $pal[$idx] = $val;
+               ($name ? $pal{$name} : $pal[$idx]) = $val;
        }
 
        my $huesum = 0;
@@ -34,10 +42,18 @@ for my $path (glob 'data/xcolors/themes/*') {
        $huesum > 3 or next;  # require number of significant hue changes
        #TODO tweak to include good-pants, exclude cheesecake-*
 
+       if ($seen{"@pal"}++) {
+               warn "ignore duplicate palette $name\n";
+               next;
+       }
+
+       splice @pal, 8 if "@pal[0..7]" eq "@pal[8..15]";
+
        say qq("$name" => {);
        say qq(\ttitle => '$name',);
        say qq(\tparent => 'cga',);
        say qq(\tlist => [qw(@pal)],);
+       say qq(\t$_ => '$pal{$_}',) for keys %pal;
        say qq(},);
        push @names, $name;
 }