Authentication-Results: mail-b.sr.ht; dkim=fail header.d=eliasnaur.com header.i=@eliasnaur.com Received: from mail-ed1-f48.google.com (mail-ed1-f48.google.com [209.85.208.48]) by mail-b.sr.ht (Postfix) with ESMTPS id B966AFF125 for <~eliasnaur/gio-patches@lists.sr.ht>; Mon, 11 Jan 2021 09:41:40 +0000 (UTC) Received: by mail-ed1-f48.google.com with SMTP id h16so18067814edt.7 for <~eliasnaur/gio-patches@lists.sr.ht>; Mon, 11 Jan 2021 01:41:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=eliasnaur.com; s=google; h=mime-version:content-transfer-encoding:date:message-id:cc:subject :from:to:references:in-reply-to; bh=eeu73GRm5wqBmnI+Oz/zpi/k0VFfcRnIqbe7gK4ZqKo=; b=G0HnSLbKFJa6kFBEybDr0JnLTBJeIsMMV+RYvFiMMEmhijQYkDiS2TJAKmr53Oqf7l Q4qALz3czZb+wC6NAPlDdWpHw4GpKwqPfIxfPF9Gv2+tayseMFf6F2HMkVU/zEh9zvOE Gkdff39Kz/3kk9b6Xn3tUhl042TIp9scja1pYaN0gFkhLwmWRPWqLMpwQIafoqtFxV/f Dhq7q7PygQlONX6XePb7wJ7/esZEpfHh6N+lgjCMFKu7WTrR1ZCJwP4De7GM21Ffv7uk 31MkzgP5ikgRA3mu4p5B4fxdyv8z0OO3yx5mFHo2OtJDVBW0fK6H985uyCZxdKnZx5/O rJHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:content-transfer-encoding:date :message-id:cc:subject:from:to:references:in-reply-to; bh=eeu73GRm5wqBmnI+Oz/zpi/k0VFfcRnIqbe7gK4ZqKo=; b=K6S+tgQfN2sYV1/Qz6ZtjqnU9CXNfoJ4maKGEUTB/iMVmw1Uq5XtsLywHSG03T7Ofh g+B2BZtCDP+lKhXxi85C6cA5ko9qkDMAGEaD9thpAhF363K5udpMeaW0W0vmSWw4B7Sh fsPeTe43KcJJak0TJrAoV4w+uHKnNjO0GCZnT8tF8pGt0DockUhb8ONHLZhXdbxR53Rp DAHotxCgxMOD81sv2NBVPpwiJAkq+BvlPFsizh4j3A+ch0rO5Vq9slkUFBvOmnVV+zQF TZiaCUCtuEMF/VN4f7hjxIpukDB9vJIlM/xjvF5rS5Nh6Lk/HSJk/ZF8qzHyobbXEWxn gxtA== X-Gm-Message-State: AOAM532nW8wkyzt1E7/yJp6/0fwtxukobgzBVSJQB+GCT6oU+7MN0lZy 3W6cxpyxsQRh1MCT+qTFr+Q45Q== X-Google-Smtp-Source: ABdhPJxIx5zXDCMyOsMLnQ1vu9Z+O4whrP9Yx980wiCeLrYj+xZa4KHO0y7wn/ZymhFq24ut0cNSag== X-Received: by 2002:a50:8b66:: with SMTP id l93mr12977553edl.384.1610358099820; Mon, 11 Jan 2021 01:41:39 -0800 (PST) Received: from localhost ([145.255.60.245]) by smtp.gmail.com with ESMTPSA id lc18sm6787390ejb.77.2021.01.11.01.41.38 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 11 Jan 2021 01:41:38 -0800 (PST) Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Mon, 11 Jan 2021 10:41:38 +0100 Message-Id: Cc: Subject: Re: [PATCH gio] widget: use correct color in Icon From: "Elias Naur" To: "~egonelbre" , <~eliasnaur/gio-patches@lists.sr.ht> References: <161035668868.18065.4407285431413627246-0@git.sr.ht> In-Reply-To: <161035668868.18065.4407285431413627246-0@git.sr.ht> On Mon Jan 11, 2021 at 10:04 AM CET, ~egonelbre wrote: > From: Egon Elbre > > iconvg seems to expect a linear premultiplied color. > Thanks. Is it easy to add a test that triggers https://github.com/golang/go/issues/39526 without your change? > Signed-off-by: Egon Elbre > --- > internal/f32color/rgba.go | 34 ++++++++++++++++++++++++++++++++ > internal/f32color/rgba_test.go | 36 ++++++++++++++++++++++++++++++++++ > widget/icon.go | 2 +- > 3 files changed, 71 insertions(+), 1 deletion(-) > create mode 100644 internal/f32color/rgba_test.go > > diff --git a/internal/f32color/rgba.go b/internal/f32color/rgba.go > index d1b7564..4fc2742 100644 > --- a/internal/f32color/rgba.go > +++ b/internal/f32color/rgba.go > @@ -77,6 +77,40 @@ func NRGBAToRGBA(col color.NRGBA) color.RGBA { > } > } > =20 > +// NRGBAToLinearRGBA converts from non-premultiplied sRGB color to > premultiplied linear RGBA color. > +// > +// Each component in the result is `c * alpha`, where `c` is the linear > color. > +func NRGBAToLinearRGBA(col color.NRGBA) color.RGBA { > + if col.A =3D=3D 0xFF { > + return color.RGBA(col) > + } > + c :=3D LinearFromSRGB(col) > + return color.RGBA{ > + R: uint8(c.R*255 + .5), > + G: uint8(c.G*255 + .5), > + B: uint8(c.B*255 + .5), > + A: col.A, > + } > +} > + > +// NRGBAToRGBA_PostAlpha converts from non-premultiplied sRGB color to > premultiplied sRGB color. > +// > +// Each component in the result is `sRGBToLinear(c) * alpha`, where `c` > +// is the linear color. > +func NRGBAToRGBA_PostAlpha(col color.NRGBA) color.RGBA { > + if col.A =3D=3D 0xFF { > + return color.RGBA(col) > + } else if col.A =3D=3D 0x00 { > + return color.RGBA{} > + } > + return color.RGBA{ > + R: uint8(uint32(col.R) * uint32(col.A) / 0xFF), > + G: uint8(uint32(col.G) * uint32(col.A) / 0xFF), > + B: uint8(uint32(col.B) * uint32(col.A) / 0xFF), > + A: col.A, > + } > +} > + > // RGBAToNRGBA converts from premultiplied sRGB color to > non-premultiplied sRGB color. > func RGBAToNRGBA(col color.RGBA) color.NRGBA { > if col.A =3D=3D 0xFF { > diff --git a/internal/f32color/rgba_test.go > b/internal/f32color/rgba_test.go > new file mode 100644 > index 0000000..eba1262 > --- /dev/null > +++ b/internal/f32color/rgba_test.go > @@ -0,0 +1,36 @@ > +package f32color > + > +import ( > + "image/color" > + "testing" > +) > + > +func TestNRGBAToRGBA_PostAlpha_Boundary(t *testing.T) { > + for col :=3D 0; col <=3D 0xFF; col++ { > + for alpha :=3D 0; alpha <=3D 0xFF; alpha++ { > + in :=3D color.NRGBA{R: uint8(col), A: uint8(alpha)} > + premul :=3D NRGBAToRGBA_PostAlpha(in) > + if premul.A !=3D uint8(alpha) { > + t.Errorf("%v: got %v expected %v", in, premul.A, alpha) > + } > + if premul.R > premul.A { > + t.Errorf("%v: R=3D%v > A=3D%v", in, premul.R, premul.A) > + } > + } > + } > +} > + > +func TestNRGBAToLinearRGBA_Boundary(t *testing.T) { > + for col :=3D 0; col <=3D 0xFF; col++ { > + for alpha :=3D 0; alpha <=3D 0xFF; alpha++ { > + in :=3D color.NRGBA{R: uint8(col), A: uint8(alpha)} > + premul :=3D NRGBAToLinearRGBA(in) > + if premul.A !=3D uint8(alpha) { > + t.Errorf("%v: got %v expected %v", in, premul.A, alpha) > + } > + if premul.R > premul.A { > + t.Errorf("%v: R=3D%v > A=3D%v", in, premul.R, premul.A) > + } > + } > + } > +} > diff --git a/widget/icon.go b/widget/icon.go > index f6f88d3..4e6350d 100644 > --- a/widget/icon.go > +++ b/widget/icon.go > @@ -51,7 +51,7 @@ func (ic *Icon) image(sz int) paint.ImageOp { > img :=3D image.NewRGBA(image.Rectangle{Max: image.Point{X: sz, Y: > int(float32(sz) * dy / dx)}}) > var ico iconvg.Rasterizer > ico.SetDstImage(img, img.Bounds(), draw.Src) > - m.Palette[0] =3D f32color.NRGBAToRGBA(ic.Color) > + m.Palette[0] =3D f32color.NRGBAToLinearRGBA(ic.Color) > iconvg.Decode(&ico, ic.src, &iconvg.DecodeOptions{ > Palette: &m.Palette, > }) > -- > 2.26.2