File: gradient-text.go

package info (click to toggle)
golang-github-fogleman-gg 1.3.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 956 kB
  • sloc: makefile: 3
file content (41 lines) | stat: -rw-r--r-- 713 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main

import (
	"image/color"

	"github.com/fogleman/gg"
)

const (
	W = 1024
	H = 512
)

func main() {
	dc := gg.NewContext(W, H)

	// draw text
	dc.SetRGB(0, 0, 0)
	dc.LoadFontFace("/Library/Fonts/Impact.ttf", 128)
	dc.DrawStringAnchored("Gradient Text", W/2, H/2, 0.5, 0.5)

	// get the context as an alpha mask
	mask := dc.AsMask()

	// clear the context
	dc.SetRGB(1, 1, 1)
	dc.Clear()

	// set a gradient
	g := gg.NewLinearGradient(0, 0, W, H)
	g.AddColorStop(0, color.RGBA{255, 0, 0, 255})
	g.AddColorStop(1, color.RGBA{0, 0, 255, 255})
	dc.SetFillStyle(g)

	// using the mask, fill the context with the gradient
	dc.SetMask(mask)
	dc.DrawRectangle(0, 0, W, H)
	dc.Fill()

	dc.SavePNG("out.png")
}