Merge pull request 'mario-feature' (#1) from mario-feature into master

Reviewed-on: nathan/rgb-go-test#1
pull/2/head
nathan 2 years ago
commit 4165cdf830

@ -7,6 +7,7 @@ RUN go mod tidy
RUN go build .
RUN mkdir -p /usr/src/app/work/
RUN cp /usr/src/app/rgb/rgb /usr/src/app/work/rgb
RUN cp /usr/src/app/rgb/mario* /usr/src/app/work/
#FROM alpine:latest

@ -4,9 +4,13 @@ import (
"flag"
"image"
"image/color"
_ "image/jpeg"
"log"
"os"
"time"
rgbmatrix "gitea.wagshome.duckdns.org/publicWagsHome/go-rpi-rgb-led-matrix"
"github.com/disintegration/imaging"
"github.com/fogleman/gg"
)
@ -34,6 +38,19 @@ func main() {
config.InverseColors = *inverse_colors
config.DisableHardwarePulsing = *disable_hardware_pulsing
/*bounds := mario.Bounds()
var histogram [16][4]int
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
r, g, b, a := mario.At(x, y).RGBA()
// A color's RGBA method returns values in the range [0, 65535].
// Shifting by 12 reduces this to the range [0, 15].
histogram[r>>12][0]++
histogram[g>>12][1]++
histogram[b>>12][2]++
histogram[a>>12][3]++
}
}*/
m, err := rgbmatrix.NewRGBLedMatrix(config)
fatal(err)
@ -57,25 +74,37 @@ type Animation struct {
ctx *gg.Context
position image.Point
dir image.Point
height int
width int
stroke int
image image.Image
}
func NewAnimation(sz image.Point) *Animation {
reader, err := os.Open("mario.jpg")
if err != nil {
log.Fatal(err)
}
rawMario, _, err := image.Decode(reader)
mario := imaging.FlipH(imaging.Resize(rawMario, 16, 16, imaging.Lanczos))
return &Animation{
ctx: gg.NewContext(sz.X, sz.Y),
dir: image.Point{1, 1},
stroke: 5,
height: 8,
width: 8,
stroke: 16,
image: mario,
}
}
func (a *Animation) Next() (image.Image, <-chan time.Time, error) {
defer a.updatePosition()
a.ctx.SetColor(color.Black)
a.ctx.Clear()
a.ctx.DrawCircle(float64(a.position.X), float64(a.position.Y), float64(a.stroke))
a.ctx.SetColor(color.RGBA{0, 255, 0, 255})
a.ctx.DrawImageAnchored(a.image, a.position.X, a.position.Y, 0.5, 0.5)
//a.image = imaging.FlipH(a.image)
//a.ctx.DrawCircle(float64(a.position.X), float64(a.position.Y), float64(a.stroke))
//a.ctx.SetColor(color.RGBA{0, 255, 0, 255})
a.ctx.Fill()
return a.ctx.Image(), time.After(time.Millisecond * 50), nil
}
@ -84,15 +113,17 @@ func (a *Animation) updatePosition() {
a.position.X += 1 * a.dir.X
a.position.Y += 1 * a.dir.Y
if a.position.Y+a.stroke > a.ctx.Height() {
if a.position.Y+a.height > a.ctx.Height() {
a.dir.Y = -1
} else if a.position.Y-a.stroke < 0 {
} else if a.position.Y-a.height < 0 {
a.dir.Y = 1
}
if a.position.X+a.stroke > a.ctx.Width() {
if a.position.X+a.width > a.ctx.Width() {
a.dir.X = -1
} else if a.position.X-a.stroke < 0 {
a.image = imaging.FlipH(a.image)
} else if a.position.X-a.width < 0 {
a.dir.X = 1
a.image = imaging.FlipH(a.image)
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 334 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 368 B

Loading…
Cancel
Save