widget/material: correctly apply alpha to ProgressBar color

color.RGBA values are pre-multiplied, so transparency must be applied
to all components.

Fixes gio#117

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-05-19 15:39:43 +02:00
parent 5a8e1c5acf
commit 7a13c2c905
+13 -3
View File
@@ -55,10 +55,9 @@ func (b ProgressBarStyle) Layout(gtx *layout.Context, progress int) {
layout.Stack{Alignment: layout.W}.Layout(gtx,
layout.Stacked(func() {
// Use a transparent equivalent of progress color.
backgroundColor := b.Color
backgroundColor.A = 100
bgCol := mulAlpha(b.Color, 150)
shader(progressBarWidth, backgroundColor)
shader(progressBarWidth, bgCol)
}),
layout.Stacked(func() {
fillWidth := (progressBarWidth / 100) * float32(progress)
@@ -66,3 +65,14 @@ func (b ProgressBarStyle) Layout(gtx *layout.Context, progress int) {
}),
)
}
// mulAlpha scales all color components by alpha/255.
func mulAlpha(c color.RGBA, alpha uint8) color.RGBA {
a := uint16(alpha)
return color.RGBA{
A: uint8(uint16(c.A) * a / 255),
R: uint8(uint16(c.R) * a / 255),
G: uint8(uint16(c.G) * a / 255),
B: uint8(uint16(c.B) * a / 255),
}
}