Debian Codegraph @

palette/brewer/generate_palettes.go

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