internal/f32color: add colorspace-correct function for alpha scaling

Package material's ad-hoc mulAlpha didn't take the sRGB color-space
into account, which meant that alpha-scaled colors were subtly wrong.
Introduce f32color.MulAlpha and convert all uses to it.

Thanks to René Post for finding and debugging the issue.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-09-28 09:02:38 +02:00
parent 679a34b116
commit 4bab6fcf32
7 changed files with 31 additions and 23 deletions
+3 -13
View File
@@ -7,6 +7,7 @@ import (
"image/color"
"gioui.org/f32"
"gioui.org/internal/f32color"
"gioui.org/layout"
"gioui.org/op/clip"
"gioui.org/op/paint"
@@ -58,7 +59,7 @@ func (p ProgressBarStyle) Layout(gtx layout.Context) layout.Dimensions {
return layout.Stack{Alignment: layout.W}.Layout(gtx,
layout.Stacked(func(gtx layout.Context) layout.Dimensions {
// Use a transparent equivalent of progress color.
bgCol := mulAlpha(p.Color, 150)
bgCol := f32color.MulAlpha(p.Color, 150)
return shader(progressBarWidth, bgCol)
}),
@@ -66,20 +67,9 @@ func (p ProgressBarStyle) Layout(gtx layout.Context) layout.Dimensions {
fillWidth := (progressBarWidth / 100) * float32(progress)
fillColor := p.Color
if gtx.Queue == nil {
fillColor = mulAlpha(fillColor, 200)
fillColor = f32color.MulAlpha(fillColor, 200)
}
return shader(fillWidth, fillColor)
}),
)
}
// 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),
}
}