shiny/iconvg: fix color blending.
Change-Id: I1be11ef544670b6d111fcd3db571b77202da103e
Reviewed-on: https://go-review.googlesource.com/30571
Reviewed-by: David Crawshaw <crawshaw@golang.org>
diff --git a/shiny/iconvg/color.go b/shiny/iconvg/color.go
index 131d303..f8ff941 100644
--- a/shiny/iconvg/color.go
+++ b/shiny/iconvg/color.go
@@ -57,14 +57,14 @@
return cReg[c.cReg()&0x3f]
}
t, c0, c1 := c.blend()
- s := 255 - t
+ p, q := uint32(255-t), uint32(t)
rgba0 := decodeColor1(c0).Resolve(pal, cReg)
rgba1 := decodeColor1(c1).Resolve(pal, cReg)
return color.RGBA{
- ((s * rgba0.R) + (t * rgba1.R) + 128) / 255,
- ((s * rgba0.G) + (t * rgba1.G) + 128) / 255,
- ((s * rgba0.B) + (t * rgba1.B) + 128) / 255,
- ((s * rgba0.A) + (t * rgba1.A) + 128) / 255,
+ uint8(((p * uint32(rgba0.R)) + q*uint32(rgba1.R) + 128) / 255),
+ uint8(((p * uint32(rgba0.G)) + q*uint32(rgba1.G) + 128) / 255),
+ uint8(((p * uint32(rgba0.B)) + q*uint32(rgba1.B) + 128) / 255),
+ uint8(((p * uint32(rgba0.A)) + q*uint32(rgba1.A) + 128) / 255),
}
}
diff --git a/shiny/iconvg/decode_test.go b/shiny/iconvg/decode_test.go
index 5d57480..d3be5a6 100644
--- a/shiny/iconvg/decode_test.go
+++ b/shiny/iconvg/decode_test.go
@@ -8,6 +8,7 @@
"bytes"
"fmt"
"image"
+ "image/color"
"image/draw"
"image/png"
"io/ioutil"
@@ -302,3 +303,17 @@
}
}
}
+
+func TestBlendColor(t *testing.T) {
+ // This example comes from doc.go. Look for "orange" in the "Colors"
+ // section.
+ pal := Palette{
+ 2: color.RGBA{0xff, 0xcc, 0x80, 0xff}, // "Material Design Orange 200".
+ }
+ cReg := [64]color.RGBA{}
+ got := BlendColor(0x40, 0x7f, 0x82).Resolve(&pal, &cReg)
+ want := color.RGBA{0x40, 0x33, 0x20, 0x40} // 25% opaque "Orange 200", alpha-premultiplied.
+ if got != want {
+ t.Errorf("\ngot %x\nwant %x", got, want)
+ }
+}