color_2Colorbars.py
3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
title = "Merge 2 Colorbars"
tip = "applies reverse 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, QwtInterval
FULLRANGE = QwtInterval(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):
cmaps = get_colormap_list()
class FuncParam(DataSet):
cmap1 = ChoiceItem(_(u"Colormap1"), zip(cmaps, cmaps), default='gray')
cmap2 = ChoiceItem(_(u"Colormap2"), zip(cmaps, cmaps), default='hot')
name = title.replace(" ", "")
if args == {}:
param = FuncParam(_(title), "Set gamma 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 decode(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 merge_color_maps(self, cmap1, cmap2, f=255):
cmap_1 = cmap1.colorTable(FULLRANGE)
cmap_2 = cmap2.colorTable(FULLRANGE)
lut = {'red': [], 'green': [], 'blue': []}
n = 0
for i in range(0, len(cmap_1), 2):
k = float(i/2)/f
r, g, b = self.setColor(cmap_1[i])
lut['red'].append((k, r, r))
lut['green'].append((k , g, g))
lut['blue'].append((k, b, b))
for i in range(0, len(cmap_2), 2):
k = 0.5+float(i/2)/f
r, g, b = self.setColor(cmap_2[i])
lut['red'].append((k, r, r))
lut['green'].append((k, g, g))
lut['blue'].append((k, b, b))
return lut
def function(self, m, p):
cmap1 = get_cmap(p.cmap1)
cmap2 = get_cmap(p.cmap2)
cmdata = self.merge_color_maps(cmap1, cmap2)
my_cmap = QwtLinearColorMap()
cname = p.cmap1 + "_merged"
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