color_0RGB.py 2.87 KB
title = "RGBcolors"
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):
              val = FloatItem("Value:", default=1.0)
        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 sign(self, x):
        return np.where(x < 0, -1, 1)

    def setup_color_map(self, val, f=255.0):
        cmap_name = self.parent.cmap
        #table = cm.get_cmap(cmap_name)
        #cmap = table.colorTable(FULLRANGE)
        lut = {'red': [], 'green': [], 'blue': []}
        x = np.linspace(0, 1, 256)
        r = 1.0 - self.sign(x)*abs(2*x - 2)**val
        r = np.where(r < 0, 0, r)
        r = zip(x, r, r)
        g = 1.0 - self.sign(x)*abs(2*x - 1)**val
        g = np.where(g < 0, 0, g)
        g = zip(x, g, g)
        b = 1.0 - self.sign(x)*abs(2*x - 0)**val
        b = np.where(b < 0, 0, b)
        b = zip(x, b, b)
        lut = {}
        lut['red'] = r
        lut['green'] = g
        lut['blue'] = b
        return lut

    def function(self, m, p):
        cmdata = self.setup_color_map(p.val)
        my_cmap = QwtLinearColorMap()
        cname = self.parent.cmap + "_RGB"
        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()
        self.parent.cmap = cname
        if plot:
           #plot.update_colormap_axis(cm.get_cmap(cname))
           plot.replot()
        return [], p