color_Special.py 3.29 KB
title = "Special"
tip = "applies special manipulations to colorbar"
onein = True

import numpy as np

import guiqwt.colormap as cm
from guidata.qt.QtGui import QMessageBox
from guidata.dataset.datatypes import DataSet
from guidata.dataset.dataitems import (IntItem, StringItem, ChoiceItem, FloatItem, BoolItem)
#from guiqwt.colormap import get_colormap_list, get_cmap, register_extra_colormap
from guiqwt.config import _

from guiqwt.transitional import QwtLinearColorMap, QwtDoubleInterval
FULLRANGE = QwtDoubleInterval(0.0, 1.0)

def nint(x):
    if x > 0: return int(x+0.5)
    else: return int(x-0.5)

class NOD3_App:

    def __init__(self, parent):
        self.parent = parent
        self.parent.activateWindow()

    def Error(self, msg):
        QMessageBox.critical(self.parent.parent(), title,
                              _(u"Error:")+"\n%s" % str(msg))

    def compute_app(self, **args):
        class FuncParam(DataSet):
              special = ChoiceItem("Special", (("Square", "Square"), ("Squareroot", "Squareroot"),
                                              ("Linear", "Linear")), default = "Square")
        name = title.replace(" ", "")
        if args == {}:
           param = FuncParam(_(title), "Set special correction to colormap:")
        else:
           param = self.parent.ScriptParameter(name, args)

        # if no parameter needed set param to None. activate next line
        #param = None
        self.parent.compute_11(name, lambda m, p: self.function(m, p), param, onein) 

    def setColor(self, a, f=255.0):
        y = hex(a)[2:-1]
        x = [ord(c) for c in y.decode('hex')]
        r = (float(x[1])/f)
        g = (float(x[2])/f)
        b = (float(x[3])/f)
        return r, g, b

    def _reverse(self, color):
        color.reverse()
        return color

    def setup_color_map(self, special, f=255.0):
        cmap_name = self.parent.cmap
        if special == "Linear":
           table = cm.get_cmap(cmap_name)
           cmap = table.colorTable(FULLRANGE)
        else:
           item = self.parent.items[self.parent.row]
           cmap = item.get_color_map().colorTable(FULLRANGE)
        lut = {'red': [], 'green': [], 'blue': []}
        for i in range(len(cmap)):
            r, g, b = self.setColor(cmap[i])
            if special == "Square":
               r = r*r
               g = g*g
               b = b*b
            elif special == "Squareroot":
               r = np.sqrt(r)
               g = np.sqrt(g)
               b = np.sqrt(b)
            else:
               pass
            lut['red'].append((float(i)/f, r, r))
            lut['green'].append((float(i)/f, g, g))
            lut['blue'].append((float(i)/f, b, b))
        #lut['red'] = self._reverse(lut['red'])
        #lut['green'] = self._reverse(lut['green'])
        #lut['blue'] = self._reverse(lut['blue'])
        return lut

    def function(self, m, p):
        cmdata = self.setup_color_map(p.special)
        my_cmap = QwtLinearColorMap()
        cname = self.parent.cmap + "_special"
        cm._setup_colormap(my_cmap, cmdata)
        cm.register_extra_colormap(cname, my_cmap)
        item = self.parent.items[self.parent.row]
        item.set_color_map(cname)
        plot = item.plot()
        if plot:
           #plot.update_colormap_axis(cm.get_cmap(cname))
           plot.replot()
        return [], p