Skip to content
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

fix: Fix selection of custom label colors for napari 0.5.0 #1138

Merged
merged 3 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions package/PartSeg/common_gui/napari_image_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,9 @@ def get_roi_view_parameters(self, image_info: ImageInfo) -> ColorInfo:
return res

def set_roi_colormap(self, image_info) -> None:
if _napari_ge_5:
image_info.roi.colormap = self.get_roi_view_parameters(image_info)
return
if _napari_ge_4_13:
image_info.roi.color = self.get_roi_view_parameters(image_info)
return
Expand Down Expand Up @@ -583,7 +586,10 @@ def set_mask(self, mask: Optional[np.ndarray] = None, image: Optional[Image] = N
else:
image_info.mask.data = mask_marker
image_info.mask.metadata["valid"] = True
image_info.mask.color = self.mask_color()
if _napari_ge_5:
image_info.mask.colormap = self.mask_color()
else:
image_info.mask.color = self.mask_color()
image_info.mask.opacity = self.mask_opacity()
image_info.mask.visible = self.mask_chk.isChecked()
self._toggle_mask_chk_visibility()
Expand All @@ -602,7 +608,10 @@ def update_mask_parameters(self):
for image_info in self.image_info.values():
if image_info.mask is not None:
image_info.mask.opacity = opacity
image_info.mask.color = colormap
if _napari_ge_5:
image_info.mask.colormap = colormap
else:
image_info.mask.color = colormap

def set_image(self, image: Optional[Image] = None):
self.image_info = {}
Expand Down
13 changes: 11 additions & 2 deletions package/tests/test_PartSeg/test_napari_image_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@

if NAPARI_GE_5_0:
EXPECTED_RANGE = (0, 0, 1)

def get_color_dict(layer):
return layer.colormap.color_dict

else:
EXPECTED_RANGE = (0, 1, 1)

def get_color_dict(layer):
return layer.color


def test_image_info():
image_info = ImageInfo(Image(np.zeros((10, 10)), image_spacing=(1, 1), axes_order="XY"), [])
Expand Down Expand Up @@ -159,9 +166,11 @@ def test_mask_rendering(self, base_settings, image_view, qtbot, tmp_path):
base_settings.set_in_profile("mask_presentation_opacity", 0.5)
assert image_view.image_info[str(tmp_path / "test2.tiff")].mask.opacity == 0.5
base_settings.set_in_profile("mask_presentation_color", (255, 0, 0))
assert np.all(image_view.image_info[str(tmp_path / "test2.tiff")].mask.color[1] == (1, 0, 0, 1))
assert np.all(get_color_dict(image_view.image_info[str(tmp_path / "test2.tiff")].mask)[1] == (1, 0, 0, 1))
base_settings.set_in_profile("mask_presentation_color", (128, 0, 0))
assert np.allclose(image_view.image_info[str(tmp_path / "test2.tiff")].mask.color[1], (128 / 255, 0, 0, 1))
assert np.allclose(
get_color_dict(image_view.image_info[str(tmp_path / "test2.tiff")].mask)[1], (128 / 255, 0, 0, 1)
)

assert not image_view.image_info[str(tmp_path / "test2.tiff")].mask.visible
with qtbot.waitSignal(image_view.mask_chk.stateChanged):
Expand Down