8000 Add multiplicative blending by brisvag · Pull Request #7868 · napari/napari · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add multiplicative blending #7868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 13, 2025

Conversation

brisvag
Copy link
Contributor
@brisvag brisvag commented Apr 29, 2025

References and relevant issues

Description

Add multiplicative blending to our blending modes.

This can be useful in several scenarios, such as the example from the above discussion:

import napari
import numpy as np
v = napari.Viewer()
v.open_sample('napari', 'astronaut')

gradient = np.arange(512 * 512).reshape(512, 512)
l = v.add_image(gradient, colormap='I Orange', blending='multiplicative')

image

@brisvag brisvag requested review from melonora and a team as code owners April 29, 2025 09:01
Copy link
codecov bot commented Apr 29, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.93%. Comparing base (00026c1) to head (7ee369d).
Report is 13 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7868      +/-   ##
==========================================
+ Coverage   92.91%   92.93%   +0.02%     
==========================================
  Files         641      641              
  Lines       60615    60616       +1     
==========================================
+ Hits        56319    56336      +17     
+ Misses       4296     4280      -16     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@brisvag brisvag added feature New feature or request topic:vispy Vispy integration labels Apr 29, 2025
@brisvag brisvag added this to the 0.6.1 milestone Apr 29, 2025
Copy link
Contributor
@TimMonko TimMonko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it! Works intuitively and is a nice simple feature.

Would be great to add this as an example. I think users will have difficulty conceptualizing how it should look -- so an example would help get that across.

Your PR description is sufficient, just needs the example formatting. I can add in a follow-up PR if desired.

@TimMonko TimMonko added the ready to merge Last chance for comments! Will be merged in ~24h label May 8, 2025
@brisvag
Copy link
Contributor Author
brisvag commented May 9, 2025

I don't know, seems a bit excessive to have an example for just a blending mode? Unless we add one with several blending modes side by side? Either way, maybe let's do this in a followp PR, yeah :)

@jni
Copy link
Member
jni commented May 10, 2025

+1 to a gallery example, preferably a realistic one (e.g. some kind of probability map), +1 for new PR.

'cull_face': False,
'blend': True,
'blend_func': ('dst_color', 'zero', 'one', 'one'),
'blend_equation': 'func_add',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brisvag can you explain to me how this work? The func_add really threw me. 😂

what are the contents of the blend_func tuple?

Maybe a comment here would be interesting.

Copy link
Contributor Author
@brisvag brisvag May 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's trippy, yeah. I learned from https://www.khronos.org/opengl/wiki/Blending#Blending_Parameters

Maybe I'll just put a link, cause this requires some background.

The relevant bits are:

Blending equation:

* Orgb = srgb * Srgb + drgb * Drgb
* Oa = sa * Sa + da * Da

(O = origin, D = destination, small is parameter, capitalized is the value itself)

"For example, if the blending you want is a straight multiplication of the source and destination colors, O = S * D, then you should set the GL_FUNC_ADD function. Then set the source parameter for that equation to GL_DST_COLOR, and set the destination parameter to GL_ZERO (we don't want to do addition, so null out the additive term). This will give O = D * S + 0 * D. "

Basically, we're using the destination color as a parameter for the origin color, and ignoring the destination color itself because it's already accounted for.

@psobolewskiPhD
Copy link
Member

This PR made me realize something that applies to additive as well, that I guess I never appreciated: the blending happens after the colormap is applied. So e.g. if you use Binary blobs over another image (e.g. Brick), switching between gray and gray_r determines what gets multiplied to 0 and what is preserved.

  1. Something else that surprised me is that opacity slider doesn't do anything -- is that expected? maybe we should disable it, like minimum.

  2. I'm 💯 for an example, here's the minimum one from way back when: https://napari.org/stable/gallery/minimum_blending.html#sphx-glr-gallery-minimum-blending-py
    We can link to it from the documentation!

  3. Where in the list does it make sense to put this in the combobox? I think i'd put it next to additive?
    so translucent, additive, mult, minimum, opaque? This is sort of like inverse additive, because white in additive makes everything white, while black has no effect, while here black makes everything black and white has no effect. (btw, for images is translucent vs translucent_no_depth meaningful?)

@brisvag
Copy link
Contributor Author
brisvag commented May 12, 2025
  1. Something else that surprised me is that opacity slider doesn't do anything -- is that expected? maybe we should disable it, like minimum.

We can introduce it somehow, it currently does nothing because of the one one part I think? I'm not sure what's the expected behaviour here...

3. Where in the list does it make sense to put this in the combobox? I think i'd put it next to additive?
so translucent, additive, mult, minimum, opaque? This is sort of like inverse additive, because white in additive makes everything white, while black has no effect, while here black makes everything black and white has no effect.

Makes sense to me, no strong opinions :P

btw, for images is translucent vs translucent_no_depth meaningful?

Yes, very important in 3D: try the cells3d example and switch both layers to translucent. Especially imortant for plane rendering.

@TimMonko TimMonko merged commit b779489 into napari:main May 13, 2025
40 checks passed
@github-actions github-actions bot removed the ready to merge Last chance for comments! Will be merged in ~24h label May 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request topic:vispy Vispy integration
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0