slint-ui / slint

Slint is a declarative GUI toolkit to build native user interfaces for Rust, C++, or JavaScript apps.
https://slint.dev
Other
16.96k stars 568 forks source link

Linear gradients may be rendered differently between femtovg and Qt #313

Open tronical opened 3 years ago

tronical commented 3 years ago

The following minimal test-case produces different rendering with femtovg and Qt:

T := Window {
  preferred-width: 640px;
  preferred-height: 480px;
  Rectangle {
    background: @linear-gradient(0, red, yellow, green, transparent);
  }
}

Ideally they are rendered the same.

Screenshots to illustrate:

femtovg Qt
femtovg qt
nyanpasu64 commented 3 years ago

My guess as to the issue is that you're not using premultiplied alpha for blending, so halfway between rgba(0, 0, 0, 0) and rgba(0, 255, 0, 1) is blended as "draw halfway between black and green, with alpha halfway between 0 and 100%" rather than "draw green with 50% alpha".

Another possibility for improving gradients is using gamma-corrected blending for gradients. This produces more consistent brightnesses (halfway through a gradient is the same brightness as a checkerboard, blending from red to green isn't darker in the middle). The downside is that it makes black-white linear gradients lighter in the middle than expected. Unfortunately this shouldn't be implemented, because browsers blend in sRGB gamma space rather than linear.

tronical commented 3 years ago

Thank you @nyanpasu64!! What you’re saying about the lack of premultiplied alpha makes sense just looking at the fragment shader in femtovg. I’ll take a look on Monday.

ogoffart commented 1 year ago

We have more renderer now.

The Qt and software renderer agree, and Skia and Femtovg also agree

image

But who is right?

ogoffart commented 1 year ago

I think Qt and software is right. Skia and FemtoVG show some "gray" color which seems coming from the pixel value of the transparent pixel. (which is black with 0 alpha)

tronical commented 1 year ago

Yes, Qt and software renderer are right IMO - they're interpolating pre-multiplied colors and there's no grey.

tronical commented 1 year ago

Related femtovg issue: https://github.com/femtovg/femtovg/issues/16