drw.go (1652B)
1 package drw // import "hhvn.uk/hbspbar/drw" 2 3 import ( 4 "image" 5 "image/draw" 6 "image/color" 7 8 "hhvn.uk/hbspbar/config" 9 10 "github.com/jezek/xgbutil/xgraphics" 11 col "github.com/lucasb-eyer/go-colorful" 12 "github.com/BurntSushi/freetype-go/freetype" 13 ) 14 15 func DrawText(i *image.RGBA, x int, col color.Color, text string) (int, error) { 16 ft := freetype.NewContext() 17 ft.SetDPI(72) 18 ft.SetFont(font) 19 ft.SetFontSize(config.FontSize) 20 ft.SetClip(i.Bounds()) 21 ft.SetDst(i) 22 23 src := image.NewUniform(col) 24 ft.SetSrc(src) 25 26 pt := freetype.Pt(x, config.FontYPad+ 27 int(ft.PointToFix32(config.FontSize)>>8)) 28 npt, err := ft.DrawString(text, pt) 29 30 return int(npt.X / 256) - x, err 31 } 32 33 func TextWidth(text string) (int) { 34 w, _ := xgraphics.Extents(font, config.FontSize, text) 35 return w 36 } 37 38 func DrawRect(i *image.RGBA, x, y, w, h int, c color.Color, fill bool) { 39 var ix, iy int 40 41 for ix = x; ix < x + w; ix++ { 42 for iy = y; iy < y + h; iy++ { 43 if fill || ix == x || ix == x + w - 1 || 44 iy == y || iy == y + h - 1 { 45 i.Set(ix, iy, c) 46 } 47 } 48 } 49 } 50 51 func AddImg(dst *image.RGBA, x, w int, src *image.RGBA) { 52 r := image.Rect(x, 0, x + w, int(config.H)) 53 draw.Draw(dst, r, src, image.Pt(0,0), draw.Src) 54 } 55 56 func Blend(percent int, cols ...color.Color) color.Color { 57 step := 100 / (len(cols) - 1) 58 59 if percent < 0 { percent = 0 } 60 if percent > 100 { percent = 100 } 61 62 for i, cstep := 1, step; i < len(cols); i, cstep = i + 1, cstep + step { 63 if percent >= cstep - step && percent <= cstep { 64 a, _ := col.MakeColor(cols[i - 1]) 65 b, _ := col.MakeColor(cols[i]) 66 67 return a.BlendHcl(b, float64(percent) / 100).Clamped() 68 } 69 } 70 71 return cols[len(cols) - 1] 72 }