From b2bebbdc8c4c366d9a861cd665bafd6c41ee53a5 Mon Sep 17 00:00:00 2001 From: Laurent Guerard Date: Thu, 25 Jun 2026 17:51:33 +0200 Subject: [PATCH] =?UTF-8?q?fix(napari):=20=F0=9F=90=9B=20wrap=20glasbey=20?= =?UTF-8?q?palette=20in=20CyclicLabelColormap?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Passing the raw glasbey palette list to add_labels made napari map large label IDs past the end of the palette, so every cell rendered one flat colour (green). Wrap the palette in a CyclicLabelColormap so each label value cycles through a distinct colour. Co-Authored-By: Claude Opus 4.8 --- src/patchworks/plugins/napari.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/patchworks/plugins/napari.py b/src/patchworks/plugins/napari.py index f177034..e60016b 100644 --- a/src/patchworks/plugins/napari.py +++ b/src/patchworks/plugins/napari.py @@ -260,11 +260,26 @@ def view_in_napari( if glasbey: try: import glasbey as _glasbey - - # bias lighter so colours read on napari's dark canvas - label_kwargs["colormap"] = _glasbey.create_palette( - 256, lightness_bounds=(40, 100) + import numpy as np + from napari.utils.colormaps import CyclicLabelColormap + + # glasbey palette (biased lighter so colours read on the dark + # canvas), wrapped in a CyclicLabelColormap so each label value + # cycles through a distinct colour. Passing the raw palette list + # makes napari map large label IDs past the end -> one flat colour. + palette = _glasbey.create_palette(256, lightness_bounds=(40, 100)) + colors = np.array( + [ + [ + int(h[1:3], 16) / 255, + int(h[3:5], 16) / 255, + int(h[5:7], 16) / 255, + 1.0, + ] + for h in palette + ] ) + label_kwargs["colormap"] = CyclicLabelColormap(colors=colors) except ImportError: logger.warning( "glasbey not installed; using napari's default label colours "