|
1 | 1 | # -*- coding: utf-8 -*- |
2 | | -"""This module provides utilities to interact with colormap. It provides functions |
3 | | -to load/save colormaps from/to json files and to build icons representing the |
4 | | -colormaps. |
5 | | -""" |
6 | 2 | # |
7 | 3 | # Licensed under the terms of the BSD 3-Clause |
8 | 4 | # (see plotpy/LICENSE for details) |
| 5 | + |
| 6 | +""" |
| 7 | +This module provides utilities to interact with colormap. It provides functions |
| 8 | +to load/save colormaps from/to json files and to build icons representing the |
| 9 | +colormaps. |
| 10 | +""" |
| 11 | + |
9 | 12 | from __future__ import annotations |
10 | 13 |
|
11 | 14 | import json |
|
21 | 24 | from plotpy.config import CONF |
22 | 25 | from plotpy.widgets.colormap.widget import EditableColormap |
23 | 26 |
|
24 | | -# from guidata.dataset.datatypes import NoDefault |
25 | 27 | FULLRANGE = QwtInterval(0.0, 1.0) |
26 | | -DEFAULT = EditableColormap(name="jet") |
27 | 28 | SQUARE_ICON_SIZE = 16 |
28 | 29 | RECT_ICON_SIZE_W, RECT_ICON_SIZE_H = 80, 16 |
29 | 30 |
|
@@ -70,6 +71,63 @@ def load_qwt_colormaps_from_json(json_path: str) -> CmapDictType: |
70 | 71 | } |
71 | 72 |
|
72 | 73 |
|
| 74 | +def get_cmap_path(config_path: str): |
| 75 | + """Takes a file path (i.e. from the CONF global variable) and tries to find it in |
| 76 | + this order: |
| 77 | + 1. in Plotpy's data directory |
| 78 | + 2. in user's plotpy configuration directory |
| 79 | + 3. anywhere else using the path as an absolute path |
| 80 | + If the file is not found, the absolute filepath returned will point to user's plotpy |
| 81 | + configuration folder. |
| 82 | +
|
| 83 | + Args: |
| 84 | + config_path: file path/name to check in the order listed above. |
| 85 | + """ |
| 86 | + try: |
| 87 | + data_config_path = os.path.join( |
| 88 | + get_module_data_path("plotpy", "data"), config_path |
| 89 | + ) |
| 90 | + if os.path.isfile(data_config_path): |
| 91 | + return data_config_path |
| 92 | + except (FileNotFoundError, PermissionError, OSError) as e: |
| 93 | + print(e) |
| 94 | + |
| 95 | + user_config_path = CONF.get_path(config_path) |
| 96 | + if os.path.isfile(user_config_path): |
| 97 | + return user_config_path |
| 98 | + |
| 99 | + if os.path.isfile(config_path): |
| 100 | + return config_path |
| 101 | + |
| 102 | + return user_config_path |
| 103 | + |
| 104 | + |
| 105 | +# Load default colormaps path from the config file |
| 106 | +DEFAULT_COLORMAPS_PATH = get_cmap_path( |
| 107 | + CONF.get( |
| 108 | + "colormaps", |
| 109 | + "colormaps/default", |
| 110 | + default="colormaps_default.json", # type: ignore |
| 111 | + ) |
| 112 | +) |
| 113 | +# Load custom colormaps path from the config file |
| 114 | +CUSTOM_COLORMAPS_PATH = get_cmap_path( |
| 115 | + CONF.get( |
| 116 | + "colormaps", "colormaps/custom", default="colormaps_custom.json" # type: ignore |
| 117 | + ) |
| 118 | +) |
| 119 | + |
| 120 | +# Load default and custom colormaps from json files |
| 121 | +DEFAULT_COLORMAPS: CmapDictType = load_qwt_colormaps_from_json(DEFAULT_COLORMAPS_PATH) |
| 122 | +CUSTOM_COLORMAPS: CmapDictType = load_qwt_colormaps_from_json(CUSTOM_COLORMAPS_PATH) |
| 123 | + |
| 124 | +# Merge default and custom colormaps into a single dictionnary to simplify access |
| 125 | +ALL_COLORMAPS: CmapDictType = {**DEFAULT_COLORMAPS, **CUSTOM_COLORMAPS} |
| 126 | + |
| 127 | +# Default colormap to use if a colormap is not found |
| 128 | +DEFAULT = ALL_COLORMAPS["jet"] |
| 129 | + |
| 130 | + |
73 | 131 | def save_colormaps(json_filename: str, colormaps: CmapDictType): |
74 | 132 | """Saves colormaps into the given json file. Refer ton function get_cmap_path to |
75 | 133 | know what json_filename can be used. |
@@ -211,57 +269,3 @@ def add_cmap(cmap: EditableColormap) -> None: |
211 | 269 | ALL_COLORMAPS[cmap.name.lower()] = cmap |
212 | 270 | CUSTOM_COLORMAPS[cmap.name.lower()] = cmap |
213 | 271 | save_colormaps(CUSTOM_COLORMAPS_PATH, CUSTOM_COLORMAPS) |
214 | | - |
215 | | - |
216 | | -def get_cmap_path(config_path: str): |
217 | | - """Takes a file path (i.e. from the CONF global variable) and tries to find it in |
218 | | - this order: |
219 | | - 1. in Plotpy's data directory |
220 | | - 2. in user's plotpy configuration directory |
221 | | - 3. anywhere else using the path as an absolute path |
222 | | - If the file is not found, the absolute filepath returned will point to user's plotpy |
223 | | - configuration folder. |
224 | | -
|
225 | | - Args: |
226 | | - config_path: file path/name to check in the order listed above. |
227 | | - """ |
228 | | - try: |
229 | | - data_config_path = os.path.join( |
230 | | - get_module_data_path("plotpy", "data"), config_path |
231 | | - ) |
232 | | - if os.path.isfile(data_config_path): |
233 | | - return data_config_path |
234 | | - except (FileNotFoundError, PermissionError, OSError) as e: |
235 | | - print(e) |
236 | | - |
237 | | - user_config_path = CONF.get_path(config_path) |
238 | | - if os.path.isfile(user_config_path): |
239 | | - return user_config_path |
240 | | - |
241 | | - if os.path.isfile(config_path): |
242 | | - return config_path |
243 | | - |
244 | | - return user_config_path |
245 | | - |
246 | | - |
247 | | -# Load default colormaps path from the config file |
248 | | -DEFAULT_COLORMAPS_PATH = get_cmap_path( |
249 | | - CONF.get( |
250 | | - "colormaps", |
251 | | - "colormaps/default", |
252 | | - default="colormaps_default.json", # type: ignore |
253 | | - ) |
254 | | -) |
255 | | -# Load custom colormaps path from the config file |
256 | | -CUSTOM_COLORMAPS_PATH = get_cmap_path( |
257 | | - CONF.get( |
258 | | - "colormaps", "colormaps/custom", default="colormaps_custom.json" # type: ignore |
259 | | - ) |
260 | | -) |
261 | | - |
262 | | -# Load default and custom colormaps from json files |
263 | | -DEFAULT_COLORMAPS: CmapDictType = load_qwt_colormaps_from_json(DEFAULT_COLORMAPS_PATH) |
264 | | -CUSTOM_COLORMAPS: CmapDictType = load_qwt_colormaps_from_json(CUSTOM_COLORMAPS_PATH) |
265 | | - |
266 | | -# Merge default and custom colormaps into a single dictionnary to simplify access |
267 | | -ALL_COLORMAPS: CmapDictType = {**DEFAULT_COLORMAPS, **CUSTOM_COLORMAPS} |
|
0 commit comments