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
|
// 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.
//go:build ignore
// +build ignore
// This program generates table.go from
// https://material.google.com/style/color.html
package main
import (
"bytes"
"fmt"
"go/format"
"image/color"
"io"
"log"
"net/http"
"strings"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
type matchFunc func(*html.Node) bool
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
}
func match(a atom.Atom, namespace, key, value string) matchFunc {
return func(n *html.Node) bool {
return n.DataAtom == a && strings.HasPrefix(getAttr(n, namespace, key), value)
}
}
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 ""
}
func getText(n *html.Node) string {
if n.FirstChild == nil || n.FirstChild.Type != html.TextNode {
return ""
}
return n.FirstChild.Data
}
var unhex = [256]byte{
'0': 0,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'a': 10,
'b': 11,
'c': 12,
'd': 13,
'e': 14,
'f': 15,
}
func parseRGB(s string) color.RGBA {
if len(s) != 7 || s[0] != '#' {
return color.RGBA{}
}
return color.RGBA{
R: unhex[s[1]]<<4 | unhex[s[2]],
G: unhex[s[3]]<<4 | unhex[s[4]],
B: unhex[s[5]]<<4 | unhex[s[6]],
A: 0xff,
}
}
type entry struct {
name string
rgba color.RGBA
}
func extractColors(tree *html.Node) (ret []entry) {
for _, table := range appendAll(nil, tree, match(atom.Section, "", "class", "color-group")) {
name := ""
for _, nameNode := range appendAll(nil, table, match(atom.Span, "", "class", "name")) {
name = strings.Replace(getText(nameNode), " ", "", -1)
break
}
shades := appendAll(nil, table, match(atom.Span, "", "class", "shade"))
hexes := appendAll(nil, table, match(atom.Span, "", "class", "hex"))
if len(shades) != len(hexes) || len(shades) == 0 {
continue
}
// Remove the duplicated 500 shade at the start of the list.
if getText(shades[0]) == "500" {
shades, hexes = shades[1:], hexes[1:]
}
for i, shade := range shades {
ret = append(ret, entry{
name + getText(shade),
parseRGB(getText(hexes[i])),
})
}
}
return ret
}
const preamble = `// generated by go generate; DO NOT EDIT.
package colornames
import "image/color"
`
func writeColorNames(w io.Writer, entries []entry) {
fmt.Fprintln(w, preamble)
fmt.Fprintln(w, "// Map contains named colors defined in the Material Design style guide.")
fmt.Fprintln(w, "var Map = map[string]color.RGBA{")
for _, e := range entries {
c := e.rgba
fmt.Fprintf(w, "%q:color.RGBA{%#02x, %#02x, %#02x, %#02x}, // rgb(%d, %d, %d)\n",
e.name, 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 Material Design style guide.")
fmt.Fprintln(w, "var Names = []string{")
for _, e := range entries {
fmt.Fprintf(w, "%q,\n", e.name)
}
fmt.Fprintln(w, "}\n")
fmt.Fprintln(w, "var (")
for _, e := range entries {
c := e.rgba
fmt.Fprintf(w, "%s=color.RGBA{%#02x, %#02x, %#02x, %#02x} // rgb(%d, %d, %d)\n",
e.name, c.R, c.G, c.B, c.A, c.R, c.G, c.B)
}
fmt.Fprintln(w, ")")
}
const url = "https://material.google.com/style/color.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)
}
buf := &bytes.Buffer{}
writeColorNames(buf, extractColors(tree))
fmted, err := format.Source(buf.Bytes())
if err != nil {
log.Fatalf("Error while formatting code: %s\n", err)
}
if err := os.WriteFile("table.go", fmted, 0644); err != nil {
log.Fatalf("Error writing table.go: %s\n", err)
}
}
|