summaryrefslogtreecommitdiff
path: root/drw/drw.go
blob: 8ec05bc18cbda404eb9f519d3ac7a5c37279bfe4 (plain)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package drw // import "hhvn.uk/hbspbar/drw"

import (
	"image"
	"image/draw"
	"image/color"

	"hhvn.uk/hbspbar/config"

	"github.com/jezek/xgbutil/xgraphics"
	col "github.com/lucasb-eyer/go-colorful"
	"github.com/BurntSushi/freetype-go/freetype"
)

func DrawText(i *image.RGBA, x int, col color.Color, text string) (int, error) {
	ft := freetype.NewContext()
	ft.SetDPI(72)
	ft.SetFont(font)
	ft.SetFontSize(config.FontSize)
	ft.SetClip(i.Bounds())
	ft.SetDst(i)

	src := image.NewUniform(col)
	ft.SetSrc(src)

	pt := freetype.Pt(x, config.FontYPad+
		int(ft.PointToFix32(config.FontSize)>>8))
	npt, err := ft.DrawString(text, pt)

	return int(npt.X / 256) - x, err
}

func TextWidth(text string) (int) {
	w, _ := xgraphics.Extents(font, config.FontSize, text)
	return w
}

func DrawRect(i *image.RGBA, x, y, w, h int, c color.Color, fill bool) {
	var ix, iy int

	for ix = x; ix < x + w; ix++ {
		for iy = y; iy < y + h; iy++ {
			if fill || ix == x || ix == x + w - 1 ||
					iy == y || iy == y + h - 1 {
				i.Set(ix, iy, c)
			}
		}
	}
}

func AddImg(dst *image.RGBA, x, w int, src *image.RGBA) {
	r := image.Rect(x, 0, x + w, int(config.H))
	draw.Draw(dst, r, src, image.Pt(0,0), draw.Src)
}

func Blend(percent int, cols ...color.Color) color.Color {
	step := 100 / (len(cols) - 1)

	if percent < 0 { percent = 0 }
	if percent > 100 { percent = 100 }

	for i, cstep := 1, step; i < len(cols); i, cstep = i + 1, cstep + step {
		if percent >= cstep - step && percent <= cstep {
			a, _ := col.MakeColor(cols[i - 1])
			b, _ := col.MakeColor(cols[i])

			return a.BlendHcl(b, float64(percent) / 100).Clamped()
		}
	}

	return cols[len(cols) - 1]
}