8000 Enable creation of custom linear colormaps in layer controls by lukasz-migas · Pull Request #7600 · napari/napari · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Enable creation of custom linear colormaps in layer controls #7600

8000
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 11 commits into from
Feb 17, 2025

Conversation

lukasz-migas
Copy link
Contributor
@lukasz-migas lukasz-migas commented Feb 12, 2025
  • replace QLabel with QPushButton in the image controls

Description

This PR replaces the 'colormap' label (which displays the current colormap) with a push button (which also displays the colormap as before).
When the button is pressed, it will ask the user to select a color using color picker.
If a color was selected, then it creates a new colormap, otherwise nothing happens.

REC-20250212145409.mp4

- replace QLabel with QPushButton in the image controls
@github-actions github-actions bot added the qt Relates to qt label Feb 12, 2025
@psobolewskiPhD
Copy link
Member

At a glance, pretty cool! Similar to thoughts I had here:
#7555

@Czaki
Copy link
Collaborator
Czaki commented Feb 12, 2025

In the past such thing was not accepted.
You may see my issue #1786

cc @jni

@lukasz-migas
Copy link
Contributor Author

Thanks for the issue links! Good to know!

@psobolewskiPhD
Copy link
Member

@Czaki Your issue relates to labels, while I think this is more similar to the feature of passing hex codes to generate colormaps that was added more recently (0.4.19?).

@Czaki
Copy link
Collaborator
Czaki commented Feb 12, 2025

Whops. I mean to link #1779. That is bigger than this because it also suggests adding of save between sessions.

Copy link
codecov bot commented Feb 12, 2025

Codecov Report

Attention: Patch coverage is 89.87342% with 8 lines in your changes missing coverage. Please review.

Project coverage is 92.84%. Comparing base (c13b8d0) to head (a21429f).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
...apari/_qt/layer_controls/qt_image_controls_base.py 50.00% 5 Missing ⚠️
napari/_qt/_tests/test_qt_utils.py 95.00% 2 Missing ⚠️
...layer_controls/_tests/test_qt_image_base_layer_.py 88.88% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7600      +/-   ##
==========================================
- Coverage   92.87%   92.84%   -0.04%     
==========================================
  Files         629      629              
  Lines       58900    58973      +73     
==========================================
+ Hits        54705    54752      +47     
- Misses       4195     4221      +26     

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

@jni
Copy link
Member
jni commented Feb 13, 2025

The more important bigger scope from #1779 is the proposal of a custom segmented colormap creation, which I still oppose. However, reading through the comments, we were quite positive about linear colormaps, which this PR implements. And quite minimally, too! 🥳

The only thing I would add to this PR is naming the colormap from the hex value of the picked colour, see e.g. #1779 (comment).

@jni
Copy link
Member
jni commented Feb 13, 2025

Oh and btw, long time no see @lukasz-migas!!! 👋😊

Copy link
Collaborator
@Czaki Czaki left a comment

Choose a reason for hiding this comment

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

I would like to see some test here.

@github-actions github-actions bot added the tests Something related to our tests label Feb 13, 2025
@TimMonko
Copy link
Contributor
TimMonko commented Feb 13, 2025

This is cool! I have no comment on if it's appropriate for napari but I like it. A few things:
My GUI looks completely different (I'm on Windows), have you changed it since the video? I like it this way, personally.
image

  1. Pick Screen Color works, even for things outside the napari interface (cool!), but it does result in losing focus on napari. Is there a way to keep napari in focus after clicking?
  2. I like that there is HSV and RGB setters. Could you add it so that if a user inputs a Hex Code (likely making HTML: a line edit) it would also work to set the color?

Also wondering how this would interact with #7555 where using QColormapComboBox opens up some possibilities

@lukasz-migas
Copy link
Contributor Author

I made that recording on MacOS and the QColorDialog looks quite different there (I think it also looks different on Linux but can't confirm this).

@lukasz-migas
Copy link
Contributor Author

Perhaps it would make sense to use the same color window as it's used for defining face/border/edge colors in points/shapes.

It would be more consistent across platforms too.
Thoughts?

image

@psobolewskiPhD
Copy link
Member
psobolewskiPhD commented Feb 13, 2025

While I personally prefer to have native widgets, I think it's probably better to have the same ones across the app.
So for now, for this PR it's probably best to have DontUseNativeDialog.

Also wondering how this would interact with #7555 where using QColormapComboBox opens up some possibilities

QColormapComboBox lets you (optionally) access the full library of colormaps. This PR lets you use an arbitrary single color. So I think they can be complementary?
For my needs there are sufficient single-color options, but I'd like more of the other scientific visualization colormaps, so #7555 still relevant.

For reference, here is the cmap catalog of colormaps:
https://cmap-docs.readthedocs.io/en/stable/catalog/

@psobolewskiPhD
Copy link
Member
psobolewskiPhD commented Feb 13, 2025

@TimMonko

Could you add it so that if a user inputs a Hex Code (likely making HTML: a line edit) it would also work to set the color?

This already works for me?

@lukasz-migas
Was just about to suggest that! Do we need the alpha? I don't think it's used -- the layer opacity would control that?
I think all existing color maps use 1 for alpha?

Edit: I don't see any effect of changing alpha (unlike for Points for example), so lets remove that to prevent confusion.

8000

@TimMonko
Copy link
Contributor

This already works for me?

It at least works for me now! I think this morning I may have been just trying to put it in without '#' first, but cleverly(?) it prevents any input not starting with '#'

QColormapComboBox lets you (optionally) access the full library of colormaps. This PR lets you use an arbitrary single color. So I think they can be complementary?

What I was thinking about was the editable width of the cmap in the combobox display. I think it would be awkard to try to use QColormapComboBox AND this PR, since you'd end up with two cmaps, no? Maybe it would be possible to disable the QColormapComboBox cmap visualization, except when in the dropdown.

@jni
Copy link
Member
jni commented Feb 14, 2025

While I personally prefer to have native widgets, I think it's probably better to have the same ones across the app.

I disagree here — let's use the native widgets. It's not just a preference, it also works better: I can't use "pick screen color" with the non-native widget: it only allows me to pick from the widget window, not from the full desktop. I've granted screen permission to the app — if I go to d6764cc, I can pick from the screen no problem.

@psobolewskiPhD
Copy link
Member

@jni I too prefer the native ones! What about the Points/Shapes? my thought was we do a followup to switch to native widgets everywhere?

@jni
Copy link
Member
jni commented Feb 15, 2025

my thought was we do a followup to switch to native widgets everywhere?

yes, let's do this in follow-ups, not here.

@lukasz-migas could you revert 8f01d08? Then hopefully the tests will be green and we can merge!

@jni jni added this to the 0.6.0 milestone Feb 15, 2025
@jni jni added the feature New feature or request label Feb 15, 2025
This reverts commit 8f01d08.
@lukasz-migas
Copy link
Contributor Author < A93C /div>
lukasz-migas commented Feb 16, 2025

@jni @psobolewskiPhD Done, thanks for the feedback.
I personally prefer the native dialog (at least on MacOS), so would be happy to do a follow-up PR changing the color editor for Shapes/Point layers.
It currently uses a custom 'QtPopup' window, which could easily be replaced with a default color picker, with a few minor changes.

@melonora melonora added the ready to merge Last chance for comments! Will be merged in ~24h label Feb 17, 2025
Copy link
Collaborator
@willingc willingc left a comment

Choose a reason for hiding this comment

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

Hi @lukasz-migas 👋🏼 To be a bit more explicit and add context for future devs, I've suggested some docstring additions. Thanks!

@@ -158,6 +158,15 @@ def __init__(self, layer: Image) -> None:
# widgets.
self.layout().addRow(self.button_grid)

def _on_make_colormap(self):
"""Make new colormap."""
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
"""Make new colormap."""
"""Make new colormap when receiving this event."""

from napari.utils.translations import trans

QBYTE_FLAG = '!QBYTE_'
RICH_TEXT_PATTERN = re.compile('<[^\n]+>')


class ColorMode(StringEnum):
"""Looping mode for animating an axis.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
"""Looping mode for animating an axis.
"""Represents a color mode, useful for looping mode when animating an axis.

color: str | np.ndarray | QColor | None = None,
mode: ColorMode = ColorMode.HEX,
) -> np.ndarray | None:
"""Get color."""
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
"""Get color."""
"""Get color. Returns a new color when a color and color mode are passed."""

@lukasz-migas
Copy link
Contributor Author

Thanks @willingc! I tend to forget to do docstrings (and then forget what the function does...)
I made a couple of minor changes in response to your suggestions.

Copy link
Collaborator
@willingc willingc left a comment

Choose a reason for hiding this comment

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

Looks great @lukasz-migas. Thanks for adding context 😄

@jni jni changed the title Enable creation of custom colormaps in layer controls Enable creation of custom linear colormaps in layer controls Feb 17, 2025
@jni jni merged commit 5ad360a into napari:main Feb 17, 2025
37 checks passed
@github-actions github-actions bot removed the ready to merge Last chance for comments! Will be merged in ~24h label Feb 17, 2025
@jni jni added the highlight PR that should be mentioned in next release notes label Feb 17, 2025
jni pushed a commit that referenced this pull request Feb 18, 2025
This fixes a minor layout issue for the image/surface layer controls
(which use. the colormapComboBox.

I noticed that if the width of the layer controls is changed, the
'colormap' combobox doesn't get resized (because there was a stretch
after the control).

I should have noticed in #7600 but clearly missed it.

before

![image](https://github.com/user-attachments/assets/a5644564-274b-4ba6-b522-c2639a22fed4)

after

![image](https://github.com/user-attachments/assets/7ec326c8-3441-4be2-9dbb-3c4e293fef78)

# References and relevant issues

# Description
@imagesc-bot
Copy link

This pull request has been mentioned on Image.sc Forum. There might be relevant details there:

https://forum.image.sc/t/napari-0-6-0-released/112147/1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request highlight PR that should be mentioned in next release notes qt Relates to qt tests Something related to our tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants
0