func main(){ src := image.NewRGBA(image.Rect(0, 0, 2, 2)) src.SetRGBA(0, 0, color.RGBA{R: 100, G: 150, B: 200, A: 0})
dst := image.NewRGBA(src.Bounds()) draw.Draw(dst, dst.Bounds(), &image.Uniform{color.White}, image.Point{}, draw.Src) draw.Draw(dst, dst.Bounds(), src, image.Point{}, draw.Over) 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) } result: Pixel (0,0) RGBA: 100 150 200 255 However, when I tried to blend a transparent pixel onto a white background, the result was not white, which is what really confused me. 在2025年8月21日星期四 UTC+8 07:10:20<Rob Pike> 写道: > This is correct behavior. The original pixel has value (100, 150, 200, 0) > and you use the Src operator, which copies the source to the destination > unmodified. > > -rob > > > On Thu, Aug 21, 2025 at 4:55 AM SugarMGP <a2350...@gmail.com> wrote: > >> 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:* >> >> Pixel (0,0) RGBA: 100 150 200 0 >> >> 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 >> >> <https://groups.google.com/d/msgid/golang-nuts/9b7cb846-622c-44c1-9e9b-87ff8ee7453fn%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > -- 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+unsubscr...@googlegroups.com. To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/f8cb79d3-4c35-4910-b3a3-d4b8ad08ac1en%40googlegroups.com.