Skip to content

Commit 17841af

Browse files
committed
Colormaps: fixed some critical bugs
Image colormap parameters (dataset) + Case issue / global variable issue
1 parent e69a56c commit 17841af

4 files changed

Lines changed: 36 additions & 21 deletions

File tree

plotpy/mathutils/colormaps.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
# from guidata.dataset.datatypes import NoDefault
2424
FULLRANGE = QwtInterval(0.0, 1.0)
25-
DEFAULT = EditableColormap(name="default")
25+
DEFAULT = EditableColormap(name="jet")
2626

2727

2828
def load_raw_colormaps_from_json(
@@ -122,9 +122,23 @@ def get_cmap(cmap_name: str) -> EditableColormap:
122122
A CustomQwtLinearColormap instance corresponding to the given name, if no
123123
colormap is found, returns the DEFAULT colormap.
124124
"""
125+
global ALL_COLORMAPS
125126
return ALL_COLORMAPS.get(cmap_name.lower(), DEFAULT)
126127

127128

129+
def add_cmap(cmap: EditableColormap) -> None:
130+
"""Adds the given colormap to both ALL_COLORMAPS and CUSTOM_COLORMAPS global
131+
variables.
132+
133+
Args:
134+
cmap: colormap to add
135+
"""
136+
global ALL_COLORMAPS, CUSTOM_COLORMAPS
137+
ALL_COLORMAPS[cmap.name.lower()] = cmap
138+
CUSTOM_COLORMAPS[cmap.name.lower()] = cmap
139+
save_colormaps(CUSTOM_COLORMAPS_PATH, CUSTOM_COLORMAPS)
140+
141+
128142
def get_cmap_path(config_path: str):
129143
"""Takes a file path (i.e. from the CONF global variable) and tries to find it in
130144
this order:

plotpy/styles/image.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# -*- coding: utf-8 -*-
2+
23
from __future__ import annotations
34

4-
from typing import Callable
5+
from typing import TYPE_CHECKING, Any, Callable
56

67
import numpy as np
78
from guidata.dataset import (
@@ -25,11 +26,17 @@
2526
from plotpy.mathutils.colormaps import ALL_COLORMAPS, build_icon_from_cmap_name
2627
from plotpy.styles.base import ItemParameters
2728

29+
if TYPE_CHECKING:
30+
from guidata.dataset import DataSet
31+
2832

29-
def _create_choices() -> list[tuple[str, str, Callable[[str], QG.QIcon]]]:
33+
def _create_choices(
34+
dataset: DataSet, item: ImageChoiceItem, value: Any
35+
) -> list[tuple[str, str, Callable[[str], QG.QIcon]]]:
36+
"""Create the list of choices for the colormap item."""
3037
choices: list[tuple[str, str, Callable[[str], QG.QIcon]]] = []
31-
for cmap_name in ALL_COLORMAPS:
32-
choices.append((cmap_name, cmap_name, build_icon_from_cmap_name))
38+
for cmap in ALL_COLORMAPS.values():
39+
choices.append((cmap.name, cmap.name, build_icon_from_cmap_name))
3340
return choices
3441

3542

@@ -52,9 +59,9 @@ class BaseImageParam(DataSet):
5259
_("Global alpha"), default=1.0, min=0, max=1, help=_("Global alpha value")
5360
)
5461
_hide_colormap = False
55-
colormap = ImageChoiceItem(
56-
_("Colormap"), _create_choices(), default="jet"
57-
).set_prop("display", hide=GetAttrProp("_hide_colormap"))
62+
colormap = ImageChoiceItem(_("Colormap"), _create_choices, default="jet").set_prop(
63+
"display", hide=GetAttrProp("_hide_colormap")
64+
)
5865

5966
interpolation = ChoiceItem(
6067
_("Interpolation"),
@@ -134,9 +141,9 @@ class QuadGridParam(DataSet):
134141
_("Global alpha"), default=1.0, min=0, max=1, help=_("Global alpha value")
135142
)
136143
_hide_colormap = False
137-
colormap = ImageChoiceItem(
138-
_("Colormap"), _create_choices(), default="jet"
139-
).set_prop("display", hide=GetAttrProp("_hide_colormap"))
144+
colormap = ImageChoiceItem(_("Colormap"), _create_choices, default="jet").set_prop(
145+
"display", hide=GetAttrProp("_hide_colormap")
146+
)
140147

141148
interpolation = ChoiceItem(
142149
_("Interpolation"),

plotpy/tools/image.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@
3434
)
3535
from plotpy.mathutils.colormaps import (
3636
ALL_COLORMAPS,
37-
CUSTOM_COLORMAPS,
38-
DEFAULT,
39-
DEFAULT_COLORMAPS,
40-
build_icon_from_cmap,
4137
build_icon_from_cmap_name,
4238
get_cmap,
4339
)
@@ -454,7 +450,7 @@ def __init__(
454450
tip=_("Select colormap for active image"),
455451
toolbar_id=toolbar_id,
456452
)
457-
self._active_colormap: EditableColormap = ALL_COLORMAPS.get("jet", DEFAULT)
453+
self._active_colormap: EditableColormap = ALL_COLORMAPS["jet"]
458454
self.default_icon = build_icon_from_cmap_name(self._active_colormap.name)
459455
if self.action is not None:
460456
self.action.setEnabled(False)
@@ -534,7 +530,7 @@ def update_status(self, plot: BasePlot) -> None:
534530
self._active_colormap = get_cmap(cmap_name)
535531
else:
536532
self.action.setEnabled(False)
537-
self._active_colormap = ALL_COLORMAPS.get("jet", DEFAULT)
533+
self._active_colormap = ALL_COLORMAPS["jet"]
538534
self.action.setIcon(icon)
539535

540536

plotpy/widgets/colormap/manager.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
2222
from plotpy.mathutils.colormaps import (
2323
ALL_COLORMAPS,
2424
CUSTOM_COLORMAPS,
25-
CUSTOM_COLORMAPS_PATH,
2625
DEFAULT_COLORMAPS,
26+
add_cmap,
2727
build_icon_from_cmap,
28-
save_colormaps,
2928
)
3029
from plotpy.widgets.colormap.editor import ColorMapEditor
3130
from plotpy.widgets.colormap.widget import EditableColormap
@@ -297,8 +296,7 @@ def save_colormap(self, cmap: EditableColormap | None = None) -> bool:
297296
new_name = CUSTOM_COLORMAPS[new_name.lower()].name
298297

299298
cmap.name = new_name
300-
CUSTOM_COLORMAPS[new_name] = ALL_COLORMAPS[new_name] = cmap
301-
save_colormaps(CUSTOM_COLORMAPS_PATH, CUSTOM_COLORMAPS)
299+
add_cmap(cmap)
302300

303301
icon = build_icon_from_cmap(cmap)
304302
if is_existing_custom_cmap:

0 commit comments

Comments
 (0)