diff --git a/bec_widgets/utils/colors.py b/bec_widgets/utils/colors.py index 94fc0676..47298021 100644 --- a/bec_widgets/utils/colors.py +++ b/bec_widgets/utils/colors.py @@ -1,11 +1,13 @@ from __future__ import annotations +from functools import lru_cache import re from typing import TYPE_CHECKING, Literal import bec_qthemes import numpy as np import pyqtgraph as pg +from bec_lib import bec_logger from bec_qthemes._os_appearance.listener import OSThemeSwitchListener from pydantic_core import PydanticCustomError from qtpy.QtGui import QColor @@ -15,6 +17,9 @@ if TYPE_CHECKING: # pragma: no cover from bec_qthemes._main import AccentColors +logger = bec_logger.logger + + def get_theme_name(): if QApplication.instance() is None or not hasattr(QApplication.instance(), "theme"): return "dark" @@ -194,6 +199,11 @@ class Colors: if not name: raise ValueError("Empty colormap name") + return Colors._get_colormap_cached(name) + + @staticmethod + @lru_cache(maxsize=256) + def _get_colormap_cached(name: str) -> pg.ColorMap: # 1) Registry/backends try: return pg.colormap.get(name) @@ -608,7 +618,8 @@ class Colors: normalized = Colors.canonical_colormap_name(color_map) try: Colors.get_colormap(normalized) - except Exception: + except Exception as ext: + logger.warning(f"Colormap validation error: {ext}") if return_error: available_colormaps = sorted( set(Colors.list_available_colormaps()) @@ -616,7 +627,7 @@ class Colors: ) raise PydanticCustomError( "unsupported colormap", - f"Colormap '{color_map}' not found in the current installation of pyqtgraph. Choose on the following: {available_colormaps}.", + f"Colormap '{color_map}' not found in the current installation of pyqtgraph. Choose from the following: {available_colormaps}.", {"wrong_value": color_map}, ) else: diff --git a/bec_widgets/widgets/plots/image/image_base.py b/bec_widgets/widgets/plots/image/image_base.py index 6b17d3e0..ab0435e2 100644 --- a/bec_widgets/widgets/plots/image/image_base.py +++ b/bec_widgets/widgets/plots/image/image_base.py @@ -249,6 +249,8 @@ class ImageBase(PlotBase): Base class for the Image widget. """ + MAX_TICKS_COLORBAR = 10 + sync_colorbar_with_autorange = Signal() image_updated = Signal() layer_added = Signal(str) @@ -499,14 +501,11 @@ class ImageBase(PlotBase): if self.config.color_bar != "full": return - self._color_bar.setColorMap(cmap) - gradient = getattr(self._color_bar, "gradient", None) if gradient is None: return - max_ticks = 10 - positions = np.linspace(0.0, 1.0, max_ticks) + positions = np.linspace(0.0, 1.0, self.MAX_TICKS_COLORBAR) colors = cmap.map(positions, mode="byte") colors = np.asarray(colors)