1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package main
19
20 import (
21 "bufio"
22 "bytes"
23 "flag"
24 "fmt"
25 "os"
26 "strconv"
27 "strings"
28 )
29
30 var (
31 delim string
32 hex bool
33 )
34
35 func init() {
36 flag.StringVar(&delim, "d", "\t", "indicate field delimiter of input")
37 flag.BoolVar(&hex, "hex", true, "indicate color values output in hex format")
38 flag.Parse()
39 }
40
41 func mustAtoi(f string) byte {
42 i, err := strconv.Atoi(f)
43 if err != nil {
44 panic(err)
45 }
46 if i < 0 || i > 0xff {
47 panic(fmt.Sprintf("byte out of range", i))
48 }
49 return byte(i)
50 }
51
52 func main() {
53 fmt.Println(`// Apache-Style Software License for ColorBrewer software and ColorBrewer Color Schemes
54 //
55 // Copyright ©2002 Cynthia Brewer, Mark Harrower, and The Pennsylvania State University.
56 //
57 // Licensed under the Apache License, Version 2.0 (the "License");
58 // you may not use this file except in compliance with the License.
59 // You may obtain a copy of the License at
60 //
61 // http://www.apache.org/licenses/LICENSE-2.0
62 //
63 // Unless required by applicable law or agreed to in writing, software distributed
64 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
65 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
66 // specific language governing permissions and limitations under the License.
67 // Go implementation Copyright ©2013 The bíogo Authors. All rights reserved.
68 // Use of this source code is governed by a BSD-style
69 // license that can be found in the LICENSE file.
70
71 // Go port Copyright ©2015 The gonum Authors. All rights reserved.
72 // Use of this source code is governed by a BSD-style
73 // license that can be found in the LICENSE file.
74
75 // Go port Copyright ©2013 The bíogo Authors. All rights reserved.
76 // Use of this source code is governed by a BSD-style
77 // license that can be found in the LICENSE file.
78
79 // Palette Copyright ©2002 Cynthia Brewer, Mark Harrower, and The Pennsylvania State University.
80
81 package brewer
82
83 import (
84 "image/color"
85 )
86 `)
87 label := make(map[string]int)
88 scanner := bufio.NewScanner(os.Stdin)
89 var (
90 lastType string
91
92 last = make(map[string]string)
93
94 defBuf = map[string]*bytes.Buffer{
95 "Qualitative": {},
96 "Sequential": {},
97 "Diverging": {},
98 }
99
100 cls = map[string]string{"Diverging": "Diverging", "Qualitative": "NonDiverging", "Sequential": "NonDiverging"}
101
102 lookBuf = make(map[string][]string)
103 )
104
105 for scanner.Scan() {
106 line := scanner.Text()
107 if len(strings.TrimSpace(line)) == 0 {
108 break
109 }
110 fields := strings.Split(line, delim)
111 if fields[0] == "ColorName" {
112 for i, f := range fields {
113 label[f] = i
114 }
115 continue
116 }
117 if name := fields[label["ColorName"]]; len(name) != 0 {
118 if len(fields) > label["SchemeType"] {
119 if typ := fields[label["SchemeType"]]; len(typ) != 0 {
120 lastType = typ
121 }
122 }
123 if name != last[lastType] {
124 lookBuf[lastType] = append(lookBuf[lastType], fmt.Sprintf("%q: %s", name, name))
125 if last[lastType] != "" {
126 fmt.Fprintf(defBuf[lastType], "\t\t\t},\n\t\t},\n\t}\n")
127 }
128 fmt.Fprintf(defBuf[lastType], "\t%s %s = %s{\n", fields[label["ColorName"]], lastType, lastType)
129 last[lastType] = name
130 } else {
131 fmt.Fprintf(defBuf[lastType], "\t\t\t},\n\t\t},\n")
132 }
133 fmt.Fprintf(defBuf[lastType], "\t\t%d: %sPalette{\n\t\t\tID: %q,\n\t\t\tColor: []color.Color{\n",
134 mustAtoi(fields[label["NumOfColors"]]), cls[lastType], last[lastType])
135 }
136 values := []interface{}{
137 fields[label["ColorLetter"]],
138 mustAtoi(fields[label["R"]]),
139 mustAtoi(fields[label["G"]]),
140 mustAtoi(fields[label["B"]]),
141 }
142 if hex {
143 fmt.Fprintf(defBuf[lastType], "\t\t\t\tColor{'%s', color.RGBA{0x%02x, 0x%02x, 0x%02x, 0xff}},\n", values...)
144 } else {
145 fmt.Fprintf(defBuf[lastType], "\t\t\t\tColor{'%s', color.RGBA{0x%02x, 0x%02x, 0x%02x, 0xff}},\n", values...)
146 }
147 }
148 if err := scanner.Err(); err != nil {
149 fmt.Fprintln(os.Stderr, "reading standard input:", err)
150 os.Exit(1)
151 }
152 for _, typ := range []string{"Diverging", "Qualitative", "Sequential"} {
153 fmt.Printf("var (\n%s\t\t\t},\n\t\t},\n\t}\n)\n", defBuf[typ].Bytes())
154 }
155 fmt.Println("\nvar (")
156 for _, typ := range []string{"Diverging", "Qualitative", "Sequential"} {
157 fmt.Printf("\t%s = map[string]%s{\n\t\t%v,\n\t}\n", strings.ToLower(typ), typ, strings.Join(lookBuf[typ], ",\n\t\t"))
158 }
159 fmt.Println("\tall = map[string]interface{}{")
160 for _, typ := range []string{"Diverging", "Qualitative", "Sequential"} {
161 fmt.Printf("\t\t%v,\n", strings.Join(lookBuf[typ], ",\n\t\t"))
162 }
163 fmt.Println("\t}\n)")
164 }
165
View as plain text