Skip to content

Commit 4ab9619

Browse files
Set interpolation when exporting to pygfx (#88)
* return TextureMap * update type hints on Colormap.to_pygfx * add interpolation check to test
1 parent 0bc7a1d commit 4ab9619

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

src/cmap/_colormap.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -703,8 +703,14 @@ def to_vispy(self) -> vispy.color.Colormap:
703703
"""Return a vispy colormap."""
704704
return _external.to_vispy(self)
705705

706-
def to_pygfx(self, N: int = 256, *, as_view: bool | None = None) -> pygfx.Texture:
707-
"""Return a pygfx Texture."""
706+
def to_pygfx(
707+
self, N: int = 256, *, as_view: bool | None = None
708+
) -> pygfx.TextureMap:
709+
"""Return a pygfx TextureMap.
710+
711+
Note that it is recommended to use at least 256 colors for the LUT.
712+
https://github.com/pygfx/pygfx/pull/1025#issuecomment-2706170445
713+
"""
708714
if as_view is not None:
709715
warnings.warn(
710716
"as_view argument is deprecated and does nothing",

src/cmap/_external.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,19 @@ def to_vispy(cm: Colormap) -> VispyColormap:
6262
return Colormap(colors=cm.color_stops.color_array, controls=cm.color_stops.stops)
6363

6464

65-
def to_pygfx(cm: Colormap, N: int = 256) -> pygfx.Texture:
65+
def to_pygfx(cm: Colormap, N: int = 256) -> pygfx.TextureMap:
6666
"""Return a pygfx Texture."""
6767
import pygfx
6868

6969
# TODO: check whether pygfx has it's own stop-aware interpolation,
7070
# and if so, use that instead of .lut()
7171
# (get_view has a filter argument... but I don't know whether it will take
7272
# care of the stops)
73-
return pygfx.Texture(cm.lut(N).astype(np.float32), dim=1)
73+
pygfx_texture = pygfx.Texture(cm.lut(N).astype(np.float32), dim=1)
74+
75+
return pygfx.TextureMap(
76+
texture=pygfx_texture, filter=cm.interpolation, wrap="clamp"
77+
)
7478

7579

7680
def to_plotly(cm: Colormap) -> list[list[float | str]]:

tests/test_third_party.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,17 @@ def test_pygfx(qapp: "QApplication") -> None:
111111
# camera.position.x = IMG.shape[1] / 2
112112
# camera.scale.y = -1
113113

114+
# check that the colormap interpolation is set
115+
color_map = CMAP.to_pygfx()
116+
assert color_map.mag_filter == CMAP.interpolation
117+
assert color_map.min_filter == CMAP.interpolation
118+
assert color_map.mipmap_filter == CMAP.interpolation
119+
114120
scene = gfx.Scene()
115121
scene.add(
116122
gfx.Image(
117123
gfx.Geometry(grid=gfx.Texture(IMG, dim=2)),
118-
gfx.ImageBasicMaterial(clim=(0, IMG.max()), map=CMAP.to_pygfx()),
124+
gfx.ImageBasicMaterial(clim=(0, IMG.max()), map=color_map),
119125
)
120126
)
121127

0 commit comments

Comments
 (0)