package main import ( "fmt" "image/color" ) // 定义一个RGBA颜色 type MyColor struct { R, G, B, A uint8 } // 实现color.Color接口 func (c MyColor) RGBA() (r, g, b, a uint32) { r = uint32(c.R) r |= r << 8 g = uint32(c.G) g |= g << 8 b = uint32(c.B) b |= b << 8 a = uint32(c.A) a |= a << 8 return } func main() { // 创建一个自定义颜色 customColor := MyColor{R: 255, G: 0, B: 0, A: 255} // 红色 // 打印颜色的RGBA值 fmt.Printf("Custom Color: RGBA(%d, %d, %d, %d)\n", customColor.R, customColor.G, customColor.B, customColor.A) // 使用color.RGBA来创建颜色 officialColor := color.RGBA{R: 0, G: 255, B: 0, A: 255} // 绿色 fmt.Printf("Official Color: RGBA(%d, %d, %d, %d)\n", officialColor.R, officialColor.G, officialColor.B, officialColor.A) }
标签:颜色,自定义,color,RGBA,MyColor,uint32 From: https://www.cnblogs.com/xiongwei/p/18631755