affine: Bug fix: ColorM.Apply was wrong when alpha is 0

This commit is contained in:
Hajime Hoshi 2018-02-27 12:16:16 +09:00
parent deed56f971
commit bea63946fc
2 changed files with 23 additions and 14 deletions

View File

@ -170,11 +170,11 @@ func TestColorMConcatSelf(t *testing.T) {
} }
} }
func absU32(x uint32) uint32 { func absDiffU32(x, y uint32) uint32 {
if x < 0 { if x < y {
return -x return y - x
} }
return x return x - y
} }
func TestColorMApply(t *testing.T) { func TestColorMApply(t *testing.T) {
@ -184,6 +184,9 @@ func TestColorMApply(t *testing.T) {
shiny := ColorM{} shiny := ColorM{}
shiny.Translate(1, 1, 1, 0) shiny.Translate(1, 1, 1, 0)
shift := ColorM{}
shift.Translate(0.5, 0.5, 0.5, 0.5)
cases := []struct { cases := []struct {
ColorM ColorM ColorM ColorM
In color.Color In color.Color
@ -214,14 +217,20 @@ func TestColorMApply(t *testing.T) {
Out: color.RGBA{0xb0, 0xb0, 0xb0, 0xb0}, Out: color.RGBA{0xb0, 0xb0, 0xb0, 0xb0},
Delta: 1, Delta: 1,
}, },
{
ColorM: shift,
In: color.RGBA{0x00, 0x00, 0x00, 0x00},
Out: color.RGBA{0x40, 0x40, 0x40, 0x80},
Delta: 0x101,
},
} }
for _, c := range cases { for _, c := range cases {
out := c.ColorM.Apply(c.In) out := c.ColorM.Apply(c.In)
r0, g0, b0, a0 := out.RGBA() r0, g0, b0, a0 := out.RGBA()
r1, g1, b1, a1 := c.Out.RGBA() r1, g1, b1, a1 := c.Out.RGBA()
if absU32(r0-r1) > c.Delta || absU32(g0-g1) > c.Delta || if absDiffU32(r0, r1) > c.Delta || absDiffU32(g0, g1) > c.Delta ||
absU32(b0-b1) > c.Delta || absU32(a0-a1) > c.Delta { absDiffU32(b0, b1) > c.Delta || absDiffU32(a0, a1) > c.Delta {
t.Errorf("%v.Apply(%v) = %v, want %v", c.ColorM, c.In, out, c.Out) t.Errorf("%v.Apply(%v) = {%d, %d, %d, %d}, want {%d, %d, %d, %d}", c.ColorM, c.In, r0, g0, b0, a0, r1, g1, b1, a1)
} }
} }
} }

View File

@ -69,14 +69,14 @@ func (c *ColorM) Apply(clr color.Color) color.Color {
return clr return clr
} }
r, g, b, a := clr.RGBA() r, g, b, a := clr.RGBA()
if a == 0 { rf, gf, bf, af := float32(0.0), float32(0.0), float32(0.0), float32(0.0)
return color.Transparent
}
// Unmultiply alpha // Unmultiply alpha
rf := float32(r) / float32(a) if a > 0 {
gf := float32(g) / float32(a) rf = float32(r) / float32(a)
bf := float32(b) / float32(a) gf = float32(g) / float32(a)
af := float32(a) / 0xffff bf = float32(b) / float32(a)
af = float32(a) / 0xffff
}
eb := c.body eb := c.body
et := c.translate et := c.translate
rf2 := eb[0]*rf + eb[4]*gf + eb[8]*bf + eb[12]*af + et[0] rf2 := eb[0]*rf + eb[4]*gf + eb[8]*bf + eb[12]*af + et[0]