From: ChaiShushan Date: Thu, 28 Aug 2014 21:49:32 +0000 (-0700) Subject: image/color/palette: use go generate to create palette.go X-Git-Tag: go1.4beta1~648 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=882933f09b35bf4f1791203a6666af99ff9bea54;p=gostls13.git image/color/palette: use go generate to create palette.go LGTM=r R=golang-codereviews, r CC=golang-codereviews https://golang.org/cl/134940043 --- diff --git a/src/pkg/image/color/palette/gen.go b/src/pkg/image/color/palette/gen.go index 4f4d88345a..2b5fdaaf2b 100644 --- a/src/pkg/image/color/palette/gen.go +++ b/src/pkg/image/color/palette/gen.go @@ -7,29 +7,49 @@ package main // This program generates palette.go. Invoke it as -// go run gen.go | gofmt > palette.go +// go run gen.go -output palette.go import ( + "bytes" + "flag" "fmt" + "go/format" + "io" + "io/ioutil" + "log" ) +var filename = flag.String("output", "palette.go", "output file name") + func main() { - fmt.Println(`// Copyright 2013 The Go Authors. All rights reserved. + flag.Parse() + + var buf bytes.Buffer + + fmt.Fprintln(&buf, `// Copyright 2013 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.`) - fmt.Println() - fmt.Println("// generated by go run gen.go; DO NOT EDIT") - fmt.Println() - fmt.Println("// Package palette provides standard color palettes.") - fmt.Println("package palette") - fmt.Println() - fmt.Println(`import "image/color"`) - fmt.Println() - printPlan9() - printWebSafe() + fmt.Fprintln(&buf) + fmt.Fprintln(&buf, "// generated by go run gen.go -output palette.go; DO NOT EDIT") + fmt.Fprintln(&buf) + fmt.Fprintln(&buf, "package palette") + fmt.Fprintln(&buf) + fmt.Fprintln(&buf, `import "image/color"`) + fmt.Fprintln(&buf) + printPlan9(&buf) + printWebSafe(&buf) + + data, err := format.Source(buf.Bytes()) + if err != nil { + log.Fatal(err) + } + err = ioutil.WriteFile(*filename, data, 0644) + if err != nil { + log.Fatal(err) + } } -func printPlan9() { +func printPlan9(w io.Writer) { c, lines := [3]int{}, [256]string{} for r, i := 0, 0; r != 4; r++ { for v := 0; v != 4; v, i = v+1, i+16 { @@ -58,27 +78,27 @@ func printPlan9() { } } } - fmt.Println("// Plan9 is a 256-color palette that partitions the 24-bit RGB space") - fmt.Println("// into 4×4×4 subdivision, with 4 shades in each subcube. Compared to the") - fmt.Println("// WebSafe, the idea is to reduce the color resolution by dicing the") - fmt.Println("// color cube into fewer cells, and to use the extra space to increase the") - fmt.Println("// intensity resolution. This results in 16 gray shades (4 gray subcubes with") - fmt.Println("// 4 samples in each), 13 shades of each primary and secondary color (3") - fmt.Println("// subcubes with 4 samples plus black) and a reasonable selection of colors") - fmt.Println("// covering the rest of the color cube. The advantage is better representation") - fmt.Println("// of continuous tones.") - fmt.Println("//") - fmt.Println("// This palette was used in the Plan 9 Operating System, described at") - fmt.Println("// http://plan9.bell-labs.com/magic/man2html/6/color") - fmt.Println("var Plan9 = []color.Color{") + fmt.Fprintln(w, "// Plan9 is a 256-color palette that partitions the 24-bit RGB space") + fmt.Fprintln(w, "// into 4×4×4 subdivision, with 4 shades in each subcube. Compared to the") + fmt.Fprintln(w, "// WebSafe, the idea is to reduce the color resolution by dicing the") + fmt.Fprintln(w, "// color cube into fewer cells, and to use the extra space to increase the") + fmt.Fprintln(w, "// intensity resolution. This results in 16 gray shades (4 gray subcubes with") + fmt.Fprintln(w, "// 4 samples in each), 13 shades of each primary and secondary color (3") + fmt.Fprintln(w, "// subcubes with 4 samples plus black) and a reasonable selection of colors") + fmt.Fprintln(w, "// covering the rest of the color cube. The advantage is better representation") + fmt.Fprintln(w, "// of continuous tones.") + fmt.Fprintln(w, "//") + fmt.Fprintln(w, "// This palette was used in the Plan 9 Operating System, described at") + fmt.Fprintln(w, "// http://plan9.bell-labs.com/magic/man2html/6/color") + fmt.Fprintln(w, "var Plan9 = []color.Color{") for _, line := range lines { - fmt.Println(line) + fmt.Fprintln(w, line) } - fmt.Println("}") - fmt.Println() + fmt.Fprintln(w, "}") + fmt.Fprintln(w) } -func printWebSafe() { +func printWebSafe(w io.Writer) { lines := [6 * 6 * 6]string{} for r := 0; r < 6; r++ { for g := 0; g < 6; g++ { @@ -88,14 +108,14 @@ func printWebSafe() { } } } - fmt.Println("// WebSafe is a 216-color palette that was popularized by early versions") - fmt.Println("// of Netscape Navigator. It is also known as the Netscape Color Cube.") - fmt.Println("//") - fmt.Println("// See http://en.wikipedia.org/wiki/Web_colors#Web-safe_colors for details.") - fmt.Println("var WebSafe = []color.Color{") + fmt.Fprintln(w, "// WebSafe is a 216-color palette that was popularized by early versions") + fmt.Fprintln(w, "// of Netscape Navigator. It is also known as the Netscape Color Cube.") + fmt.Fprintln(w, "//") + fmt.Fprintln(w, "// See http://en.wikipedia.org/wiki/Web_colors#Web-safe_colors for details.") + fmt.Fprintln(w, "var WebSafe = []color.Color{") for _, line := range lines { - fmt.Println(line) + fmt.Fprintln(w, line) } - fmt.Println("}") - fmt.Println() + fmt.Fprintln(w, "}") + fmt.Fprintln(w) } diff --git a/src/pkg/image/color/palette/generate.go b/src/pkg/image/color/palette/generate.go new file mode 100644 index 0000000000..64c2ec0d9a --- /dev/null +++ b/src/pkg/image/color/palette/generate.go @@ -0,0 +1,8 @@ +// Copyright 2014 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:generate go run gen.go -output palette.go + +// Package palette provides standard color palettes. +package palette diff --git a/src/pkg/image/color/palette/palette.go b/src/pkg/image/color/palette/palette.go index f761e5368d..0bf2c8e1aa 100644 --- a/src/pkg/image/color/palette/palette.go +++ b/src/pkg/image/color/palette/palette.go @@ -2,9 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// generated by go run gen.go; DO NOT EDIT +// generated by go run gen.go -output palette.go; DO NOT EDIT -// Package palette provides standard color palettes. package palette import "image/color"