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] }