...
1 package main
2
3 import (
4 "image"
5 "image/draw"
6 "image/png"
7 "os"
8 "strconv"
9
10 "github.com/lucasb-eyer/go-colorful"
11 )
12
13
14
15 type GradientTable []struct {
16 Col colorful.Color
17 Pos float64
18 }
19
20
21
22
23 func (gt GradientTable) GetInterpolatedColorFor(t float64) colorful.Color {
24 for i := 0; i < len(gt)-1; i++ {
25 c1 := gt[i]
26 c2 := gt[i+1]
27 if c1.Pos <= t && t <= c2.Pos {
28
29 t := (t - c1.Pos) / (c2.Pos - c1.Pos)
30 return c1.Col.BlendHcl(c2.Col, t).Clamped()
31 }
32 }
33
34
35 return gt[len(gt)-1].Col
36 }
37
38
39
40 func MustParseHex(s string) colorful.Color {
41 c, err := colorful.Hex(s)
42 if err != nil {
43 panic("MustParseHex: " + err.Error())
44 }
45 return c
46 }
47
48 func main() {
49
50 keypoints := GradientTable{
51 {MustParseHex("#9e0142"), 0.0},
52 {MustParseHex("#d53e4f"), 0.1},
53 {MustParseHex("#f46d43"), 0.2},
54 {MustParseHex("#fdae61"), 0.3},
55 {MustParseHex("#fee090"), 0.4},
56 {MustParseHex("#ffffbf"), 0.5},
57 {MustParseHex("#e6f598"), 0.6},
58 {MustParseHex("#abdda4"), 0.7},
59 {MustParseHex("#66c2a5"), 0.8},
60 {MustParseHex("#3288bd"), 0.9},
61 {MustParseHex("#5e4fa2"), 1.0},
62 }
63
64 h := 1024
65 w := 40
66
67 if len(os.Args) == 3 {
68
69 w, _ = strconv.Atoi(os.Args[1])
70 h, _ = strconv.Atoi(os.Args[2])
71 }
72
73 img := image.NewRGBA(image.Rect(0, 0, w, h))
74
75 for y := h - 1; y >= 0; y-- {
76 c := keypoints.GetInterpolatedColorFor(float64(y) / float64(h))
77 draw.Draw(img, image.Rect(0, y, w, y+1), &image.Uniform{c}, image.Point{}, draw.Src)
78 }
79
80 outpng, err := os.Create("gradientgen.png")
81 if err != nil {
82 panic("Error storing png: " + err.Error())
83 }
84 defer outpng.Close()
85
86 png.Encode(outpng, img)
87 }
88
View as plain text