forked from pyqtgraph/pyqtgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_colormap.py
94 lines (77 loc) · 2.7 KB
/
test_colormap.py
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
import pytest
import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtGui
pos = [0.0, 0.5, 1.0]
qcols = [
QtGui.QColor('#FF0000'),
QtGui.QColor('#00FF00'),
QtGui.QColor('#0000FF')
]
float_tuples = [
(1.0, 0.0, 0.0, 1.0),
(0.0, 1.0, 0.0, 1.0),
(0.0, 0.0, 1.0, 1.0)
]
int_tuples = [
(255, 0, 0,255),
( 0,255, 0,255),
( 0, 0,255,255)
]
@pytest.mark.parametrize("color_list", (qcols, int_tuples))
def test_ColorMap_getStops(color_list):
cm = pg.ColorMap(pos, color_list, name='test')
# default is byte format:
stops, colors = cm.getStops()
assert (stops == pos).all()
assert (colors == int_tuples).all()
# manual byte format:
stops, colors = cm.getStops(pg.ColorMap.BYTE)
assert (stops == pos).all()
assert (colors == int_tuples).all()
stops, colors = cm.getStops('bYTe')
assert (stops == pos).all()
assert (colors == int_tuples).all()
# manual float format:
stops, colors = cm.getStops(pg.ColorMap.FLOAT)
assert (stops == pos).all()
assert (colors == float_tuples).all()
stops, colors = cm.getStops('floaT')
assert (stops == pos).all()
assert (colors == float_tuples).all()
# manual QColor format:
stops, colors = cm.getStops(pg.ColorMap.QCOLOR)
assert (stops == pos).all()
for actual, good in zip(colors, qcols):
assert actual.getRgbF() == good.getRgbF()
stops, colors = cm.getStops('qColor')
assert (stops == pos).all()
for actual, good in zip(colors, qcols):
assert actual.getRgbF() == good.getRgbF()
@pytest.mark.parametrize("color_list", (qcols, int_tuples))
def test_ColorMap_getColors(color_list):
cm = pg.ColorMap(pos, color_list, name='from QColors')
colors = cm.getColors()
assert (colors == int_tuples).all()
colors = cm.getColors('byte')
assert (colors == int_tuples).all()
colors = cm.getColors('float')
assert (colors == float_tuples).all()
colors = cm.getColors('qcolor')
for actual, good in zip(colors, qcols):
assert actual.getRgbF() == good.getRgbF()
def test_ColorMap_getByIndex():
cm = pg.ColorMap([0.0, 1.0], [(0,0,0), (255,0,0)])
assert cm.getByIndex(0) == QtGui.QColor.fromRgbF(0.0, 0.0, 0.0, 1.0)
assert cm.getByIndex(1) == QtGui.QColor.fromRgbF(1.0, 0.0, 0.0, 1.0)
def test_round_trip():
# test that colormap survives a round trip.
# note that while both input and output are in BYTE,
# internally the colors are stored as float; thus
# there is a conversion BYTE -> float -> BYTE
nPts = 256
zebra = np.zeros((nPts, 3), dtype=np.uint8)
zebra[1::2, :] = 255
cmap = pg.ColorMap(None, zebra)
lut = cmap.getLookupTable(nPts=nPts)
assert np.all(lut == zebra)