From 103d30d0bf2656539a3d4577e25a7d6d8e75d31f Mon Sep 17 00:00:00 2001 From: Megan Parsons Date: Sat, 11 Nov 2023 17:24:22 -0500 Subject: [PATCH] added colorbar --- fastplotlib/graphics/legends/__init__.py | 0 fastplotlib/graphics/legends/colorbar.py | 51 ++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 fastplotlib/graphics/legends/__init__.py create mode 100644 fastplotlib/graphics/legends/colorbar.py diff --git a/fastplotlib/graphics/legends/__init__.py b/fastplotlib/graphics/legends/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/fastplotlib/graphics/legends/colorbar.py b/fastplotlib/graphics/legends/colorbar.py new file mode 100644 index 000000000..4739bc8f0 --- /dev/null +++ b/fastplotlib/graphics/legends/colorbar.py @@ -0,0 +1,51 @@ +from .._base import Graphic +from .. import TextGraphic +import pygfx + +class CmapLegend(Graphic): + def __init__( + self, + cmap_name: str, + vmin: float, + vmax: float, + width: float, + height: float, + *args, + **kwargs, + ): + super().__init__(*args, **kwargs) + + wo = pygfx.Group() + + cmap = get_cmap(cmap_name) + + tex = pygfx.Texture(np.repeat([cmap], 2, axis=0), dim=2) + + geometry = pygfx.plane_geometry(width, height, 1, 1) + material = pygfx.MeshBasicMaterial(map=tex) + self._rectangle = pygfx.Mesh(geometry, material) + + self._text_vmax = TextGraphic( + text=str(vmax), + size=16, + position=(-width/2, 0), + anchor="center", + outline_color="black", + outline_thickness=1, + ) + + self._text_vmax.position_x = width/2 + 5 + + self._text_vmin = TextGraphic( + text=str(vmin), + size=16, + position=(width, 0), + anchor="center", + outline_color="black", + outline_thickness=1, + ) + + self._text_vmin.position_x = -width/2 - 5 + + wo.add(self._rectangle, self._text_vmin.world_object, self._text_vmax.world_object) + self._set_world_object(wo) \ No newline at end of file