From e4acdac3a5051ad0658cca8b69b22fe4d150b257 Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Sat, 15 Feb 2020 13:58:01 +0100 Subject: [PATCH] add funmode :^) --- pixelflut/api.go | 19 ++++++++++++++++++- render/image.go | 14 ++++++++++++++ render/text.go | 7 +++++-- rpc/repl.go | 8 +++++++- 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/pixelflut/api.go b/pixelflut/api.go index c9266b4..dc71841 100644 --- a/pixelflut/api.go +++ b/pixelflut/api.go @@ -8,14 +8,31 @@ import ( "log" "net" "sync" + + "github.com/SpeckiJ/Hochwasser/render" ) +var funmode = true + // Flut asynchronously sends the given image to pixelflut server at `address` // using `conns` connections. Pixels are sent column wise, unless `shuffle` // is set. Stops when stop is closed. // @cleanup: use FlutTask{} as arg func Flut(img *image.NRGBA, position image.Point, shuffle bool, address string, conns int, stop chan bool, wg *sync.WaitGroup) { - cmds := commandsFromImage(img, position) + var cmds commands + if funmode { + // do a RGB split of white + imgmod := render.ImgReplaceColors(img, color.NRGBA{0xff, 0xff, 0xff, 0xff}, color.NRGBA{0xff, 0, 0, 0xff}) + cmds = append(cmds, commandsFromImage(imgmod, image.Pt(position.X-10, position.Y-10))...) + imgmod = render.ImgReplaceColors(img, color.NRGBA{0xff, 0xff, 0xff, 0xff}, color.NRGBA{0, 0xff, 0, 0xff}) + cmds = append(cmds, commandsFromImage(imgmod, image.Pt(position.X+10, position.Y))...) + imgmod = render.ImgReplaceColors(img, color.NRGBA{0xff, 0xff, 0xff, 0xff}, color.NRGBA{0, 0, 0xff, 0xff}) + cmds = append(cmds, commandsFromImage(imgmod, image.Pt(position.X-10, position.Y+10))...) + cmds = append(cmds, commandsFromImage(img, position)...) + } else { + cmds = commandsFromImage(img, position) + } + if shuffle { cmds.Shuffle() } diff --git a/render/image.go b/render/image.go index b7aad79..b9fe2ff 100644 --- a/render/image.go +++ b/render/image.go @@ -2,6 +2,7 @@ package render import ( "image" + "image/color" _ "image/gif" // register gif, jpeg, png format handlers _ "image/jpeg" "image/png" @@ -45,6 +46,19 @@ func ImgToNRGBA(img image.Image) *image.NRGBA { return r } +func ImgReplaceColors(img *image.NRGBA, from, to color.NRGBA) *image.NRGBA { + b := img.Bounds() + r := image.NewNRGBA(b) + for x := b.Min.X; x < b.Max.X; x++ { + for y := b.Min.Y; y < b.Max.Y; y++ { + if img.At(x, y) == from { + r.SetNRGBA(x, y, to) + } + } + } + return r +} + func ScaleImage(img image.Image, factor int) (scaled *image.NRGBA) { b := img.Bounds() scaledBounds := image.Rect(0, 0, b.Max.X*factor, b.Max.Y*factor) diff --git a/render/text.go b/render/text.go index e66ea00..91690c2 100644 --- a/render/text.go +++ b/render/text.go @@ -17,7 +17,7 @@ func pt(p fixed.Point26_6) image.Point { } } -func RenderText(text string, scale int, col color.Color) *image.NRGBA { +func RenderText(text string, scale int, col, bgCol color.Color) *image.NRGBA { // @incomplete: draw with texture via Drawer.Src face := basicfont.Face7x13 stringBounds, _ := font.BoundString(face, text) @@ -25,7 +25,10 @@ func RenderText(text string, scale int, col color.Color) *image.NRGBA { b := image.Rectangle{pt(stringBounds.Min), pt(stringBounds.Max)} img := image.NewNRGBA(b) - draw.Draw(img, b, image.Black, image.Point{}, draw.Src) // fill with black bg + // fill with black bg + if (bgCol != color.NRGBA{}) { + draw.Draw(img, b, image.NewUniform(bgCol), image.Point{}, draw.Src) + } d := font.Drawer{ Dst: img, diff --git a/rpc/repl.go b/rpc/repl.go index 0405f7c..73a49ba 100644 --- a/rpc/repl.go +++ b/rpc/repl.go @@ -29,6 +29,7 @@ func RunREPL(f Fluter) { mode := commandMode textSize := 4 textCol := color.NRGBA{0xff, 0xff, 0xff, 0xff} + bgCol := color.NRGBA{} scanner := bufio.NewScanner(os.Stdin) for scanner.Scan() { @@ -42,7 +43,7 @@ func RunREPL(f Fluter) { continue } t := f.getTask() - t.Img = render.RenderText(inputStr, textSize, textCol) + t.Img = render.RenderText(inputStr, textSize, textCol, bgCol) f.applyTask(t) case commandMode: @@ -94,6 +95,11 @@ func RunREPL(f Fluter) { textCol = color.NRGBA{col[0], col[1], col[2], 0xff} } } + if len(args) > 2 { + if col, err := hex.DecodeString(args[2]); err == nil { + bgCol = color.NRGBA{col[0], col[1], col[2], 0xff} + } + } case "img": if len(args) > 0 {