support alpha in SetHexColor

This commit is contained in:
Michael Fogleman 2016-02-20 15:42:36 -05:00
parent d930cb4052
commit a46d144fed
2 changed files with 8 additions and 3 deletions

View File

@ -141,8 +141,8 @@ func (dc *Context) SetColor(c color.Color) {
} }
func (dc *Context) SetHexColor(x string) { func (dc *Context) SetHexColor(x string) {
r, g, b := parseHexColor(x) r, g, b, a := parseHexColor(x)
dc.SetRGB255(r, g, b) dc.SetRGBA255(r, g, b, a)
} }
func (dc *Context) SetRGBA255(r, g, b, a int) { func (dc *Context) SetRGBA255(r, g, b, a int) {

View File

@ -48,8 +48,9 @@ func imageToRGBA(src image.Image) *image.RGBA {
return dst return dst
} }
func parseHexColor(x string) (r, g, b int) { func parseHexColor(x string) (r, g, b, a int) {
x = strings.TrimPrefix(x, "#") x = strings.TrimPrefix(x, "#")
a = 255
if len(x) == 3 { if len(x) == 3 {
format := "%1x%1x%1x" format := "%1x%1x%1x"
fmt.Sscanf(x, format, &r, &g, &b) fmt.Sscanf(x, format, &r, &g, &b)
@ -61,6 +62,10 @@ func parseHexColor(x string) (r, g, b int) {
format := "%02x%02x%02x" format := "%02x%02x%02x"
fmt.Sscanf(x, format, &r, &g, &b) fmt.Sscanf(x, format, &r, &g, &b)
} }
if len(x) == 8 {
format := "%02x%02x%02x%02x"
fmt.Sscanf(x, format, &r, &g, &b, &a)
}
return return
} }