7f39c80c4ca6332805c108d80c0c362da8c24623
[sheet.git] / tools / mktermcol-xcolor
1 #!/usr/bin/env perl
2 use 5.014;
3
4 use Shiar_Sheet::Colour 1.05;
5
6 say "# automatically generated by $0";
7 say 'use strict;';
8 say '+{';
9 my @names;
10 my %seen;
11
12 for my $path (@ARGV) {
13         open my $theme, '<', $path or do {
14                 warn "could not open $path: $!\n";
15                 next;
16         };
17         (my $name = $path) =~ s{.*/}{};  # basename
18
19         my (%pal, @pal);
20         while (readline $theme) {
21                 m{
22                         (?: (foreground | background) | color(\d+) ) \h* : \h*
23                         (?: \#(\S+) | rgb:(\S+) )
24                 }x or next;
25                 my ($name, $idx, $val) = ($1, $2, uc $+);
26                 $name or $idx < 16 or next;
27                 $val =~ s/[^0-9A-F]//g;
28                 ($name ? $pal{$name} : $pal[$idx]) = $val;
29         }
30
31         my $huesum = 0;
32         for my $hue (
33                 sort map { $_->hue }
34                 grep { ($_->hsv)[1] > 32 }  # ignore unsaturated
35                 map { Shiar_Sheet::Colour->new($_) }
36                 @pal
37         ) {
38                 state $lasthue;
39                 $huesum += abs($lasthue - $hue) > .02 if defined $lasthue;
40                 $lasthue = $hue;
41         }
42         $huesum > 3 or next;  # require number of significant hue changes
43         #TODO tweak to include good-pants, exclude cheesecake-*
44
45         if ($seen{"@pal"}++) {
46                 warn "ignore duplicate palette $name\n";
47                 next;
48         }
49
50         splice @pal, 8 if "@pal[0..7]" eq "@pal[8..15]";
51
52         say qq("$name" => {);
53         say qq(\ttitle => '$name',);
54         say qq(\tparent => 'cga',);
55         say qq(\tlist => [qw(@pal)],);
56         say qq(\t$_ => '$pal{$_}',) for keys %pal;
57         say qq(},);
58         push @names, $name;
59 }
60
61 say 'xcolor => [', join(', ', map {"'$_'"} @names), '],';
62 say '}';