From 7a13c2c905ddb22b22faacaeb89168a282e13073 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Tue, 19 May 2020 15:39:43 +0200 Subject: [PATCH] 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 --- widget/material/progressbar.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/widget/material/progressbar.go b/widget/material/progressbar.go index 1293ab8c..33e8c974 100644 --- a/widget/material/progressbar.go +++ b/widget/material/progressbar.go @@ -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), + } +}