internal/f32color: add colorspace-correct function for alpha scaling

Package material's ad-hoc mulAlpha didn't take the sRGB color-space
into account, which meant that alpha-scaled colors were subtly wrong.
Introduce f32color.MulAlpha and convert all uses to it.

Thanks to René Post for finding and debugging the issue.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-09-28 09:02:38 +02:00
parent 679a34b116
commit 4bab6fcf32
7 changed files with 31 additions and 23 deletions
+12
View File
@@ -73,3 +73,15 @@ func sRGBToLinear(c float32) float32 {
return float32(math.Pow(float64((c+0.055)/1.055), 2.4))
}
}
// MulAlpha scales all color components by alpha/255.
func MulAlpha(c color.RGBA, alpha uint8) color.RGBA {
// TODO: Optimize. This is pretty slow.
a := float32(alpha) / 255.
rgba := RGBAFromSRGB(c)
rgba.A *= a
rgba.R *= a
rgba.G *= a
rgba.B *= a
return rgba.SRGB()
}