| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
package main |
| 10 |
|
| 11 |
import ( |
| 12 |
"bytes" |
| 13 |
"fmt" |
| 14 |
"go/format" |
| 15 |
"image/color" |
| 16 |
"io" |
| 17 |
"log" |
| 18 |
"net/http" |
| 19 |
"strings" |
| 20 |
|
| 21 |
"golang.org/x/net/html" |
| 22 |
"golang.org/x/net/html/atom" |
| 23 |
) |
| 24 |
|
| 25 |
type matchFunc func(*html.Node) bool |
| 26 |
|
| 27 |
func appendAll(dst []*html.Node, n *html.Node, mf matchFunc) []*html.Node { |
| 28 |
if mf(n) { |
| 29 |
dst = append(dst, n) |
| 30 |
} |
| 31 |
for c := n.FirstChild; c != nil; c = c.NextSibling { |
| 32 |
dst = appendAll(dst, c, mf) |
| 33 |
} |
| 34 |
return dst |
| 35 |
} |
| 36 |
|
| 37 |
func match(a atom.Atom, namespace, key, value string) matchFunc { |
| 38 |
return func(n *html.Node) bool { |
| 39 |
return n.DataAtom == a && strings.HasPrefix(getAttr(n, namespace, key), value) |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
func getAttr(n *html.Node, namespace, key string) string { |
| 44 |
for _, attr := range n.Attr { |
| 45 |
if attr.Namespace == namespace && attr.Key == key { |
| 46 |
return attr.Val |
| 47 |
} |
| 48 |
} |
| 49 |
return "" |
| 50 |
} |
| 51 |
|
| 52 |
func getText(n *html.Node) string { |
| 53 |
if n.FirstChild == nil || n.FirstChild.Type != html.TextNode { |
| 54 |
return "" |
| 55 |
} |
| 56 |
return n.FirstChild.Data |
| 57 |
} |
| 58 |
|
| 59 |
var unhex = [256]byte{ |
| 60 |
'0': 0, |
| 61 |
'1': 1, |
| 62 |
'2': 2, |
| 63 |
'3': 3, |
| 64 |
'4': 4, |
| 65 |
'5': 5, |
| 66 |
'6': 6, |
| 67 |
'7': 7, |
| 68 |
'8': 8, |
| 69 |
'9': 9, |
| 70 |
'a': 10, |
| 71 |
'b': 11, |
| 72 |
'c': 12, |
| 73 |
'd': 13, |
| 74 |
'e': 14, |
| 75 |
'f': 15, |
| 76 |
} |
| 77 |
|
| 78 |
func parseRGB(s string) color.RGBA { |
| 79 |
if len(s) != 7 || s[0] != '#' { |
| 80 |
return color.RGBA{} |
| 81 |
} |
| 82 |
return color.RGBA{ |
| 83 |
R: unhex[s[1]]<<4 | unhex[s[2]], |
| 84 |
G: unhex[s[3]]<<4 | unhex[s[4]], |
| 85 |
B: unhex[s[5]]<<4 | unhex[s[6]], |
| 86 |
A: 0xff, |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
type entry struct { |
| 91 |
name string |
| 92 |
rgba color.RGBA |
| 93 |
} |
| 94 |
|
| 95 |
func extractColors(tree *html.Node) (ret []entry) { |
| 96 |
for _, table := range appendAll(nil, tree, match(atom.Section, "", "class", "color-group")) { |
| 97 |
name := "" |
| 98 |
for _, nameNode := range appendAll(nil, table, match(atom.Span, "", "class", "name")) { |
| 99 |
name = strings.Replace(getText(nameNode), " ", "", -1) |
| 100 |
break |
| 101 |
} |
| 102 |
shades := appendAll(nil, table, match(atom.Span, "", "class", "shade")) |
| 103 |
hexes := appendAll(nil, table, match(atom.Span, "", "class", "hex")) |
| 104 |
if len(shades) != len(hexes) || len(shades) == 0 { |
| 105 |
continue |
| 106 |
} |
| 107 |
|
| 108 |
if getText(shades[0]) == "500" { |
| 109 |
shades, hexes = shades[1:], hexes[1:] |
| 110 |
} |
| 111 |
for i, shade := range shades { |
| 112 |
ret = append(ret, entry{ |
| 113 |
name + getText(shade), |
| 114 |
parseRGB(getText(hexes[i])), |
| 115 |
}) |
| 116 |
} |
| 117 |
} |
| 118 |
return ret |
| 119 |
} |
| 120 |
|
| 121 |
const preamble = `// generated by go generate; DO NOT EDIT. |
| 122 |
|
| 123 |
package colornames |
| 124 |
|
| 125 |
import "image/color" |
| 126 |
|
| 127 |
` |
| 128 |
|
| 129 |
func writeColorNames(w io.Writer, entries []entry) { |
| 130 |
fmt.Fprintln(w, preamble) |
| 131 |
fmt.Fprintln(w, "// Map contains named colors defined in the Material Design style guide.") |
| 132 |
fmt.Fprintln(w, "var Map = map[string]color.RGBA{") |
| 133 |
for _, e := range entries { |
| 134 |
c := e.rgba |
| 135 |
fmt.Fprintf(w, "%q:color.RGBA{%#02x, %#02x, %#02x, %#02x}, // rgb(%d, %d, %d)\n", |
| 136 |
e.name, c.R, c.G, c.B, c.A, c.R, c.G, c.B) |
| 137 |
} |
| 138 |
fmt.Fprintln(w, "}\n") |
| 139 |
fmt.Fprintln(w, "// Names contains the color names defined in the Material Design style guide.") |
| 140 |
fmt.Fprintln(w, "var Names = []string{") |
| 141 |
for _, e := range entries { |
| 142 |
fmt.Fprintf(w, "%q,\n", e.name) |
| 143 |
} |
| 144 |
fmt.Fprintln(w, "}\n") |
| 145 |
fmt.Fprintln(w, "var (") |
| 146 |
for _, e := range entries { |
| 147 |
c := e.rgba |
| 148 |
fmt.Fprintf(w, "%s=color.RGBA{%#02x, %#02x, %#02x, %#02x} // rgb(%d, %d, %d)\n", |
| 149 |
e.name, c.R, c.G, c.B, c.A, c.R, c.G, c.B) |
| 150 |
} |
| 151 |
fmt.Fprintln(w, ")") |
| 152 |
} |
| 153 |
|
| 154 |
const url = "https://material.google.com/style/color.html" |
| 155 |
|
| 156 |
func main() { |
| 157 |
res, err := http.Get(url) |
| 158 |
if err != nil { |
| 159 |
log.Fatalf("Couldn't read from %s: %s\n", url, err) |
| 160 |
} |
| 161 |
defer res.Body.Close() |
| 162 |
|
| 163 |
tree, err := html.Parse(res.Body) |
| 164 |
if err != nil { |
| 165 |
log.Fatalf("Couldn't parse %s: %s\n", url, err) |
| 166 |
} |
| 167 |
|
| 168 |
buf := &bytes.Buffer{} |
| 169 |
writeColorNames(buf, extractColors(tree)) |
| 170 |
fmted, err := format.Source(buf.Bytes()) |
| 171 |
if err != nil { |
| 172 |
log.Fatalf("Error while formatting code: %s\n", err) |
| 173 |
} |
| 174 |
|
| 175 |
if err := os.WriteFile("table.go", fmted, 0644); err != nil { |
| 176 |
log.Fatalf("Error writing table.go: %s\n", err) |
| 177 |
} |
| 178 |
} |