internal/f32color: correct pre-multipled color conversion

Tweak a test color to avoid an off-by-1 rounding error after changing
the conversion formula.

Fixes gio#192

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2021-01-15 16:01:18 +01:00
parent 85c0a7d803
commit 01d5e72291
3 changed files with 23 additions and 8 deletions
+1 -1
View File
@@ -107,7 +107,7 @@ func TestFramebuffers(t *testing.T) {
fbo1 := newFBO(t, b, sz)
fbo2 := newFBO(t, b, sz)
var (
col1 = color.NRGBA{R: 0xac, G: 0xbe, B: 0xef, A: 0xde}
col1 = color.NRGBA{R: 0xac, G: 0xbd, B: 0xef, A: 0xde}
col2 = color.NRGBA{R: 0xfe, G: 0xba, B: 0xbe, A: 0xca}
)
fcol1, fcol2 := f32color.LinearFromSRGB(col1), f32color.LinearFromSRGB(col2)
+7 -7
View File
@@ -28,9 +28,9 @@ func (col RGBA) SRGB() color.NRGBA {
return color.NRGBA{}
}
return color.NRGBA{
R: uint8(linearTosRGB(col.R/col.A)*255 + .5),
G: uint8(linearTosRGB(col.G/col.A)*255 + .5),
B: uint8(linearTosRGB(col.B/col.A)*255 + .5),
R: uint8(linearTosRGB(col.R)/col.A*255 + .5),
G: uint8(linearTosRGB(col.G)/col.A*255 + .5),
B: uint8(linearTosRGB(col.B)/col.A*255 + .5),
A: uint8(col.A*255 + .5),
}
}
@@ -49,13 +49,13 @@ func (col RGBA) Opaque() RGBA {
return col
}
// LinearFromSRGB converts from SRGBA to RGBA.
// LinearFromSRGB converts from col in the sRGB colorspace to RGBA.
func LinearFromSRGB(col color.NRGBA) RGBA {
af := float32(col.A) / 0xFF
return RGBA{
R: sRGBToLinear(float32(col.R)/0xff) * af,
G: sRGBToLinear(float32(col.G)/0xff) * af,
B: sRGBToLinear(float32(col.B)/0xff) * af,
R: sRGBToLinear(float32(col.R) / 0xff * af),
G: sRGBToLinear(float32(col.G) / 0xff * af),
B: sRGBToLinear(float32(col.B) / 0xff * af),
A: af,
}
}
+15
View File
@@ -36,3 +36,18 @@ func TestNRGBAToLinearRGBA_Boundary(t *testing.T) {
}
}
}
func TestLinearToRGBARoundtrip(t *testing.T) {
for col := 0; col <= 0xFF; col++ {
for alpha := 0; alpha <= 0xFF; alpha++ {
want := color.NRGBA{R: uint8(col), A: uint8(alpha)}
if alpha == 0 {
want.R = 0
}
got := LinearFromSRGB(want).SRGB()
if want != got {
t.Errorf("got %v expected %v", got, want)
}
}
}
}