Change ColorMatrix's methods to functions

This commit is contained in:
Hajime Hoshi 2014-12-15 02:04:33 +09:00
parent 2c67a71a53
commit 6eaec92a4e
5 changed files with 30 additions and 23 deletions

View File

@ -81,6 +81,32 @@ func Monochrome() ColorMatrix {
}
}
// ScaleColor returns a color matrix that scales a color matrix by clr.
func ScaleColor(clr color.Color) ColorMatrix {
rf, gf, bf, af := rgba(clr)
return ColorMatrix{
[ColorMatrixDim - 1][ColorMatrixDim]float64{
{rf, 0, 0, 0, 0},
{0, gf, 0, 0, 0},
{0, 0, bf, 0, 0},
{0, 0, 0, af, 0},
},
}
}
// TranslateColor returns a color matrix that translates a color matrix by clr.
func (c *ColorMatrix) Translate(clr color.Color) ColorMatrix {
rf, gf, bf, af := rgba(clr)
return ColorMatrix{
[ColorMatrixDim - 1][ColorMatrixDim]float64{
{1, 0, 0, 0, rf},
{0, 1, 0, 0, gf},
{0, 0, 1, 0, bf},
{0, 0, 0, 1, af},
},
}
}
// RotateHue returns a color matrix to rotate the hue
func RotateHue(theta float64) ColorMatrix {
sin, cos := math.Sincos(theta)
@ -106,22 +132,3 @@ func rgba(clr color.Color) (float64, float64, float64, float64) {
af := float64(a) / float64(math.MaxUint16)
return rf, gf, bf, af
}
// Scale scales the color matrix by clr.
func (c *ColorMatrix) Scale(clr color.Color) {
rf, gf, bf, af := rgba(clr)
for i, e := range []float64{rf, gf, bf, af} {
for j := 0; j < 4; j++ {
c.Elements[i][j] *= e
}
}
}
// Translate translates the color matrix by clr.
func (c *ColorMatrix) Translate(clr color.Color) {
rf, gf, bf, af := rgba(clr)
c.Elements[0][4] = rf
c.Elements[1][4] = gf
c.Elements[2][4] = bf
c.Elements[3][4] = af
}

View File

@ -57,7 +57,7 @@ func (d *debugPrintState) drawText(gr ebiten.GraphicsContext, str string, x, y i
geom := ebiten.GeometryMatrixI()
geom.Concat(ebiten.TranslateGeometry(float64(x)+1, float64(y)))
clrm := ebiten.ColorMatrixI()
clrm.Scale(clr)
clrm.Concat(ebiten.ScaleColor(clr))
gr.Texture(d.textTexture).Draw(parts, geom, clrm)
}

View File

@ -59,7 +59,7 @@ func drawText(context ebiten.GraphicsContext, textures *Textures, str string, x,
geoMat.Concat(ebiten.ScaleGeometry(float64(scale), float64(scale)))
geoMat.Concat(ebiten.TranslateGeometry(float64(x), float64(y)))
clrMat := ebiten.ColorMatrixI()
clrMat.Scale(clr)
clrMat.Concat(ebiten.ScaleColor(clr))
context.Texture(fontTextureId).Draw(parts, geoMat, clrMat)
}

View File

@ -119,7 +119,7 @@ func (s *GameScene) Draw(context ebiten.GraphicsContext, textures *Textures) {
geoMat.Concat(ebiten.ScaleGeometry(float64(fieldWidth)/float64(emptyWidth), float64(fieldHeight)/float64(emptyHeight)))
geoMat.Concat(ebiten.TranslateGeometry(20, 20)) // TODO: magic number?
colorMat := ebiten.ColorMatrixI()
colorMat.Scale(color.RGBA{0, 0, 0, 0x80})
colorMat.Concat(ebiten.ScaleColor(color.RGBA{0, 0, 0, 0x80}))
ebiten.DrawWhole(context.Texture(field), emptyWidth, emptyHeight, geoMat, colorMat)
geoMat = ebiten.GeometryMatrixI()

View File

@ -57,7 +57,7 @@ func (g *Game) Draw(gr ebiten.GraphicsContext) error {
geo := ebiten.GeometryMatrixI()
geo.Concat(ebiten.TranslateGeometry(float64(mx), float64(my)))
clr := ebiten.ColorMatrixI()
clr.Scale(color.RGBA{0xff, 0xff, 0x00, 0xff})
clr.Concat(ebiten.ScaleColor(color.RGBA{0xff, 0xff, 0x00, 0xff}))
theta := 2.0 * math.Pi * float64(g.count%60) / 60.0
clr.Concat(ebiten.RotateHue(theta))
ebiten.DrawWhole(gr.RenderTarget(g.brushRenderTarget), 1, 1, geo, clr)