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