display-test/drawer/bar.go
2024-08-21 19:07:29 +03:00

70 lines
1.6 KiB
Go

package drawer
import "math"
func (d *drawer) FillBar(sx, sy, ex, ey, color int) {
d.Lock()
defer d.Unlock()
bounds := d.img.Bounds()
// Crop
sx = int(math.Max(float64(sx), 0))
sy = int(math.Max(float64(sy), 0))
ex = int(math.Min(float64(ex), float64(bounds.Dx())))
ey = int(math.Min(float64(ey), float64(bounds.Dy())))
w := ex - sx
// Fill
for lineaddr := sy*bounds.Dx() + sx; lineaddr < (ey-1)*bounds.Dx()+ex; lineaddr += bounds.Dx() {
addr := lineaddr
le := addr + w
for ; addr < le; addr++ {
d.img.Pix[addr] = 255
}
}
}
func (d *drawer) PutBar(sx, sy, ex, ey, color int) {
d.Lock()
defer d.Unlock()
bounds := d.img.Bounds()
// Crop
// Top
if sy >= 0 && sy < bounds.Dy() {
x0 := int(math.Max(float64(sx), 0))
x1 := int(math.Min(float64(ex), float64(bounds.Dx())))
for addr := sy*bounds.Dx() + x0; addr < sy*bounds.Dx()+x1; addr++ {
d.img.Pix[addr] = 255
}
}
// Bottom
if ey >= 0 && ey <= bounds.Dy() {
x0 := int(math.Max(float64(sx), 0))
x1 := int(math.Min(float64(ex), float64(bounds.Dx())))
for addr := (ey-1)*bounds.Dx() + x0; addr < (ey-1)*bounds.Dx()+x1; addr++ {
d.img.Pix[addr] = 255
}
}
// Left
if sx >= 0 && sx < bounds.Dx() {
y0 := int(math.Max(float64(sy), 0))
y1 := int(math.Min(float64(ey), float64(bounds.Dy())))
for addr := y0*bounds.Dx() + sx; addr < y1*bounds.Dx()+sx; addr += bounds.Dx() {
d.img.Pix[addr] = 255
}
}
// Right
if ex >= 0 && ex <= bounds.Dx() {
y0 := int(math.Max(float64(sy), 0))
y1 := int(math.Min(float64(ey), float64(bounds.Dy())))
for addr := y0*bounds.Dx() + (ex - 1); addr < y1*bounds.Dx()+(ex-1); addr += bounds.Dx() {
d.img.Pix[addr] = 255
}
}
}