primitive/primitive/color.go
拿到颜色的数字集
type Color struct {
R, G, B, A int
}
没有定义, 用图片自身的颜色, 返回 Color
// main.go 使用
var bg primitive.Color
bg = primitive.MakeColor(primitive.AverageImageColor(input))
返回, 图片的平局颜色
func MakeColor(c color.Color) Color {
r, g, b, a := c.RGBA()
return Color{int(r / 257), int(g / 257), int(b / 257), int(a / 257)}
}
用户定义了颜色, 返回 Color
// determine background color
flag.StringVar(&Background, "bg", "", "background color (hex)") // 是否 hex
var bg primitive.Color
bg = primitive.MakeHexColor(Background)
// 比如 #4292f4
func MakeHexColor(x string) Color {
x = strings.Trim(x, "#")
var r, g, b, a int
a = 255
switch len(x) {
case 3: // #fff
fmt.Sscanf(x, "%1x%1x%1x", &r, &g, &b)
r = (r << 4) | r
g = (g << 4) | g
b = (b << 4) | b
case 4: // # 4个
fmt.Sscanf(x, "%1x%1x%1x%1x", &r, &g, &b, &a)
r = (r << 4) | r
g = (g << 4) | g
b = (b << 4) | b
a = (a << 4) | a
case 6: // # 6个
fmt.Sscanf(x, "%02x%02x%02x", &r, &g, &b)
case 8: // # 8个
fmt.Sscanf(x, "%02x%02x%02x%02x", &r, &g, &b, &a)
}
return Color{r, g, b, a}
}
// model.go 使用
model.Current = uniformRGBA(target.Bounds(), background.NRGBA())
func (c *Color) NRGBA() color.NRGBA {
return color.NRGBA{uint8(c.R), uint8(c.G), uint8(c.B), uint8(c.A)}
}