Add matrix.Color.Scale

This commit is contained in:
Hajime Hoshi 2013-12-08 19:38:22 +09:00
parent b14684804f
commit cdb511b08b
2 changed files with 23 additions and 2 deletions

View File

@ -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)
}

View File

@ -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
}
}
}