Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Commit 783ca25

Browse files
committed
use image.Image instead of a specific implementation
1 parent ecab109 commit 783ca25

File tree

6 files changed

+17
-8
lines changed

6 files changed

+17
-8
lines changed

drivers/js/js.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package js
44
import (
55
"context"
66
"image"
7+
"image/draw"
78
"log"
89
"time"
910
"unicode/utf8"
@@ -16,7 +17,7 @@ import (
1617
// TileManager manages tiles fetching.
1718
type TileManager interface {
1819
// GetImage returns the image to be used for a given cell style.
19-
GetImage(gruid.Cell) *image.RGBA
20+
GetImage(gruid.Cell) image.Image
2021

2122
// TileSize returns the (width, height) in pixels of the tiles. Both
2223
// should be positive and non-zero.
@@ -358,7 +359,15 @@ func (dr *Driver) draw(cell gruid.Cell, x, y int) {
358359
log.Printf("no tile for %+v", cell)
359360
return
360361
}
361-
buf := img.Pix
362+
var rgbaimg *image.RGBA
363+
switch img := img.(type) {
364+
case *image.RGBA:
365+
rgbaimg = img
366+
default:
367+
rect := img.Bounds()
368+
draw.Draw(rgbaimg, rect, img, rect.Min, draw.Src)
369+
}
370+
buf := rgbaimg.Pix
362371
ua := js.Global().Get("Uint8Array").New(js.ValueOf(len(buf)))
363372
js.CopyBytesToJS(ua, buf)
364373
ca := js.Global().Get("Uint8ClampedArray").New(ua)

drivers/sdl/sdl.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
// TileManager manages tiles fetching.
2121
type TileManager interface {
2222
// GetImage returns the image to be used for a given cell style.
23-
GetImage(gruid.Cell) *image.RGBA
23+
GetImage(gruid.Cell) image.Image
2424

2525
// TileSize returns the (width, height) in pixels of the tiles. Both
2626
// should be positive and non-zero.

examples/messages/tiles.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type TileDrawer struct {
4545
drawer *tiles.Drawer
4646
}
4747

48-
func (t *TileDrawer) GetImage(c gruid.Cell) *image.RGBA {
48+
func (t *TileDrawer) GetImage(c gruid.Cell) image.Image {
4949
// we use some selenized colors
5050
fg := image.NewUniform(color.RGBA{0xad, 0xbc, 0xbc, 255})
5151
switch c.Style.Fg {

examples/movement/tiles.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type TileDrawer struct {
4545
drawer *tiles.Drawer
4646
}
4747

48-
func (t *TileDrawer) GetImage(c gruid.Cell) *image.RGBA {
48+
func (t *TileDrawer) GetImage(c gruid.Cell) image.Image {
4949
// we use some selenized colors
5050
fg := image.NewUniform(color.RGBA{0xad, 0xbc, 0xbc, 255})
5151
bg := image.NewUniform(color.RGBA{0x18, 0x49, 0x56, 255})

examples/pager/tiles.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type TileDrawer struct {
4545
drawer *tiles.Drawer
4646
}
4747

48-
func (t *TileDrawer) GetImage(c gruid.Cell) *image.RGBA {
48+
func (t *TileDrawer) GetImage(c gruid.Cell) image.Image {
4949
// we use some selenized colors
5050
fg := image.NewUniform(color.RGBA{0xad, 0xbc, 0xbc, 255})
5151
switch c.Style.Fg {

tiles/drawer.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ func NewDrawer(face font.Face) (*Drawer, error) {
4141

4242
// Draw draws a rune and returns the produced image with foreground and
4343
// background colors given by images fg and bg.
44-
func (d *Drawer) Draw(r rune, fg, bg image.Image) *image.RGBA {
44+
func (d *Drawer) Draw(r rune, fg, bg image.Image) image.Image {
4545
d.drawer.Dot = d.dot
4646
d.drawer.Src = fg
4747
d.drawer.Dst = image.NewRGBA(d.rect)
4848
rect := d.drawer.Dst.Bounds()
4949
draw.Draw(d.drawer.Dst, rect, bg, rect.Min, draw.Src)
5050
d.drawer.DrawString(string(r))
51-
img := d.drawer.Dst.(*image.RGBA)
51+
img := d.drawer.Dst
5252
return img
5353
}
5454

0 commit comments

Comments
 (0)