1package main 2 3import "github.com/lucasb-eyer/go-colorful" 4import "image" 5import "image/draw" 6import "image/png" 7import "os" 8import "strconv" 9 10// This table contains the "keypoints" of the colorgradient you want to generate. 11// The position of each keypoint has to live in the range [0,1] 12type GradientTable []struct { 13 Col colorful.Color 14 Pos float64 15} 16 17// This is the meat of the gradient computation. It returns a HCL-blend between 18// the two colors around `t`. 19// Note: It relies heavily on the fact that the gradient keypoints are sorted. 20func (self GradientTable) GetInterpolatedColorFor(t float64) colorful.Color { 21 for i := 0; i < len(self)-1; i++ { 22 c1 := self[i] 23 c2 := self[i+1] 24 if c1.Pos <= t && t <= c2.Pos { 25 // We are in between c1 and c2. Go blend them! 26 t := (t - c1.Pos) / (c2.Pos - c1.Pos) 27 return c1.Col.BlendHcl(c2.Col, t).Clamped() 28 } 29 } 30 31 // Nothing found? Means we're at (or past) the last gradient keypoint. 32 return self[len(self)-1].Col 33} 34 35// This is a very nice thing Golang forces you to do! 36// It is necessary so that we can write out the literal of the colortable below. 37func MustParseHex(s string) colorful.Color { 38 c, err := colorful.Hex(s) 39 if err != nil { 40 panic("MustParseHex: " + err.Error()) 41 } 42 return c 43} 44 45func main() { 46 // The "keypoints" of the gradient. 47 keypoints := GradientTable{ 48 {MustParseHex("#9e0142"), 0.0}, 49 {MustParseHex("#d53e4f"), 0.1}, 50 {MustParseHex("#f46d43"), 0.2}, 51 {MustParseHex("#fdae61"), 0.3}, 52 {MustParseHex("#fee090"), 0.4}, 53 {MustParseHex("#ffffbf"), 0.5}, 54 {MustParseHex("#e6f598"), 0.6}, 55 {MustParseHex("#abdda4"), 0.7}, 56 {MustParseHex("#66c2a5"), 0.8}, 57 {MustParseHex("#3288bd"), 0.9}, 58 {MustParseHex("#5e4fa2"), 1.0}, 59 } 60 61 h := 1024 62 w := 40 63 64 if len(os.Args) == 3 { 65 // Meh, I'm being lazy... 66 w, _ = strconv.Atoi(os.Args[1]) 67 h, _ = strconv.Atoi(os.Args[2]) 68 } 69 70 img := image.NewRGBA(image.Rect(0, 0, w, h)) 71 72 for y := h - 1; y >= 0; y-- { 73 c := keypoints.GetInterpolatedColorFor(float64(y) / float64(h)) 74 draw.Draw(img, image.Rect(0, y, w, y+1), &image.Uniform{c}, image.ZP, draw.Src) 75 } 76 77 outpng, err := os.Create("gradientgen.png") 78 if err != nil { 79 panic("Error storing png: " + err.Error()) 80 } 81 defer outpng.Close() 82 83 png.Encode(outpng, img) 84} 85