Skip to content

Commit

Permalink
rgba() color parsing now uses a float in range 0.0-1.0 for the alpha …
Browse files Browse the repository at this point in the history
…component
  • Loading branch information
tfriedel6 committed Dec 1, 2020
1 parent 0c528ec commit 364c870
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions color.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,16 @@ func parseColor(value ...interface{}) (c color.RGBA, ok bool) {
}
} else {
v = strings.Replace(v, " ", "", -1)
var ir, ig, ib, ia int
var ir, ig, ib int
n, err := fmt.Sscanf(v, "rgb(%d,%d,%d)", &ir, &ig, &ib)
if err == nil && n == 3 {
return color.RGBA{R: uint8(ir), G: uint8(ig), B: uint8(ib), A: 255}, true
}
n, err = fmt.Sscanf(v, "rgba(%d,%d,%d,%d)", &ir, &ig, &ib, &ia)
var fa float64
n, err = fmt.Sscanf(v, "rgba(%d,%d,%d,%f)", &ir, &ig, &ib, &fa)
fa = math.Max(0, math.Min(1, fa))
if err == nil && n == 4 {
return color.RGBA{R: uint8(ir), G: uint8(ig), B: uint8(ib), A: uint8(ia)}, true
return color.RGBA{R: uint8(ir), G: uint8(ig), B: uint8(ib), A: uint8(fa * 255)}, true
}
}
}
Expand Down

0 comments on commit 364c870

Please sign in to comment.