aboutsummaryrefslogtreecommitdiff
path: root/vendor/golang.org/x/image/colornames/gen.go
blob: d46e96862adce2b9a441f8c61f9359d9757e748a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build ignore

// This program generates table.go from
// http://www.w3.org/TR/SVG/types.html#ColorKeywords
package main

import (
	"bytes"
	"fmt"
	"go/format"
	"image/color"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"regexp"
	"sort"
	"strconv"
	"strings"

	"golang.org/x/net/html"
	"golang.org/x/net/html/atom"
)

// matchFunc matches HTML nodes.
type matchFunc func(*html.Node) bool

// appendAll recursively traverses the parse tree rooted under the provided
// node and appends all nodes matched by the matchFunc to dst.
func appendAll(dst []*html.Node, n *html.Node, mf matchFunc) []*html.Node {
	if mf(n) {
		dst = append(dst, n)
	}
	for c := n.FirstChild; c != nil; c = c.NextSibling {
		dst = appendAll(dst, c, mf)
	}
	return dst
}

// matchAtom returns a matchFunc that matches a Node with the specified Atom.
func matchAtom(a atom.Atom) matchFunc {
	return func(n *html.Node) bool {
		return n.DataAtom == a
	}
}

// matchAtomAttr returns a matchFunc that matches a Node with the specified
// Atom and a html.Attribute's namespace, key and value.
func matchAtomAttr(a atom.Atom, namespace, key, value string) matchFunc {
	return func(n *html.Node) bool {
		return n.DataAtom == a && getAttr(n, namespace, key) == value
	}
}

// getAttr fetches the value of a html.Attribute for a given namespace and key.
func getAttr(n *html.Node, namespace, key string) string {
	for _, attr := range n.Attr {
		if attr.Namespace == namespace && attr.Key == key {
			return attr.Val
		}
	}
	return ""
}

// re extracts RGB values from strings like "rgb( 0, 223, 128)".
var re = regexp.MustCompile(`rgb\(\s*([0-9]+),\s*([0-9]+),\s*([0-9]+)\)`)

// parseRGB parses a color from a string like "rgb( 0, 233, 128)". It sets
// the alpha value of the color to full opacity.
func parseRGB(s string) (color.RGBA, error) {
	m := re.FindStringSubmatch(s)
	if m == nil {
		return color.RGBA{}, fmt.Errorf("malformed color: %q", s)
	}
	var rgb [3]uint8
	for i, t := range m[1:] {
		num, err := strconv.ParseUint(t, 10, 8)
		if err != nil {
			return color.RGBA{}, fmt.Errorf("malformed value %q in %q: %s", t, s, err)
		}
		rgb[i] = uint8(num)
	}
	return color.RGBA{rgb[0], rgb[1], rgb[2], 0xFF}, nil
}

// extractSVGColors extracts named colors from the parse tree of the SVG 1.1
// spec HTML document "Chapter 4: Basic data types and interfaces".
func extractSVGColors(tree *html.Node) (map[string]color.RGBA, error) {
	ret := make(map[string]color.RGBA)

	// Find the tables which store the color keywords in the parse tree.
	colorTables := appendAll(nil, tree, func(n *html.Node) bool {
		return n.DataAtom == atom.Table && strings.Contains(getAttr(n, "", "summary"), "color keywords part")
	})

	for _, table := range colorTables {
		// Color names and values are stored in TextNodes within spans in each row.
		for _, tr := range appendAll(nil, table, matchAtom(atom.Tr)) {
			nameSpan := appendAll(nil, tr, matchAtomAttr(atom.Span, "", "class", "prop-value"))
			valueSpan := appendAll(nil, tr, matchAtomAttr(atom.Span, "", "class", "color-keyword-value"))

			// Since SVG 1.1 defines an odd number of colors, the last row
			// in the second table does not have contents. We skip it.
			if len(nameSpan) != 1 || len(valueSpan) != 1 {
				continue
			}
			n, v := nameSpan[0].FirstChild, valueSpan[0].FirstChild
			// This sanity checks for the existence of TextNodes under spans.
			if n == nil || n.Type != html.TextNode || v == nil || v.Type != html.TextNode {
				return nil, fmt.Errorf("extractSVGColors: couldn't find name/value text nodes")
			}
			val, err := parseRGB(v.Data)
			if err != nil {
				return nil, fmt.Errorf("extractSVGColors: couldn't parse name/value %q/%q: %s", n.Data, v.Data, err)
			}
			ret[n.Data] = val
		}
	}
	return ret, nil
}

const preamble = `// generated by go generate; DO NOT EDIT.

package colornames

import "image/color"

`

// WriteColorNames writes table.go.
func writeColorNames(w io.Writer, m map[string]color.RGBA) {
	keys := make([]string, 0, len(m))
	for k := range m {
		keys = append(keys, k)
	}
	sort.Strings(keys)

	fmt.Fprintln(w, preamble)
	fmt.Fprintln(w, "// Map contains named colors defined in the SVG 1.1 spec.")
	fmt.Fprintln(w, "var Map = map[string]color.RGBA{")
	for _, k := range keys {
		c := m[k]
		fmt.Fprintf(w, "%q:color.RGBA{%#02x, %#02x, %#02x, %#02x}, // rgb(%d, %d, %d)\n",
			k, c.R, c.G, c.B, c.A, c.R, c.G, c.B)
	}
	fmt.Fprintln(w, "}\n")
	fmt.Fprintln(w, "// Names contains the color names defined in the SVG 1.1 spec.")
	fmt.Fprintln(w, "var Names = []string{")
	for _, k := range keys {
		fmt.Fprintf(w, "%q,\n", k)
	}
	fmt.Fprintln(w, "}\n")
	fmt.Fprintln(w, "var (")
	for _, k := range keys {
		c := m[k]
		// Make the upper case version of k: "Darkred" instead of "darkred".
		k = string(k[0]-0x20) + k[1:]
		fmt.Fprintf(w, "%s=color.RGBA{%#02x, %#02x, %#02x, %#02x} // rgb(%d, %d, %d)\n",
			k, c.R, c.G, c.B, c.A, c.R, c.G, c.B)
	}
	fmt.Fprintln(w, ")")
}

const url = "http://www.w3.org/TR/SVG/types.html"

func main() {
	res, err := http.Get(url)
	if err != nil {
		log.Fatalf("Couldn't read from %s: %s\n", url, err)
	}
	defer res.Body.Close()

	tree, err := html.Parse(res.Body)
	if err != nil {
		log.Fatalf("Couldn't parse %s: %s\n", url, err)
	}

	colors, err := extractSVGColors(tree)
	if err != nil {
		log.Fatalf("Couldn't extract colors: %s\n", err)
	}

	buf := &bytes.Buffer{}
	writeColorNames(buf, colors)
	fmted, err := format.Source(buf.Bytes())
	if err != nil {
		log.Fatalf("Error while formatting code: %s\n", err)
	}

	if err := ioutil.WriteFile("table.go", fmted, 0644); err != nil {
		log.Fatalf("Error writing table.go: %s\n", err)
	}
}