It might help to review the concept of an Alpha channel, and in
particular look at the rationale for non-premultiplied alphas that the PNG 
format
describes.

https://www.w3.org/TR/PNG-DataRep.html#DR.Alpha-channel
https://www.w3.org/TR/PNG-Rationale.html#R.Non-premultiplied-alpha


On Wednesday, August 20, 2025 at 7:55:34 PM UTC+1 SugarMGP 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+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/golang-nuts/3de5794a-667a-42cf-9d9a-bc8d29a687ddn%40googlegroups.com.

Reply via email to