display-test/drawer/bar.go

70 lines
1.7 KiB
Go
Raw Normal View History

package drawer
import "math"
2024-09-22 18:53:22 +00:00
func (d *drawer) FillBar(sx, sy, ex, ey int, color byte) {
2024-09-18 19:32:47 +00:00
d.dev.LockImg()
defer d.dev.UnlockImg()
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++ {
2024-09-22 18:53:22 +00:00
d.img.Pix[addr] = color
}
}
}
2024-09-22 18:53:22 +00:00
func (d *drawer) PutBar(sx, sy, ex, ey int, color byte) {
2024-09-18 19:32:47 +00:00
d.dev.LockImg()
defer d.dev.UnlockImg()
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++ {
2024-09-22 18:53:22 +00:00
d.img.Pix[addr] = color
}
}
// 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++ {
2024-09-22 18:53:22 +00:00
d.img.Pix[addr] = color
}
}
// 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() {
2024-09-22 18:53:22 +00:00
d.img.Pix[addr] = color
}
}
// 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() {
2024-09-22 18:53:22 +00:00
d.img.Pix[addr] = color
}
}
}