1
2
3
4
5
6
7
8 package main
9
10
11
12
13 import (
14 "bytes"
15 "flag"
16 "fmt"
17 "go/format"
18 "io"
19 "log"
20 "os"
21 )
22
23 var filename = flag.String("output", "palette.go", "output file name")
24
25 func main() {
26 flag.Parse()
27
28 var buf bytes.Buffer
29
30 fmt.Fprintln(&buf, `// Copyright 2013 The Go Authors. All rights reserved.
31 // Use of this source code is governed by a BSD-style
32 // license that can be found in the LICENSE file.`)
33 fmt.Fprintln(&buf)
34 fmt.Fprintln(&buf, "// Code generated by go run gen.go -output palette.go; DO NOT EDIT.")
35 fmt.Fprintln(&buf)
36 fmt.Fprintln(&buf, "package palette")
37 fmt.Fprintln(&buf)
38 fmt.Fprintln(&buf, `import "image/color"`)
39 fmt.Fprintln(&buf)
40 printPlan9(&buf)
41 printWebSafe(&buf)
42
43 data, err := format.Source(buf.Bytes())
44 if err != nil {
45 log.Fatal(err)
46 }
47 err = os.WriteFile(*filename, data, 0644)
48 if err != nil {
49 log.Fatal(err)
50 }
51 }
52
53 func printPlan9(w io.Writer) {
54 c, lines := [3]int{}, [256]string{}
55 for r, i := 0, 0; r != 4; r++ {
56 for v := 0; v != 4; v, i = v+1, i+16 {
57 for g, j := 0, v-r; g != 4; g++ {
58 for b := 0; b != 4; b, j = b+1, j+1 {
59 den := r
60 if g > den {
61 den = g
62 }
63 if b > den {
64 den = b
65 }
66 if den == 0 {
67 c[0] = 0x11 * v
68 c[1] = 0x11 * v
69 c[2] = 0x11 * v
70 } else {
71 num := 17 * (4*den + v)
72 c[0] = r * num / den
73 c[1] = g * num / den
74 c[2] = b * num / den
75 }
76 lines[i+(j&0x0f)] =
77 fmt.Sprintf("\tcolor.RGBA{0x%02x, 0x%02x, 0x%02x, 0xff},", c[0], c[1], c[2])
78 }
79 }
80 }
81 }
82 fmt.Fprintln(w, "// Plan9 is a 256-color palette that partitions the 24-bit RGB space")
83 fmt.Fprintln(w, "// into 4×4×4 subdivision, with 4 shades in each subcube. Compared to the")
84 fmt.Fprintln(w, "// WebSafe, the idea is to reduce the color resolution by dicing the")
85 fmt.Fprintln(w, "// color cube into fewer cells, and to use the extra space to increase the")
86 fmt.Fprintln(w, "// intensity resolution. This results in 16 gray shades (4 gray subcubes with")
87 fmt.Fprintln(w, "// 4 samples in each), 13 shades of each primary and secondary color (3")
88 fmt.Fprintln(w, "// subcubes with 4 samples plus black) and a reasonable selection of colors")
89 fmt.Fprintln(w, "// covering the rest of the color cube. The advantage is better representation")
90 fmt.Fprintln(w, "// of continuous tones.")
91 fmt.Fprintln(w, "//")
92 fmt.Fprintln(w, "// This palette was used in the Plan 9 Operating System, described at")
93 fmt.Fprintln(w, "// https://9p.io/magic/man2html/6/color")
94 fmt.Fprintln(w, "var Plan9 = []color.Color{")
95 for _, line := range lines {
96 fmt.Fprintln(w, line)
97 }
98 fmt.Fprintln(w, "}")
99 fmt.Fprintln(w)
100 }
101
102 func printWebSafe(w io.Writer) {
103 lines := [6 * 6 * 6]string{}
104 for r := 0; r < 6; r++ {
105 for g := 0; g < 6; g++ {
106 for b := 0; b < 6; b++ {
107 lines[36*r+6*g+b] =
108 fmt.Sprintf("\tcolor.RGBA{0x%02x, 0x%02x, 0x%02x, 0xff},", 0x33*r, 0x33*g, 0x33*b)
109 }
110 }
111 }
112 fmt.Fprintln(w, "// WebSafe is a 216-color palette that was popularized by early versions")
113 fmt.Fprintln(w, "// of Netscape Navigator. It is also known as the Netscape Color Cube.")
114 fmt.Fprintln(w, "//")
115 fmt.Fprintln(w, "// See https://en.wikipedia.org/wiki/Web_colors#Web-safe_colors for details.")
116 fmt.Fprintln(w, "var WebSafe = []color.Color{")
117 for _, line := range lines {
118 fmt.Fprintln(w, line)
119 }
120 fmt.Fprintln(w, "}")
121 fmt.Fprintln(w)
122 }
123
View as plain text