question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add alpha blending

See original GitHub issue

We should have a method for alpha blending. This is required to be able to do contrast calculations properly (see https://github.com/w3c/csswg-drafts/issues/7358 ).

Things to decide:

  • Naming: Perhaps overlay() or over()? Or overlayOn() or just on() which is more clear as a Color method, but not so much in the procedural API (See overlayOn(color1, color2) vs color1.overlayOn(color2)). Whatever we come up with should make sense in both APIs, so I’m leaning toward over() which is also the operator name.
  • Is there any reason for it to take more than 2 arguments? My understanding is that the operator is associative.

The math seems pretty straightforward. @svgeesus are there any considerations when applying this to other color spaces? What color space do we use for the result? I guess the color space of the color being overlaid?

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
facelessusercommented, Oct 23, 2022

What missteps did you have?

  • I forgot to undo the premultiplication after the blending was done. This broke the associative nature. That was just a dumb mistake on my part.
  • It also seems that some of the operator algorithms can exceed 0 and 1 for αo (Lighter for instance). I initially didn’t clamp them, and instead applied them, and then the final color would have the alpha clamped. But I realized that to mimic browsers, I needed to clamp αo then apply it.

EDIT: Just to clarify, clamping αo is specifically related to premultiplication as well, and in my case, I was using it to undo premultiplication on each channel without first clamping it to a realistic 0 - 1 range. Basically, all my issues were related to undoing premultiplication.

1reaction
facelessusercommented, Oct 22, 2022

Let’s focus on alpha blending for now please.

As someone that has already gone down this road, if you add support for source-over compositing and normal alpha blending, you have everything you need to do the others, you can just swap out different compositing and blend methods. So the key is just to make sure the compositing step and the blend step is generic. I think that is the main point that is being conveyed.

Though I had some missteps in the beginning 😅, I can pretty much mimic what browsers are doing now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Compositing Images with Alpha Blending - Apple Developer
Combine two images by using alpha blending to create a single output.
Read more >
Transparency
1( α α. Basic Alpha Blending. Solid back color. Transparent front color with opacity α ... But can't insert surface between them later...
Read more >
Blending - LearnOpenGL
An alpha value of 0.5 tells us the object's color consist of 50% of its own color and 50% of the colors behind...
Read more >
Alpha Blending Using OpenCV (C++ / Python) - LearnOpenCV
In this tutorial – Alpha blending using OpenCV, we will learn how to alpha blend two images and overlay a transparent PNG image...
Read more >
Alpha compositing - Wikipedia
In computer graphics, alpha compositing or alpha blending is the process of combining one image with a background to create the appearance of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found