github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/image/color/palette/gen.go (about)

     1  // Copyright 2013 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build ignore
     6  
     7  package main
     8  
     9  // This program generates palette.go. Invoke it as
    10  //	go run gen.go | gofmt > palette.go
    11  
    12  import (
    13  	"fmt"
    14  )
    15  
    16  func main() {
    17  	fmt.Println("// generated by go run gen.go; DO NOT EDIT")
    18  	fmt.Println()
    19  	fmt.Println("// Package palette provides standard color palettes.")
    20  	fmt.Println("package palette")
    21  	fmt.Println()
    22  	fmt.Println(`import "image/color"`)
    23  	fmt.Println()
    24  	printPlan9()
    25  	printWebSafe()
    26  }
    27  
    28  func printPlan9() {
    29  	c, lines := [3]int{}, [256]string{}
    30  	for r, i := 0, 0; r != 4; r++ {
    31  		for v := 0; v != 4; v, i = v+1, i+16 {
    32  			for g, j := 0, v-r; g != 4; g++ {
    33  				for b := 0; b != 4; b, j = b+1, j+1 {
    34  					den := r
    35  					if g > den {
    36  						den = g
    37  					}
    38  					if b > den {
    39  						den = b
    40  					}
    41  					if den == 0 {
    42  						c[0] = 0x11 * v
    43  						c[1] = 0x11 * v
    44  						c[2] = 0x11 * v
    45  					} else {
    46  						num := 17 * (4*den + v)
    47  						c[0] = r * num / den
    48  						c[1] = g * num / den
    49  						c[2] = b * num / den
    50  					}
    51  					lines[i+(j&0x0f)] =
    52  						fmt.Sprintf("\tcolor.RGBA{0x%02x, 0x%02x, 0x%02x, 0xff},", c[0], c[1], c[2])
    53  				}
    54  			}
    55  		}
    56  	}
    57  	fmt.Println("// Plan9 is a 256-color palette that partitions the 24-bit RGB space")
    58  	fmt.Println("// into 4×4×4 subdivision, with 4 shades in each subcube. Compared to the")
    59  	fmt.Println("// WebSafe, the idea is to reduce the color resolution by dicing the")
    60  	fmt.Println("// color cube into fewer cells, and to use the extra space to increase the")
    61  	fmt.Println("// intensity resolution. This results in 16 gray shades (4 gray subcubes with")
    62  	fmt.Println("// 4 samples in each), 13 shades of each primary and secondary color (3")
    63  	fmt.Println("// subcubes with 4 samples plus black) and a reasonable selection of colors")
    64  	fmt.Println("// covering the rest of the color cube. The advantage is better representation")
    65  	fmt.Println("// of continuous tones.")
    66  	fmt.Println("//")
    67  	fmt.Println("// This palette was used in the Plan 9 Operating System, described at")
    68  	fmt.Println("// http://plan9.bell-labs.com/magic/man2html/6/color")
    69  	fmt.Println("var Plan9 = []color.Color{")
    70  	for _, line := range lines {
    71  		fmt.Println(line)
    72  	}
    73  	fmt.Println("}")
    74  	fmt.Println()
    75  }
    76  
    77  func printWebSafe() {
    78  	lines := [6 * 6 * 6]string{}
    79  	for r := 0; r < 6; r++ {
    80  		for g := 0; g < 6; g++ {
    81  			for b := 0; b < 6; b++ {
    82  				lines[36*r+6*g+b] =
    83  					fmt.Sprintf("\tcolor.RGBA{0x%02x, 0x%02x, 0x%02x, 0xff},", 0x33*r, 0x33*g, 0x33*b)
    84  			}
    85  		}
    86  	}
    87  	fmt.Println("// WebSafe is a 216-color palette that was popularized by early versions")
    88  	fmt.Println("// of Netscape Navigator. It is also known as the Netscape Color Cube.")
    89  	fmt.Println("//")
    90  	fmt.Println("// See http://en.wikipedia.org/wiki/Web_colors#Web-safe_colors for details.")
    91  	fmt.Println("var WebSafe = []color.Color{")
    92  	for _, line := range lines {
    93  		fmt.Println(line)
    94  	}
    95  	fmt.Println("}")
    96  	fmt.Println()
    97  }