commit:d3be8a31ee5b53ce979c48510882a84f38b78452
author:Chip Black
committer:Chip Black
date:Mon Oct 26 02:38:53 2015 -0500
parents:ce1698fa9af084978dc7f2013e86f55714ba464e
Update palette generator to spit out Go code
diff --git a/palette/generate.pl b/palette/generate.pl
line changes: +42/-17
index f690786..d15ea89
--- a/palette/generate.pl
+++ b/palette/generate.pl
@@ -11,8 +11,8 @@ my $yellow = Convert::Color::HUSL->new(86, 100, 92);
 my $orange = Convert::Color::HUSL->new(47.6, 100, 71.2);
 my $purple = Convert::Color::HUSL->new(316, 100, 50);
 
-my @palette = (
-    [   # 0 - EGA 16-color
+my %palette = (
+    "0 - EGA 16-color" => [
         [0x00, 0x00, 0x00], # black
         [0x00, 0x00, 0xAA], # blue
         [0x00, 0xAA, 0x00], # green
@@ -30,48 +30,48 @@ my @palette = (
         [0xFF, 0xFF, 0x55], # bright yellow
         [0xFF, 0xFF, 0xFF], # white
     ],
-    [   # 1 - Gray/R/G/B
+    "1 - Gray/R/G/B" => [
         lightness_progression($white, 0.0, 0.25, 0.5, 0.75),
         map {
             lightness_progression($_, 0.4, 0.65, 0.9, 1.15)
         } ($red, $green, $blue),
     ],
-    [   # 2 - Red/Blue/Yellow
+    "2 - Red/Blue/Yellow" => [
         [0x00, 0x00, 0x00],
         map {
             lightness_progression($_, 0.3, 0.5, 0.7, 0.9, 1.1)
         } ($red, $blue, $yellow),
     ],
-    [   # 3 - Purple/Orange/Green
+    "3 - Purple/Orange/Green" => [
         [0x00, 0x00, 0x00],
         map {
             lightness_progression($_, 0.3, 0.5, 0.7, 0.9, 1.1)
         } ($purple, $orange, $green),
     ],
-    [   # 4 - Red/Green
+    "4 - Red/Green" => [
         map {
             lightness_progression($_, 0.2, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0)
         } ($red, $green),
     ],
-    [   # 5 - Orange/Blue
+    "5 - Orange/Blue" => [
         map {
             lightness_progression($_, 0.2, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0)
         } ($orange, $blue),
         # ILL!
         # INI!
     ],
-    [   # 6 - Yellow/Purple
+    "6 - Yellow/Purple" => [
         map {
             lightness_progression($_, 0.2, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0)
         } ($yellow, $purple),
     ],
-    [   # 7 - Red
+    "7 - Red" => [
         lightness_progression($red, map { $_ / 16 } 1..16),
     ],
-    [   # 8 - Green
+    "8 - Green" => [
         lightness_progression($green, map { $_ / 16 } 1..16),
     ],
-    [   # 9 - Blue
+    "9 - Blue" => [
         lightness_progression($blue, map { $_ / 16 } 1..16),
     ],
 );
@@ -88,21 +88,46 @@ sub print_color_js {
     say "    [", join(', ', map { sprintf("0x%02X", $_) } @$c), "],";
 }
 
-sub print_palette {
+sub print_palette_js {
     my $p = shift;
+    say "// $p";
     say "[";
-    for my $c (@{$palette[$p]}) {
+    for my $c (@{$palette{$p}}) {
         print_color_js($c);
     }
     say "],";
 }
 
+sub print_color_golang {
+    my $c = shift;
+    say "    RGB8{", join(', ', map { sprintf("0x%02X", $_) } @$c), "},";
+}
+
+sub print_palette_golang {
+    my $p = shift;
+    say "// $p";
+    say "color.Palette{";
+    for my $c (@{$palette{$p}}) {
+        print_color_golang($c);
+    }
+    say "},";
+}
+
+my $print_palette = \&print_palette_js;
+
+if ($ARGV[0] eq '--go') {
+    shift;
+    $print_palette = \&print_palette_golang;
+}
+
+my @names = sort { $a <=> $b } keys %palette;
+
 if (@ARGV) {
-    for my $p (@ARGV) {
-        print_palette($p);
+    for my $n (@ARGV) {
+        &$print_palette($names[$n]);
     }
 } else {
-    for my $p (0..$#palette) {
-        print_palette($p);
+    for my $p (@names) {
+        &$print_palette($p);
     }
 }