Hi all,
I recently encountered an issue when using Go’s standard library image/draw. Specifically, I found that fully transparent pixels (alpha = 0) sometimes retain non-zero RGB values. This can cause unexpected colors when compositing the image onto a white background.
Here is a minimal reproducible example:
func main() {
// Create a source image with a fully transparent pixel
src := image.NewRGBA(image.Rect(0, 0, 2, 2))
src.SetRGBA(0, 0, color.RGBA{R: 100, G: 150, B: 200, A: 0}) // fully transparent
dst := image.NewRGBA(src.Bounds())
draw.Draw(dst, dst.Bounds(), src, image.Point{}, draw.Src)
r, g, b, a := dst.At(0, 0).RGBA()
fmt.Printf("Pixel (0,0) RGBA: %d %d %d %d\n", r>>8, g>>8, b>>8, a>>8)
}
Output:
As you can see, even though alpha is 0, the RGB channels retain their original values. This causes issues when you try to composite the image onto a white background, because these “dirty” RGB values can show up in certain blending scenarios.
In my code, I wrote a manual loop to premultiply or blend alpha into white, which works correctly. But using draw.Draw, these transparent pixels keep their original RGB values.
My questions are:
Is this the intended behavior of image/draw?
Or is this a bug (or at least a usability issue) in the standard library that should be fixed?
Thank you very much for your opinions and insights!
Note: My English is not very good, so I used an LLM to translate this post.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/9b7cb846-622c-44c1-9e9b-87ff8ee7453fn%40googlegroups.com.