1
# -*- coding: utf-8 -*-
3
# Copyright © 2009-2010 CEA
5
# Licensed under the terms of the CECILL License
6
# (see guiqwt/__init__.py for details)
12
The `colormap` module contains definition of common colormaps and tools
13
to manipulate and create them
16
from PyQt4.Qwt5 import QwtLinearColorMap, QwtDoubleInterval, toQImage
17
from PyQt4.QtGui import QColor, QIcon, QPixmap
19
from numpy import array, uint8, linspace, zeros, newaxis
22
from guiqwt import _cm # Reuse matplotlib data
25
def _interpolate(val, vmin, vmax):
26
"""Interpolate a color component between to values as provided
27
by matplotlib colormaps
29
interp = (val-vmin[0])/(vmax[0]-vmin[0])
30
return (1-interp)*vmin[1] + interp*vmax[2]
32
def _setup_colormap(cmap, cmdata):
33
"""Setup a QwtLinearColorMap according to
36
red = array(cmdata["red"])
37
green = array(cmdata["green"])
38
blue = array(cmdata["blue"])
40
qmin.setRgbF(red[ 0, 2], green[ 0, 2], blue[ 0, 2])
42
qmax.setRgbF(red[-1, 2], green[-1, 2], blue[-1, 2])
43
cmap.setColorInterval( qmin, qmax )
44
indices = sorted(set(red[:, 0]) | set(green[:, 0]) | set(blue[:, 0]))
45
for i in indices[1:-1]:
46
idxr = red[:, 0].searchsorted(i)
47
idxg = green[:, 0].searchsorted(i)
48
idxb = blue[:, 0].searchsorted(i)
49
compr = _interpolate(i, red[idxr-1], red[idxr])
50
compg = _interpolate(i, green[idxg-1], green[idxg])
51
compb = _interpolate(i, blue[idxb-1], blue[idxb] )
53
col.setRgbF(compr, compg, compb)
54
cmap.addColorStop(i, col)
56
# usefull to obtain a full color map
57
FULLRANGE = QwtDoubleInterval(0.0, 1.0)
63
Return a QwtColormap based on matplotlib's colormap of the same name
64
We avoid rebuilding the cmap by keeping it in cache
67
return COLORMAPS[name]
69
colormap = QwtLinearColorMap()
70
COLORMAPS[name] = colormap
71
COLORMAPS[colormap] = name
72
data = getattr(_cm, "_"+name+"_data")
73
_setup_colormap(colormap, data)
76
def get_cmap_name(cmap):
77
"""Return colormap's name"""
78
return COLORMAPS.get(cmap, None)
80
def get_colormap_list():
81
"""Builds a list of available colormaps
82
by introspection of the _cm module"""
85
if name.endswith("_data"):
86
obj = getattr(_cm, name)
87
if isinstance(obj, dict):
88
cmlist.append(name[1:-5])
91
def build_icon_from_cmap(cmap, width=32, height=32):
93
Builds an icon representing the colormap
95
data = zeros((width, height), uint8)
96
line = linspace(0, 255, width)
97
data[:, :] = line[:, newaxis]
99
img.setColorTable(cmap.colorTable(FULLRANGE))
100
return QIcon(QPixmap.fromImage(img))
103
def build_icon_from_cmap_name(cmap_name):
104
if cmap_name in ICON_CACHE:
105
return ICON_CACHE[cmap_name]
106
icon = build_icon_from_cmap(get_cmap(cmap_name))
107
ICON_CACHE[cmap_name] = icon
b'\\ No newline at end of file'