|
1 |
| -import numpy as np |
| 1 | +import json |
| 2 | + |
| 3 | +import pytest |
2 | 4 |
|
3 |
| -from viscm.bezierbuilder import json |
4 | 5 | from viscm.gui import Colormap, viscm_editor
|
5 | 6 |
|
6 |
| -cms = { |
7 |
| - "viscm/examples/sample_linear.jscm", |
8 |
| - "viscm/examples/sample_diverging.jscm", |
9 |
| - "viscm/examples/sample_diverging_continuous.jscm", |
10 |
| -} |
11 |
| - |
12 |
| - |
13 |
| -def test_editor_loads_native(): |
14 |
| - for k in cms: |
15 |
| - with open(k) as f: |
16 |
| - data = json.loads(f.read()) |
17 |
| - cm = Colormap(None, "CatmulClark", "CAM02-UCS") |
18 |
| - cm.load(k) |
19 |
| - viscm = viscm_editor( |
20 |
| - uniform_space=cm.uniform_space, |
21 |
| - cmtype=cm.cmtype, |
22 |
| - method=cm.method, |
23 |
| - **cm.params, |
24 |
| - ) |
25 |
| - assert viscm.name == data["name"] |
26 |
| - |
27 |
| - extensions = data["extensions"]["https://matplotlib.org/viscm"] |
28 |
| - xp, yp, fixed = viscm.control_point_model.get_control_points() |
29 |
| - |
30 |
| - assert extensions["fixed"] == fixed |
31 |
| - assert len(extensions["xp"]) == len(xp) |
32 |
| - assert len(extensions["yp"]) == len(yp) |
33 |
| - assert len(xp) == len(yp) |
34 |
| - for i in range(len(xp)): |
35 |
| - assert extensions["xp"][i] == xp[i] |
36 |
| - assert extensions["yp"][i] == yp[i] |
37 |
| - assert extensions["min_Jp"] == viscm.min_Jp |
38 |
| - assert extensions["max_Jp"] == viscm.max_Jp |
39 |
| - assert extensions["filter_k"] == viscm.filter_k |
40 |
| - assert extensions["cmtype"] == viscm.cmtype |
41 |
| - |
42 |
| - colors = data["colors"] |
43 |
| - colors = [ |
44 |
| - [int(c[i : i + 2], 16) / 256 for i in range(0, 6, 2)] |
45 |
| - for c in [colors[i : i + 6] for i in range(0, len(colors), 6)] |
46 |
| - ] |
47 |
| - editor_colors = viscm.cmap_model.get_sRGB(num=256)[0].tolist() |
48 |
| - for i in range(len(colors)): |
49 |
| - for z in range(3): |
50 |
| - assert colors[i][z] == np.rint(editor_colors[i][z] / 256) |
| 7 | + |
| 8 | +def approxeq(x, y, *, err=0.0001): |
| 9 | + return abs(y - x) < err |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.parametrize( |
| 13 | + "colormap_file", |
| 14 | + [ |
| 15 | + "viscm/examples/sample_linear.jscm", |
| 16 | + "viscm/examples/sample_diverging.jscm", |
| 17 | + "viscm/examples/sample_diverging_continuous.jscm", |
| 18 | + ], |
| 19 | +) |
| 20 | +def test_editor_loads_native(colormap_file): |
| 21 | + with open(colormap_file) as f: |
| 22 | + data = json.loads(f.read()) |
| 23 | + cm = Colormap(None, "CatmulClark", "CAM02-UCS") |
| 24 | + cm.load(colormap_file) |
| 25 | + viscm = viscm_editor( |
| 26 | + uniform_space=cm.uniform_space, |
| 27 | + cmtype=cm.cmtype, |
| 28 | + method=cm.method, |
| 29 | + **cm.params, |
| 30 | + ) |
| 31 | + assert viscm.name == data["name"] |
| 32 | + |
| 33 | + extensions = data["extensions"]["https://matplotlib.org/viscm"] |
| 34 | + xp, yp, fixed = viscm.control_point_model.get_control_points() |
| 35 | + |
| 36 | + assert extensions["fixed"] == fixed |
| 37 | + assert len(extensions["xp"]) == len(xp) |
| 38 | + assert len(extensions["yp"]) == len(yp) |
| 39 | + assert len(xp) == len(yp) |
| 40 | + for i in range(len(xp)): |
| 41 | + assert extensions["xp"][i] == xp[i] |
| 42 | + assert extensions["yp"][i] == yp[i] |
| 43 | + assert extensions["min_Jp"] == viscm.min_Jp |
| 44 | + assert extensions["max_Jp"] == viscm.max_Jp |
| 45 | + assert extensions["filter_k"] == viscm.cmap_model.filter_k |
| 46 | + assert extensions["cmtype"] == viscm.cmtype |
| 47 | + |
| 48 | + # Decode hexadecimal-encoded colormap string (grouped in units of 3 pairs of |
| 49 | + # two-character (0-255) values) to 3-tuples of floats (0-1). |
| 50 | + colors_hex = data["colors"] |
| 51 | + colors_hex = [colors_hex[i : i + 6] for i in range(0, len(colors_hex), 6)] |
| 52 | + colors = [ |
| 53 | + # TODO: Should we divide by 255 here instead of 256? The tests pass with a |
| 54 | + # lower value for `err` if we do. |
| 55 | + [int(c[i : i + 2], 16) / 256 for i in range(0, len(c), 2)] |
| 56 | + for c in colors_hex |
| 57 | + ] |
| 58 | + |
| 59 | + editor_colors = viscm.cmap_model.get_sRGB(num=256)[0].tolist() |
| 60 | + |
| 61 | + for i in range(len(colors)): |
| 62 | + for z in range(3): |
| 63 | + assert approxeq(colors[i][z], editor_colors[i][z], err=0.01) |
51 | 64 |
|
52 | 65 |
|
53 | 66 | # import matplotlib as mpl
|
54 |
| -# from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas |
| 67 | +# try: |
| 68 | +# from matplotlib.backends.backend_qtagg import FigureCanvasQTAgg as FigureCanvas |
| 69 | +# except ImportError: |
| 70 | +# try: |
| 71 | +# from matplotlib.backends.backend_qt5agg import ( |
| 72 | +# FigureCanvasQTAgg as FigureCanvas |
| 73 | +# ) |
| 74 | +# except ImportError: |
| 75 | +# from matplotlib.backends.backend_qt4agg import ( |
| 76 | +# FigureCanvasQTAgg as FigureCanvas |
| 77 | +# ) |
55 | 78 | # from matplotlib.backends.qt_compat import QtCore, QtGui
|
56 | 79 | #
|
57 | 80 | # def test_editor_add_point():
|
@@ -144,7 +167,3 @@ def test_editor_loads_native():
|
144 | 167 |
|
145 | 168 | # print(linear.control_point_model.get_control_points())
|
146 | 169 | # # print(linear.cmap_model.get_Jpapbp(3))
|
147 |
| - |
148 |
| - |
149 |
| -def approxeq(x, y, err=0.0001): |
150 |
| - return abs(y - x) < err |
|
0 commit comments