ebiten: add ColorM.ScaleWithColor

Updates #1944
This commit is contained in:
Hajime Hoshi 2022-01-10 17:36:42 +09:00
parent 245cbc2f4d
commit e687865c8c
6 changed files with 16 additions and 45 deletions

View File

@ -76,6 +76,16 @@ func (c *ColorM) Scale(r, g, b, a float64) {
c.impl = c.affineColorM().Scale(float32(r), float32(g), float32(b), float32(a))
}
// ScaleWithColor scales the matrix by clr.
func (c *ColorM) ScaleWithColor(clr color.Color) {
cr, cg, cb, ca := clr.RGBA()
if ca == 0 {
c.Scale(0, 0, 0, 0)
return
}
c.Scale(float64(cr)/float64(ca), float64(cg)/float64(ca), float64(cb)/float64(ca), float64(ca)/0xffff)
}
// Translate translates the matrix by (r, g, b, a).
func (c *ColorM) Translate(r, g, b, a float64) {
c.impl = c.affineColorM().Translate(float32(r), float32(g), float32(b), float32(a))

View File

@ -31,14 +31,6 @@ func init() {
emptyImage.Fill(color.White)
}
func colorToScale(clr color.Color) (float64, float64, float64, float64) {
cr, cg, cb, ca := clr.RGBA()
if ca == 0 {
return 0, 0, 0, 0
}
return float64(cr) / float64(ca), float64(cg) / float64(ca), float64(cb) / float64(ca), float64(ca) / 0xffff
}
// DrawLine draws a line segment on the given destination dst.
//
// DrawLine is intended to be used mainly for debugging or prototyping purpose.
@ -49,7 +41,7 @@ func DrawLine(dst *ebiten.Image, x1, y1, x2, y2 float64, clr color.Color) {
op.GeoM.Scale(length, 1)
op.GeoM.Rotate(math.Atan2(y2-y1, x2-x1))
op.GeoM.Translate(x1, y1)
op.ColorM.Scale(colorToScale(clr))
op.ColorM.ScaleWithColor(clr)
// Filter must be 'nearest' filter (default).
// Linear filtering would make edges blurred.
dst.DrawImage(emptySubImage, op)
@ -62,7 +54,7 @@ func DrawRect(dst *ebiten.Image, x, y, width, height float64, clr color.Color) {
op := &ebiten.DrawImageOptions{}
op.GeoM.Scale(width, height)
op.GeoM.Translate(x, y)
op.ColorM.Scale(colorToScale(clr))
op.ColorM.ScaleWithColor(clr)
// Filter must be 'nearest' filter (default).
// Linear filtering would make edges blurred.
dst.DrawImage(emptyImage.SubImage(image.Rect(1, 1, 2, 2)).(*ebiten.Image), op)

View File

@ -129,8 +129,7 @@ func (b *Board) Draw(boardImage *ebiten.Image) {
x := i*tileSize + (i+1)*tileMargin
y := j*tileSize + (j+1)*tileMargin
op.GeoM.Translate(float64(x), float64(y))
r, g, b, a := colorToScale(tileBackgroundColor(v))
op.ColorM.Scale(r, g, b, a)
op.ColorM.ScaleWithColor(tileBackgroundColor(v))
boardImage.DrawImage(tileImage, op)
}
}

View File

@ -322,21 +322,6 @@ func (t *Tile) Update() error {
return nil
}
func colorToScale(clr color.Color) (float64, float64, float64, float64) {
r, g, b, a := clr.RGBA()
rf := float64(r) / 0xffff
gf := float64(g) / 0xffff
bf := float64(b) / 0xffff
af := float64(a) / 0xffff
// Convert to non-premultiplied alpha components.
if 0 < af {
rf /= af
gf /= af
bf /= af
}
return rf, gf, bf, af
}
func mean(a, b int, rate float64) int {
return int(float64(a)*(1-rate) + float64(b)*rate)
}
@ -398,8 +383,7 @@ func (t *Tile) Draw(boardImage *ebiten.Image) {
op.GeoM.Translate(float64(tileSize/2), float64(tileSize/2))
}
op.GeoM.Translate(float64(x), float64(y))
r, g, b, a := colorToScale(tileBackgroundColor(v))
op.ColorM.Scale(r, g, b, a)
op.ColorM.ScaleWithColor(tileBackgroundColor(v))
boardImage.DrawImage(tileImage, op)
str := strconv.Itoa(v)

View File

@ -100,16 +100,7 @@ func (i *Image) Fill(clr color.Color) {
op := &DrawImageOptions{}
op.GeoM.Scale(float64(w), float64(h))
r, g, b, a := clr.RGBA()
var rf, gf, bf, af float64
if a > 0 {
rf = float64(r) / float64(a)
gf = float64(g) / float64(a)
bf = float64(b) / float64(a)
af = float64(a) / 0xffff
}
op.ColorM.Scale(rf, gf, bf, af)
op.ColorM.ScaleWithColor(clr)
op.CompositeMode = CompositeModeCopy
i.DrawImage(emptySubImage, op)

View File

@ -179,14 +179,9 @@ var textM sync.Mutex
//
// Draw is concurrent-safe.
func Draw(dst *ebiten.Image, text string, face font.Face, x, y int, clr color.Color) {
cr, cg, cb, ca := clr.RGBA()
if ca == 0 {
return
}
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(x), float64(y))
op.ColorM.Scale(float64(cr)/float64(ca), float64(cg)/float64(ca), float64(cb)/float64(ca), float64(ca)/0xffff)
op.ColorM.ScaleWithColor(clr)
DrawWithOptions(dst, text, face, op)
}