Backtestshugorun-20261005-133023#12536

Issue #12536 Merged fix PR # Run 5 Oct 2026 · run-20261005-133023 gohugoio/hugo

images: Opacity problem with Resize + Padding with PNG output

Better

Our patch was judged better than the merged fix.

In this judgment, Patch A = our agent, Patch B = the merged fix.

Both patches fix the root cause the same way. `hexStringToColorGo` built a translucent hex color as a `color.RGBA`, which is an invalid premultiplied color (component > alpha) and gets corrupted when drawn onto premultiplied destinations. Each patch now returns `color.NRGBA` for translucent colors, and each updates the `padding-transparent-chained` golden image.

Patch A stays narrowly scoped. It keeps opaque colors as `color.RGBA`, so existing equality expectations and tests are unchanged. It adds a small helper so hex and luminance round-trip correctly for NRGBA. Its tests are strong: a new `TestPaddingTransparentColor` runs `gift` Padding with `#00f7` against RGBA, NRGBA, RGBA64 and NRGBA64 destinations and checks the exact padded pixel. That test would fail before the fix. It also adds hex parse and round-trip cases.

Patch B goes much further, and that adds risk:
- It always rewrites `Color.color` to NRGBA and changes opaque hex parsing to NRGBA, which required editing existing test expectations.
- It changes `ColorGoToHexString` to un-premultiply every input. This alters output for existing RGBA inputs, and the old RGBA test case was removed rather than kept.
- It rewrites `AddColorToPalette` equality.
- It changes `doWithImageConfig` to skip palette clamping for all filter actions. That fixes a different issue, alters overlay/text golden images, and changes output for indexed PNGs across every filter.

None of that is needed to fix this issue, and it broadens behavioral change. Patch B also adds no direct padding test; it relies on golden image updates. Patch A is the more focused, better-tested fix.

The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.

Solve

44m 30s

Wall-clock time in the container

Full test suite

No new failures

3,761 passed · 99 skipped · 3,862 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

73vs 58 merged

Changed lines, ours vs merged

What version of Hugo are you using (hugo version)?

$ hugo version
hugo v0.126.1+extended linux/amd64

Does this issue reproduce with the latest release?

Yes.

Report

The 0 function currently ignores the transparency setting when padding a PNG image. While the documentation does state 'conversion to WEBP is required to support transparency' (under Other recipes), I can't see a reason why PNG shouldn't be supported, given that it has an alpha channel. For the moment I have resorted to first converting the image to WEBP, then applying transparent padding, and then converting it back to PNG, but this does not feel like an elegant solution.

The two patches

Our patch+68 −5
resources/images/color.goindex fe233c7b7..704e71b70 100644@@ -70,8 +70,8 @@ func (c Color) Key() string {  func (c *Color) init() error { 	c.hex = ColorGoToHexString(c.color)-	r, g, b, _ := c.color.RGBA()-	c.luminance = 0.2126*c.toSRGB(uint8(r)) + 0.7152*c.toSRGB(uint8(g)) + 0.0722*c.toSRGB(uint8(b))+	rgba := colorGoToRGBA8(c.color)+	c.luminance = 0.2126*c.toSRGB(rgba.R) + 0.7152*c.toSRGB(rgba.G) + 0.0722*c.toSRGB(rgba.B) 	return nil } @@ -108,14 +108,24 @@ func ReplaceColorInPalette(c color.Color, p color.Palette) {  // ColorGoToHexString converts a color.Color to a hex string. func ColorGoToHexString(c color.Color) string {-	r, g, b, a := c.RGBA()-	rgba := color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)}+	rgba := colorGoToRGBA8(c) 	if rgba.A == 0xff { 		return fmt.Sprintf("#%.2x%.2x%.2x", rgba.R, rgba.G, rgba.B) 	} 	return fmt.Sprintf("#%.2x%.2x%.2x%.2x", rgba.R, rgba.G, rgba.B, rgba.A) } +// colorGoToRGBA8 returns the 8-bit channel values of c.+// A color.NRGBA (e.g. a translucent hex color) keeps its+// non-alpha-premultiplied values so it survives a round trip to hex.+func colorGoToRGBA8(c color.Color) color.RGBA {+	if nc, ok := c.(color.NRGBA); ok {+		return color.RGBA{nc.R, nc.G, nc.B, nc.A}+	}+	r, g, b, a := c.RGBA()+	return color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)}+}+ // ColorGoToColor converts a color.Color to a Color. func ColorGoToColor(c color.Color) Color { 	cc := Color{color: c}@@ -195,5 +205,12 @@ func hexStringToColorGo(s string) (color.Color, error) { 		return nil, err 	} -	return color.RGBA{b[0], b[1], b[2], b[3]}, nil+	// Hex colors are not alpha-premultiplied, so translucent colors must be+	// represented as color.NRGBA. Storing them in a color.RGBA would make an+	// invalid premultiplied color (e.g. R > A), which renders wrongly when+	// drawn onto premultiplied images such as *image.RGBA.+	if b[3] == 0xff {+		return color.RGBA{b[0], b[1], b[2], b[3]}, nil+	}+	return color.NRGBA{b[0], b[1], b[2], b[3]}, nil }resources/images/color_test.goindex 540f3bea8..9492be386 100644@@ -41,6 +41,10 @@ func TestHexStringToColor(t *testing.T) { 		{"#000", color.Black}, 		{"#4287f5", color.RGBA{R: 0x42, G: 0x87, B: 0xf5, A: 0xff}}, 		{"777", color.RGBA{R: 0x77, G: 0x77, B: 0x77, A: 0xff}},+		// Translucent colors are not alpha-premultiplied. See issue 12536.+		{"#00f7", color.NRGBA{R: 0x00, G: 0x00, B: 0xff, A: 0x77}},+		{"#4287f580", color.NRGBA{R: 0x42, G: 0x87, B: 0xf5, A: 0x80}},+		{"#4287f5ff", color.RGBA{R: 0x42, G: 0x87, B: 0xf5, A: 0xff}}, 	} {  		c.Run(test.arg, func(c *qt.C) {@@ -75,6 +79,8 @@ func TestColorToHexString(t *testing.T) { 		// Note that the .Colors (dominant colors) received from the Image resource 		// will always have an alpha value of 0xff. 		{color.RGBA{R: 0x42, G: 0x87, B: 0xf5, A: 0x80}, "#4287f580"},+		{color.NRGBA{R: 0x42, G: 0x87, B: 0xf5, A: 0x80}, "#4287f580"},+		{color.NRGBA{R: 0x42, G: 0x87, B: 0xf5, A: 0xff}, "#4287f5"}, 	} {  		c.Run(test.expect, func(c *qt.C) {@@ -122,4 +128,7 @@ func TestColorLuminance(t *testing.T) { 	c.Assert(hexStringToColor("#768a9a").Luminance(), hqt.IsSameFloat64, 0.24361603589088263) 	c.Assert(hexStringToColor("#d5bc9f").Luminance(), hqt.IsSameFloat64, 0.5261577672685374) 	c.Assert(hexStringToColor("#ffffff").Luminance(), hqt.IsSameFloat64, 1.0)+	// Luminance ignores the alpha channel.+	c.Assert(hexStringToColor("#768a9a80").Luminance(), hqt.IsSameFloat64, 0.24361603589088263)+	c.Assert(hexStringToColor("#768a9a80").ColorHex(), qt.Equals, "#768a9a80") }resources/images/filters_test.goindex ce8ae9f5a..5d1a08fbd 100644@@ -14,9 +14,13 @@ package images  import (+	"image"+	"image/color"+	"image/draw" 	"testing"  	qt "github.com/frankban/quicktest"+	"github.com/gohugoio/gift" 	"github.com/gohugoio/hugo/common/hashing" ) @@ -30,3 +34,36 @@ func TestFilterHash(t *testing.T) { 	c.Assert(hashing.HashString(f.Gamma(32)), qt.Not(qt.Equals), hashing.HashString(f.Gamma(33))) 	c.Assert(hashing.HashString(f.Gamma(32)), qt.Equals, hashing.HashString(f.Gamma(32))) }++// The translucent padding color must be preserved regardless of whether the+// destination image is alpha-premultiplied or not.+// See issue 12536.+func TestPaddingTransparentColor(t *testing.T) {+	c := qt.New(t)++	f := &Filters{}+	red := color.NRGBA{R: 0xff, A: 0xff}++	for _, test := range []struct {+		name   string+		newDst func(r image.Rectangle) draw.Image+	}{+		{"RGBA", func(r image.Rectangle) draw.Image { return image.NewRGBA(r) }},+		{"NRGBA", func(r image.Rectangle) draw.Image { return image.NewNRGBA(r) }},+		{"RGBA64", func(r image.Rectangle) draw.Image { return image.NewRGBA64(r) }},+		{"NRGBA64", func(r image.Rectangle) draw.Image { return image.NewNRGBA64(r) }},+	} {+		c.Run(test.name, func(c *qt.C) {+			src := test.newDst(image.Rect(0, 0, 10, 10))+			draw.Draw(src, src.Bounds(), image.NewUniform(red), image.Point{}, draw.Src)++			g := gift.New(f.Padding(5, "#00f7"))+			dst := test.newDst(g.Bounds(src.Bounds()))+			g.Draw(dst, src)++			c.Assert(dst.Bounds(), qt.Equals, image.Rect(0, 0, 20, 20))+			c.Assert(color.NRGBAModel.Convert(dst.At(0, 0)), qt.Equals, color.NRGBA{B: 0xff, A: 0x77})+			c.Assert(color.NRGBAModel.Convert(dst.At(10, 10)), qt.Equals, red)+		})+	}+}resources/images/testdata/images_golden/filters/padding/padding-transparent-chained.pngindex 899e21f25..34392755c 100644Binary files a/resources/images/testdata/images_golden/filters/padding/padding-transparent-chained.png and b/resources/images/testdata/images_golden/filters/padding/padding-transparent-chained.png differ 
Merged fix · PR #+39 −19
resources/image.goindex 467e23f26..a2ed5f3c9 100644@@ -321,7 +321,7 @@ func (i *imageResource) Filter(filters ...any) (images.ImageResource, error) { 		return nil, err 	} -	confMain.Action = "filter"+	confMain.Action = images.ActionFilter 	confMain.Key = hashing.HashString(gfilters)  	return i.doWithImageConfig(confMain, func(src image.Image) (image.Image, error) {@@ -438,7 +438,11 @@ func (i *imageResource) doWithImageConfig(conf images.ImageConfig, f func(src im 			converted = tmp 		} -		if conf.TargetFormat == images.PNG {+		// Preserve the source palette for the geometric actions so an indexed+		// PNG stays indexed. Filters (e.g. images.Overlay, images.Text) routinely+		// introduce colors outside the source palette, and clamping the result+		// back to that palette destroys the filtered output. See issue 12543.+		if conf.TargetFormat == images.PNG && conf.Action != images.ActionFilter { 			// Apply the colour palette from the source 			if paletted, ok := src.(*image.Paletted); ok { 				palette := paletted.Paletteresources/images/color.goindex fe233c7b7..72f3710e3 100644@@ -69,9 +69,12 @@ func (c Color) Key() string { }  func (c *Color) init() error {-	c.hex = ColorGoToHexString(c.color)-	r, g, b, _ := c.color.RGBA()-	c.luminance = 0.2126*c.toSRGB(uint8(r)) + 0.7152*c.toSRGB(uint8(g)) + 0.0722*c.toSRGB(uint8(b))+	// Normalize to the canonical non-premultiplied form so that equal colors+	// compare equal whether they came from a hex string or from image pixels.+	nrgba := color.NRGBAModel.Convert(c.color).(color.NRGBA)+	c.color = nrgba+	c.hex = ColorGoToHexString(nrgba)+	c.luminance = 0.2126*c.toSRGB(nrgba.R) + 0.7152*c.toSRGB(nrgba.G) + 0.0722*c.toSRGB(nrgba.B) 	return nil } @@ -88,10 +91,13 @@ func (c Color) toSRGB(i uint8) float64 { // Note that it does no additional checks, so callers must make sure // that the palette is valid for the relevant format. func AddColorToPalette(c color.Color, p color.Palette) color.Palette {-	var found bool-	if slices.Contains(p, c) {-		found = true-	}+	// Compare color values rather than interface values, so that e.g. a+	// color.NRGBA is found even if the palette holds it as a color.RGBA.+	cr, cg, cb, ca := c.RGBA()+	found := slices.ContainsFunc(p, func(v color.Color) bool {+		vr, vg, vb, va := v.RGBA()+		return cr == vr && cg == vg && cb == vb && ca == va+	})  	if !found { 		p = append(color.Palette{c}, p...)@@ -108,12 +114,13 @@ func ReplaceColorInPalette(c color.Color, p color.Palette) {  // ColorGoToHexString converts a color.Color to a hex string. func ColorGoToHexString(c color.Color) string {-	r, g, b, a := c.RGBA()-	rgba := color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)}-	if rgba.A == 0xff {-		return fmt.Sprintf("#%.2x%.2x%.2x", rgba.R, rgba.G, rgba.B)+	// Hex notation is not alpha-premultiplied, so convert via NRGBA to+	// un-premultiply the color components of any transparent color.+	nrgba := color.NRGBAModel.Convert(c).(color.NRGBA)+	if nrgba.A == 0xff {+		return fmt.Sprintf("#%.2x%.2x%.2x", nrgba.R, nrgba.G, nrgba.B) 	}-	return fmt.Sprintf("#%.2x%.2x%.2x%.2x", rgba.R, rgba.G, rgba.B, rgba.A)+	return fmt.Sprintf("#%.2x%.2x%.2x%.2x", nrgba.R, nrgba.G, nrgba.B, nrgba.A) }  // ColorGoToColor converts a color.Color to a Color.@@ -195,5 +202,8 @@ func hexStringToColorGo(s string) (color.Color, error) { 		return nil, err 	} -	return color.RGBA{b[0], b[1], b[2], b[3]}, nil+	// Hex notation is not alpha-premultiplied, so this must be a color.NRGBA;+	// as a color.RGBA any transparent color would be invalid (component > alpha)+	// and corrupted when drawn onto an alpha-premultiplied image. See issue 12536.+	return color.NRGBA{b[0], b[1], b[2], b[3]}, nil }resources/images/color_test.goindex 540f3bea8..47bc5ae8b 100644@@ -39,8 +39,12 @@ func TestHexStringToColor(t *testing.T) { 		{"#ffffff", color.White}, 		{"ffffff", color.White}, 		{"#000", color.Black},-		{"#4287f5", color.RGBA{R: 0x42, G: 0x87, B: 0xf5, A: 0xff}},-		{"777", color.RGBA{R: 0x77, G: 0x77, B: 0x77, A: 0xff}},+		{"#4287f5", color.NRGBA{R: 0x42, G: 0x87, B: 0xf5, A: 0xff}},+		{"777", color.NRGBA{R: 0x77, G: 0x77, B: 0x77, A: 0xff}},+		// Hex notation is not alpha-premultiplied, so a transparent color+		// must parse to a color.NRGBA. See issue 12536.+		{"#00f7", color.NRGBA{R: 0x00, G: 0x00, B: 0xff, A: 0x77}},+		{"4287f580", color.NRGBA{R: 0x42, G: 0x87, B: 0xf5, A: 0x80}}, 	} {  		c.Run(test.arg, func(c *qt.C) {@@ -71,10 +75,11 @@ func TestColorToHexString(t *testing.T) { 		{color.Black, "#000000"}, 		{color.RGBA{R: 0x42, G: 0x87, B: 0xf5, A: 0xff}, "#4287f5"}, -		// 50% opacity.+		// 50% opacity. Hex notation is not alpha-premultiplied, so the+		// components of a transparent color are the non-premultiplied ones. 		// Note that the .Colors (dominant colors) received from the Image resource 		// will always have an alpha value of 0xff.-		{color.RGBA{R: 0x42, G: 0x87, B: 0xf5, A: 0x80}, "#4287f580"},+		{color.NRGBA{R: 0x42, G: 0x87, B: 0xf5, A: 0x80}, "#4287f580"}, 	} {  		c.Run(test.expect, func(c *qt.C) {resources/images/config.goindex 4cbd7e629..e8fd69c94 100644@@ -36,6 +36,7 @@ const ( 	ActionCrop   = "crop" 	ActionFit    = "fit" 	ActionFill   = "fill"+	ActionFilter = "filter" )  var Actions = map[string]bool{resources/images/testdata/images_golden/filters/padding/padding-transparent-chained.pngindex 899e21f25..34392755c 100644Binary files a/resources/images/testdata/images_golden/filters/padding/padding-transparent-chained.png and b/resources/images/testdata/images_golden/filters/padding/padding-transparent-chained.png differresources/images/testdata/images_golden/filters/paletted/overlay.pngindex 10d9dd6fd..631854dd8 100644Binary files a/resources/images/testdata/images_golden/filters/paletted/overlay.png and b/resources/images/testdata/images_golden/filters/paletted/overlay.png differresources/images/testdata/images_golden/filters/paletted/text.pngindex 71a9c3e2d..65d1a60ce 100644Binary files a/resources/images/testdata/images_golden/filters/paletted/text.png and b/resources/images/testdata/images_golden/filters/paletted/text.png differ