Add Premultiply and Unpremultiply functions

This commit is contained in:
Sasha Koshka 2024-09-10 17:39:06 -04:00
parent 6e5e9faa72
commit 9dd35c1dff

View File

@ -18,3 +18,21 @@ func ToRGBA (c color.Color) color.RGBA {
uint8(a >> 8),
}
}
// Premultiply applies alpha-premultiplication that colors must apply as per
// color.Color.RGBA. This is an intrinsically lossy proccess.
func Premultiply (ur, ug, ub, ua uint32) (r, g, b, a uint32) {
return (ur * a) / 0xFFFF,
(ug * a) / 0xFFFF,
(ub * a) / 0xFFFF,
ua
}
// Unpremultiply reverses alpha-premultiplication that colors must apply as per
// color.Color.RGBA. This may or may not return the precise original color.
func Unpremultiply (r, g, b, a uint32) (ur, ug, ub, ua uint32) {
return (r / a) * 0xFFFF,
(g / a) * 0xFFFF,
(b / a) * 0xFFFF,
a
}