1# -*- coding: utf-8 -*- 2""" 3This example demonstrates linearized ColorMap objects using colormap.makeMonochrome() 4or using the `ColorMap`'s `linearize()` method. 5""" 6# Add path to library (just for examples; you do not need this) 7import initExample 8 9import numpy as np 10from pyqtgraph.Qt import QtCore, QtGui 11import pyqtgraph as pg 12 13name_list = ( 14 'warm','neutral','cool', 15 'green','amber','blue','red','pink','lavender', 16 (0.5, 0.2, 0.1, 0.8) 17) 18ramp_list = [ 19 pg.colormap.makeMonochrome(name) 20 for name in name_list 21] 22 23cm_list = [] 24# Create a gray ramp for demonstrating the idea: 25cm = pg.ColorMap( None, [ 26 QtGui.QColor( 0, 0, 0), 27 QtGui.QColor( 10, 10, 10), 28 QtGui.QColor(127, 127, 127), 29 QtGui.QColor(240, 240, 240), 30 QtGui.QColor(255, 255, 255) 31]) 32cm_list.append(('Distorted gray ramp',cm)) 33 34# Create a rainbow scale in HSL color space: 35length = 41 36col_list = [] 37for idx in range(length): 38 frac = idx/(length-1) 39 qcol = QtGui.QColor() 40 qcol.setHslF( (2*frac-0.15)%1.0, 0.8, 0.5-0.5*np.cos(np.pi*frac) ) 41 col_list.append(qcol) 42cm = pg.ColorMap( None, col_list ) 43cm_list.append( ('Distorted HSL spiral', cm) ) 44 45# Create some random examples: 46for example_idx in range(3): 47 previous = None 48 col_list = [] 49 for idx in range(8): 50 values = np.random.random((3)) 51 if previous is not None: 52 intermediate = (values + previous) / 2 53 qcol = QtGui.QColor() 54 qcol.setRgbF( *intermediate ) 55 col_list.append( qcol) 56 qcol1 = QtGui.QColor() 57 qcol1.setRgbF( *values ) 58 col_list.append( qcol1) 59 previous = values 60 cm = pg.ColorMap( None, col_list ) 61 cm_list.append( (f'random {example_idx+1}', cm) ) 62 63app = pg.mkQApp() 64win = QtGui.QMainWindow() 65win.resize(1000,800) 66 67lw = pg.GraphicsLayoutWidget() 68lw.setFixedWidth(1000) 69lw.setSizePolicy(QtGui.QSizePolicy.Policy.Expanding, QtGui.QSizePolicy.Policy.Expanding) 70 71scr = QtGui.QScrollArea() 72scr.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarPolicy.ScrollBarAlwaysOn) 73scr.setWidget(lw) 74win.setCentralWidget(scr) 75win.setWindowTitle('pyqtgraph example: Linearized color maps') 76win.show() 77 78bar_width = 32 79bar_data = pg.colormap.modulatedBarData(width=bar_width) 80 81num_bars = 0 82 83def add_heading(lw, name): 84 global num_bars 85 lw.addLabel('=== '+name+' ===') 86 num_bars += 1 87 lw.nextRow() 88 89def add_bar(lw, name, cm): 90 global num_bars 91 lw.addLabel(name) 92 imi = pg.ImageItem( bar_data ) 93 imi.setLookupTable( cm.getLookupTable(alpha=True) ) 94 vb = lw.addViewBox(lockAspect=True, enableMouse=False) 95 vb.addItem( imi ) 96 num_bars += 1 97 lw.nextRow() 98 99add_heading(lw, 'ramp generator') 100for cm in ramp_list: 101 add_bar(lw, cm.name, cm) 102 103add_heading(lw, 'linearizer demonstration') 104for (name, cm) in cm_list: 105 add_bar(lw, name, cm) 106 cm.linearize() 107 add_bar(lw, '> linearized', cm) 108 109add_heading(lw, 'consistency with included maps') 110for name in ('CET-C3', 'CET-L17', 'CET-L2'): 111 # lw.addLabel(str(name)) 112 cm = pg.colormap.get(name) 113 add_bar(lw, name, cm) 114 cm.linearize() 115 add_bar(lw, '> linearized', cm) 116 117lw.setFixedHeight(num_bars * (bar_width+5) ) 118 119if __name__ == '__main__': 120 pg.exec() 121