66# pylint: disable=C0103
77
88"""
9- Colormap functions
9+ Colormap utilities
1010------------------
1111
12- Overview
13- ^^^^^^^^
14-
15- The :py:mod:`.colormap` module contains definition of common colormaps and tools
16- to manipulate and create them.
17-
18- The following functions are available:
19-
20- * :py:func:`.get_cmap`: get a colormap from its name
21- * :py:func:`.get_cmap_name`: get a colormap's name
22- * :py:func:`.get_colormap_list`: get the list of available colormaps
23- * :py:func:`.build_icon_from_cmap`: build an icon representing the colormap
24- * :py:func:`.build_icon_from_cmap_name`: build an icon representing the colormap
25- from its name
26- * :py:func:`.register_extra_colormap`: register a custom colormap
27-
28- Reference
29- ^^^^^^^^^
30-
31- .. autofunction:: get_cmap
32- .. autofunction:: get_cmap_name
33- .. autofunction:: get_colormap_list
34- .. autofunction:: build_icon_from_cmap
35- .. autofunction:: build_icon_from_cmap_name
36- .. autofunction:: register_extra_colormap
3712"""
3813
3914from __future__ import annotations
4015
4116import _cm
42- from numpy import array , linspace , newaxis , uint8 , zeros
17+ import numpy as np
4318from qtpy import QtGui as QG
44- from qwt import QwtInterval , toQImage
19+ from qwt import QwtInterval
4520
4621from plotpy .mathutils .colormaps import (
4722 DEFAULT_COLORMAPS ,
@@ -70,9 +45,9 @@ def _setup_colormap(cmap: EditableColormap, cmdata):
7045 """Setup a CustomQwtLinearColorMap according to
7146 matplotlib's data
7247 """
73- red = array (cmdata ["red" ])
74- green = array (cmdata ["green" ])
75- blue = array (cmdata ["blue" ])
48+ red = np . array (cmdata ["red" ])
49+ green = np . array (cmdata ["green" ])
50+ blue = np . array (cmdata ["blue" ])
7651 qmin = QG .QColor ()
7752 qmin .setRgbF (red [0 , 2 ], green [0 , 2 ], blue [0 , 2 ])
7853 qmax = QG .QColor ()
@@ -112,18 +87,6 @@ def get_cmap(name: str) -> EditableColormap:
11287 return colormap
11388
11489
115- def get_cmap_name (cmap : EditableColormap ) -> str :
116- """Return colormap's name
117-
118- Args:
119- cmap: colormap
120-
121- Returns:
122- colormap's name
123- """
124- return COLORMAPS .get (cmap , None )
125-
126-
12790def get_colormap_list () -> list [str ]:
12891 """Builds a list of available colormaps by introspection of the _cm module
12992
@@ -140,54 +103,6 @@ def get_colormap_list() -> list[str]:
140103 return cmlist
141104
142105
143- def build_icon_from_cmap (
144- cmap : EditableColormap , width : int = 24 , height : int = 24
145- ) -> QG .QIcon :
146- """Builds an icon representing the colormap
147-
148- Args:
149- cmap: colormap
150- width: icon width
151- height: icon height
152- """
153- data = zeros ((width , height ), uint8 )
154- line = linspace (0 , 255 , width )
155- data [:, :] = line [:, newaxis ]
156- img = toQImage (data )
157- img .setColorTable (cmap .colorTable (FULLRANGE ))
158- return QG .QIcon (QG .QPixmap .fromImage (img ))
159-
160-
161- def build_icon_from_cmap_name (cmap_name : str ) -> QG .QIcon :
162- """Builds an icon representing the colormap from its name
163-
164- Args:
165- cmap_name: colormap name
166-
167- Returns:
168- icon representing the colormap
169- """
170- if cmap_name in ICON_CACHE :
171- return ICON_CACHE [cmap_name ]
172- icon = build_icon_from_cmap (get_cmap (cmap_name ))
173- ICON_CACHE [cmap_name ] = icon
174- return icon
175-
176-
177- def register_extra_colormap (name : str , colormap : EditableColormap ) -> None :
178- """Add a custom colormap to the list of known colormaps
179- must be done early in the import process because
180- datasets will use get_color_map list at import time
181-
182- Args:
183- name: colormap name
184- colormap: QwtColorMap object
185- """
186- COLORMAPS [name ] = colormap
187- COLORMAPS [colormap ] = name
188- EXTRA_COLORMAPS .append (name )
189-
190-
191106if __name__ == "__main__" :
192107 for cmap in get_colormap_list ():
193108 DEFAULT_COLORMAPS [cmap ] = get_cmap (cmap )
0 commit comments