goutil/image/color/color.go

39 lines
976 B
Go
Raw Normal View History

2024-06-23 06:45:58 -06:00
package ucolor
import "image/color"
// Transparent returns whether or not a color has transparency.
func Transparent (c color.Color) bool {
_, _, _, a := c.RGBA()
return a != 0xFFFF
}
// ToRGBA converts any color to a color.RGBA value.
func ToRGBA (c color.Color) color.RGBA {
r, g, b, a := c.RGBA()
return color.RGBA {
uint8(r >> 8),
uint8(g >> 8),
uint8(b >> 8),
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) {
2024-09-12 01:13:35 -06:00
return (ur * ua) / 0xFFFF,
(ug * ua) / 0xFFFF,
(ub * ua) / 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
}