~ubuntu-branches/ubuntu/oneiric/guiqwt/oneiric

« back to all changes in this revision

Viewing changes to guiqwt/colormap.py

  • Committer: Bazaar Package Importer
  • Author(s): Picca Frédéric-Emmanuel
  • Date: 2010-11-13 11:26:05 UTC
  • Revision ID: james.westby@ubuntu.com-20101113112605-k2ffx4p80rict966
Tags: upstream-2.0.8.1
Import upstream version 2.0.8.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# Copyright © 2009-2010 CEA
 
4
# Pierre Raybaut
 
5
# Licensed under the terms of the CECILL License
 
6
# (see guiqwt/__init__.py for details)
 
7
 
 
8
"""
 
9
guiqwt.colormap
 
10
---------------
 
11
 
 
12
The `colormap` module contains definition of common colormaps and tools
 
13
to manipulate and create them
 
14
"""
 
15
 
 
16
from PyQt4.Qwt5 import QwtLinearColorMap, QwtDoubleInterval, toQImage
 
17
from PyQt4.QtGui import QColor, QIcon, QPixmap
 
18
 
 
19
from numpy import array, uint8, linspace, zeros, newaxis
 
20
 
 
21
# Local imports
 
22
from guiqwt import _cm # Reuse matplotlib data
 
23
 
 
24
 
 
25
def _interpolate(val, vmin, vmax):
 
26
    """Interpolate a color component between to values as provided
 
27
    by matplotlib colormaps
 
28
    """
 
29
    interp = (val-vmin[0])/(vmax[0]-vmin[0])
 
30
    return (1-interp)*vmin[1] + interp*vmax[2]
 
31
 
 
32
def _setup_colormap(cmap, cmdata):
 
33
    """Setup a QwtLinearColorMap according to
 
34
    matplotlib's data
 
35
    """
 
36
    red = array(cmdata["red"])
 
37
    green = array(cmdata["green"])
 
38
    blue = array(cmdata["blue"])
 
39
    qmin = QColor()
 
40
    qmin.setRgbF(red[ 0, 2], green[ 0, 2], blue[ 0, 2])
 
41
    qmax = QColor()
 
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] )
 
52
        col = QColor()
 
53
        col.setRgbF(compr, compg, compb)
 
54
        cmap.addColorStop(i, col)
 
55
 
 
56
# usefull to obtain a full color map
 
57
FULLRANGE = QwtDoubleInterval(0.0, 1.0)
 
58
 
 
59
COLORMAPS = {}
 
60
 
 
61
def get_cmap(name):
 
62
    """
 
63
    Return a QwtColormap based on matplotlib's colormap of the same name
 
64
    We avoid rebuilding the cmap by keeping it in cache
 
65
    """
 
66
    if name in COLORMAPS:
 
67
        return COLORMAPS[name]
 
68
    
 
69
    colormap = QwtLinearColorMap()
 
70
    COLORMAPS[name] = colormap
 
71
    COLORMAPS[colormap] = name
 
72
    data = getattr(_cm, "_"+name+"_data")
 
73
    _setup_colormap(colormap, data)
 
74
    return colormap
 
75
 
 
76
def get_cmap_name(cmap):
 
77
    """Return colormap's name"""
 
78
    return COLORMAPS.get(cmap, None)
 
79
 
 
80
def get_colormap_list():
 
81
    """Builds a list of available colormaps
 
82
    by introspection of the _cm module"""
 
83
    cmlist = []
 
84
    for name in dir(_cm):
 
85
        if name.endswith("_data"):
 
86
            obj = getattr(_cm, name)
 
87
            if isinstance(obj, dict):
 
88
                cmlist.append(name[1:-5])
 
89
    return cmlist
 
90
 
 
91
def build_icon_from_cmap(cmap, width=32, height=32):
 
92
    """
 
93
    Builds an icon representing the colormap
 
94
    """
 
95
    data = zeros((width, height), uint8)
 
96
    line = linspace(0, 255, width)
 
97
    data[:, :] = line[:, newaxis]
 
98
    img = toQImage(data)
 
99
    img.setColorTable(cmap.colorTable(FULLRANGE))
 
100
    return QIcon(QPixmap.fromImage(img))
 
101
    
 
102
ICON_CACHE = {}
 
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
 
108
    return icon
 
 
b'\\ No newline at end of file'