diff --git a/example/game.go b/example/game.go index c617ef6fe..b2bf04877 100644 --- a/example/game.go +++ b/example/game.go @@ -1,6 +1,7 @@ package main import ( + "image/color" "fmt" "github.com/hajimehoshi/go-ebiten/graphics" "github.com/hajimehoshi/go-ebiten/graphics/matrix" @@ -75,10 +76,11 @@ func (game *Game) Draw(g graphics.Canvas) { g.Fill(128, 128, 255) game.drawTexture(g, game.drawInfo.textureGeo, matrix.IdentityColor()) - game.drawText(g, game.drawInfo.inputStr, 5, 5) + game.drawText(g, game.drawInfo.inputStr, 6, 6, &color.RGBA{0x0, 0x0, 0x0, 0x80}) + game.drawText(g, game.drawInfo.inputStr, 5, 5, color.White) } -func (game *Game) drawText(g graphics.Canvas, text string, x, y int) { +func (game *Game) drawText(g graphics.Canvas, text string, x, y int, clr color.Color) { const letterWidth = 6 const letterHeight = 16 @@ -106,6 +108,7 @@ func (game *Game) drawText(g graphics.Canvas, text string, x, y int) { geometryMatrix := matrix.IdentityGeometry() geometryMatrix.Translate(float64(x), float64(y)) colorMatrix := matrix.IdentityColor() + colorMatrix.Scale(clr) g.DrawTextureParts(game.drawInfo.textures["text"], parts, geometryMatrix, colorMatrix) } diff --git a/graphics/matrix/color.go b/graphics/matrix/color.go index 4cc4c28ed..91a9ffa0a 100644 --- a/graphics/matrix/color.go +++ b/graphics/matrix/color.go @@ -1,5 +1,10 @@ package matrix +import ( + "image/color" + "math" +) + const colorDim = 5 type Color struct { @@ -52,3 +57,16 @@ func Monochrome() Color { }, } } + +func (matrix *Color) Scale(clr color.Color) { + r, g, b, a := clr.RGBA() + rf := float64(r) / float64(math.MaxUint16) + gf := float64(g) / float64(math.MaxUint16) + bf := float64(b) / float64(math.MaxUint16) + af := float64(a) / float64(math.MaxUint16) + for i, e := range []float64{rf, gf, bf, af} { + for j := 0; j < 4; j++ { + matrix.Elements[i][j] *= e + } + } +}