~eliasnaur/gio

7a13c2c905ddb22b22faacaeb89168a282e13073 — Elias Naur 8 days ago 5a8e1c5
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>
1 files changed, 13 insertions(+), 3 deletions(-)

M widget/material/progressbar.go
M widget/material/progressbar.go => widget/material/progressbar.go +13 -3
@@ 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),
	}
}