summaryrefslogtreecommitdiff
path: root/drw/drw.go
blob: e5a16c290869c21d34320930e88cac2a90f95cd5 (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"

	"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 uint32, 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(int2rgb(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 uint32, fill bool) {
	col := int2rgb(c)

	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, col)
			}
		}
	}
}

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 Blend3(a, b, c uint32, percent int) uint32 {
	if percent <= 50 {
		return Blend(a, b, percent * 2)
	} else {
		return Blend(b, c, (percent - 50) * 2)
	}
}

// Conversion hell, I know
func Blend(a, b uint32, percent int) uint32 {
	ac, _ := col.MakeColor(int2rgb(a))
	bc, _ := col.MakeColor(int2rgb(b))


	return rgb2int(ac.BlendHcl(bc, float64(percent) / 100).Clamped())
}