cmapkit 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cmapkit/__init__.py +17 -0
- cmapkit/_machado.py +50 -0
- cmapkit/bounds.py +257 -0
- cmapkit/calibrate.py +181 -0
- cmapkit/hdrmaps/__init__.py +33 -0
- cmapkit/hdrmaps/curve.py +313 -0
- cmapkit/hdrmaps/data/maps.json +746 -0
- cmapkit/hdrmaps/library.py +130 -0
- cmapkit/hdrmaps/measure.py +62 -0
- cmapkit/hdrmaps/noise.py +176 -0
- cmapkit/hdrmaps/paths.py +143 -0
- cmapkit/mccp/__init__.py +6 -0
- cmapkit/mccp/data/palettes.json +5782 -0
- cmapkit/mccp/figures.py +222 -0
- cmapkit/mccp/optimise.py +914 -0
- cmapkit/mccp/order.py +124 -0
- cmapkit/mpl.py +180 -0
- cmapkit/space.py +314 -0
- cmapkit-0.1.0.dist-info/METADATA +363 -0
- cmapkit-0.1.0.dist-info/RECORD +24 -0
- cmapkit-0.1.0.dist-info/WHEEL +5 -0
- cmapkit-0.1.0.dist-info/licenses/LICENSE +21 -0
- cmapkit-0.1.0.dist-info/licenses/NOTICE +34 -0
- cmapkit-0.1.0.dist-info/top_level.txt +1 -0
cmapkit/mccp/figures.py
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
"""Test figures (docs/test-figures.md)."""
|
|
2
|
+
|
|
3
|
+
import math
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
|
|
7
|
+
from .order import distance_matrix, path_order
|
|
8
|
+
from ..space import Observer, Space, hex_to_rgb, linear_to_srgb, rgb_to_hex, srgb_to_linear, viewing
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
# --- ring layout ---------------------------------------------------------------
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _neighbours(R, N):
|
|
15
|
+
nb = [[[] for _ in range(N)] for _ in range(R)]
|
|
16
|
+
for r in range(R):
|
|
17
|
+
for k in range(N):
|
|
18
|
+
a, b = (r, k), (r, (k + 1) % N)
|
|
19
|
+
nb[a[0]][a[1]].append(b)
|
|
20
|
+
nb[b[0]][b[1]].append(a)
|
|
21
|
+
if r:
|
|
22
|
+
for c in ((r - 1, k), (r - 1, (k + 1) % N)):
|
|
23
|
+
nb[r][k].append(c)
|
|
24
|
+
nb[c[0]][c[1]].append((r, k))
|
|
25
|
+
return nb
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _anneal(N, R, fixed, steps, seed, target=2):
|
|
29
|
+
"""Simulated annealing over swaps within free rings.
|
|
30
|
+
|
|
31
|
+
fixed: dict ring -> sequence of colours. Returns (shortfall, grid)."""
|
|
32
|
+
rng = np.random.default_rng(seed)
|
|
33
|
+
nb = _neighbours(R, N)
|
|
34
|
+
grid = np.array([fixed[r] if r in fixed else rng.permutation(N) for r in range(R)])
|
|
35
|
+
free = [r for r in range(R) if r not in fixed]
|
|
36
|
+
cnt = np.zeros((N, N), int)
|
|
37
|
+
for r in range(R):
|
|
38
|
+
for k in range(N):
|
|
39
|
+
for (m, j) in nb[r][k]:
|
|
40
|
+
if (m, j) > (r, k):
|
|
41
|
+
x, y = grid[r, k], grid[m, j]
|
|
42
|
+
cnt[x, y] += 1
|
|
43
|
+
cnt[y, x] += 1
|
|
44
|
+
iu = np.triu_indices(N, 1)
|
|
45
|
+
short = int(np.maximum(0, target - cnt[iu]).sum())
|
|
46
|
+
if not free:
|
|
47
|
+
return short, grid
|
|
48
|
+
T = 1.0
|
|
49
|
+
for _ in range(steps):
|
|
50
|
+
if short == 0:
|
|
51
|
+
break
|
|
52
|
+
r = free[rng.integers(len(free))]
|
|
53
|
+
k1, k2 = rng.choice(N, 2, replace=False)
|
|
54
|
+
cells = {(r, k1), (r, k2)}
|
|
55
|
+
|
|
56
|
+
def touch(sign):
|
|
57
|
+
d = 0
|
|
58
|
+
seen = set()
|
|
59
|
+
for (a, b) in cells:
|
|
60
|
+
for (m, j) in nb[a][b]:
|
|
61
|
+
e = tuple(sorted([(a, b), (m, j)]))
|
|
62
|
+
if e in seen:
|
|
63
|
+
continue
|
|
64
|
+
seen.add(e)
|
|
65
|
+
x, y = grid[a, b], grid[m, j]
|
|
66
|
+
if x == y:
|
|
67
|
+
continue
|
|
68
|
+
if sign < 0:
|
|
69
|
+
cnt[x, y] -= 1
|
|
70
|
+
cnt[y, x] -= 1
|
|
71
|
+
if cnt[x, y] < target:
|
|
72
|
+
d += 1
|
|
73
|
+
else:
|
|
74
|
+
if cnt[x, y] < target:
|
|
75
|
+
d -= 1
|
|
76
|
+
cnt[x, y] += 1
|
|
77
|
+
cnt[y, x] += 1
|
|
78
|
+
return d
|
|
79
|
+
|
|
80
|
+
d = touch(-1)
|
|
81
|
+
grid[r, k1], grid[r, k2] = grid[r, k2], grid[r, k1]
|
|
82
|
+
d += touch(+1)
|
|
83
|
+
if d <= 0 or rng.random() < math.exp(-d / T):
|
|
84
|
+
short += d
|
|
85
|
+
else:
|
|
86
|
+
touch(-1)
|
|
87
|
+
grid[r, k1], grid[r, k2] = grid[r, k2], grid[r, k1]
|
|
88
|
+
touch(+1)
|
|
89
|
+
T = max(0.02, T * 0.99995)
|
|
90
|
+
return short, grid
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def ring_layout(d, steps=150_000, seeds=3, target=2):
|
|
94
|
+
"""Ring arrangement for a distance matrix d (N x N).
|
|
95
|
+
|
|
96
|
+
Ring 0 is innermost. The outer ring holds the similar ordering; the inner
|
|
97
|
+
ring holds the contrasting ordering where the search allows."""
|
|
98
|
+
N = len(d)
|
|
99
|
+
similar = path_order(d, "similar")
|
|
100
|
+
contrasting = path_order(d, "contrasting")
|
|
101
|
+
R = max(2, math.ceil((N + 1) / 3))
|
|
102
|
+
while True:
|
|
103
|
+
for fixed in ({0: contrasting, R - 1: similar}, {R - 1: similar}):
|
|
104
|
+
for s in range(seeds):
|
|
105
|
+
short, grid = _anneal(N, R, fixed, steps, s, target)
|
|
106
|
+
if short == 0:
|
|
107
|
+
return grid
|
|
108
|
+
R += 1
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
# --- simulation ----------------------------------------------------------------
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def simulate(hexes, observer):
|
|
115
|
+
"""Colours as seen by a simulated observer, clipped to sRGB for display."""
|
|
116
|
+
lin = srgb_to_linear(hex_to_rgb(hexes))
|
|
117
|
+
return rgb_to_hex(linear_to_srgb(lin @ observer.matrix().T))
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
# --- drawing -------------------------------------------------------------------
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def draw_rings(ax, hexes, grid, background="#ffffff", hole=0.22):
|
|
124
|
+
from matplotlib.patches import Wedge
|
|
125
|
+
R, N = grid.shape
|
|
126
|
+
width = (1 - hole) / R
|
|
127
|
+
for r in range(R):
|
|
128
|
+
# Ring r is turned through r/2 cells, so that cell (r, k) borders
|
|
129
|
+
# cells (r - 1, k) and (r - 1, k + 1), as _neighbours assumes.
|
|
130
|
+
offset = 0.5 * r
|
|
131
|
+
for k in range(N):
|
|
132
|
+
t0 = 360 * (k + offset) / N
|
|
133
|
+
ax.add_patch(Wedge((0, 0), hole + (r + 1) * width, t0, t0 + 360 / N,
|
|
134
|
+
width=width, facecolor=hexes[grid[r, k]], edgecolor="none"))
|
|
135
|
+
ax.add_patch(Wedge((0, 0), hole, 0, 360, facecolor=background, edgecolor="none"))
|
|
136
|
+
ax.set_xlim(-1.02, 1.02)
|
|
137
|
+
ax.set_ylim(-1.02, 1.02)
|
|
138
|
+
ax.set_aspect("equal")
|
|
139
|
+
ax.set_facecolor(background)
|
|
140
|
+
ax.set_xticks([])
|
|
141
|
+
ax.set_yticks([])
|
|
142
|
+
for s in ax.spines.values():
|
|
143
|
+
s.set_visible(False)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def closest_pairs(hexes, space, k=6):
|
|
147
|
+
d = distance_matrix(hex_to_rgb(hexes), space)
|
|
148
|
+
iu = np.triu_indices(len(hexes), 1)
|
|
149
|
+
order = np.argsort(d[iu])[:k]
|
|
150
|
+
return [(int(iu[0][o]), int(iu[1][o]), float(d[iu][o])) for o in order]
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def draw_pairs(ax, hexes, pairs, background="#ffffff"):
|
|
154
|
+
"""Each closest pair as touching swatches and as two thin lines."""
|
|
155
|
+
ax.set_facecolor(background)
|
|
156
|
+
x = np.linspace(0, 1, 50)
|
|
157
|
+
for row, (i, j, dist) in enumerate(pairs):
|
|
158
|
+
y = -row
|
|
159
|
+
ax.add_patch(_rect(0.0, y - 0.4, 0.3, 0.8, hexes[i]))
|
|
160
|
+
ax.add_patch(_rect(0.3, y - 0.4, 0.3, 0.8, hexes[j]))
|
|
161
|
+
ax.plot(0.8 + 0.8 * x, y + 0.15 + 0.1 * np.sin(6 * x), color=hexes[i], lw=1.0)
|
|
162
|
+
ax.plot(0.8 + 0.8 * x, y - 0.15 + 0.1 * np.sin(6 * x + 1), color=hexes[j], lw=1.0)
|
|
163
|
+
ax.text(1.7, y, f"{dist:.1f}", va="center", fontsize=7)
|
|
164
|
+
ax.set_xlim(-0.05, 2.0)
|
|
165
|
+
ax.set_ylim(-len(pairs) + 0.4, 0.6)
|
|
166
|
+
ax.axis("off")
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def _rect(x, y, w, h, c):
|
|
170
|
+
from matplotlib.patches import Rectangle
|
|
171
|
+
return Rectangle((x, y), w, h, facecolor=c, edgecolor="none")
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def draw_plot(ax, hexes, background="#ffffff"):
|
|
175
|
+
"""A realistic plot: lines of two widths and markers."""
|
|
176
|
+
ax.set_facecolor(background)
|
|
177
|
+
x = np.linspace(0, 10, 200)
|
|
178
|
+
n = len(hexes)
|
|
179
|
+
for i, c in enumerate(hexes):
|
|
180
|
+
ax.plot(x, np.sin(x + 0.6 * i) + 0.25 * i, color=c, lw=1.0 if i % 2 else 2.2)
|
|
181
|
+
ax.scatter(np.linspace(0.5, 9.5, 6) + 0.1 * i,
|
|
182
|
+
np.cos(np.linspace(0.5, 9.5, 6) + i) - 1.5 - 0.1 * i, color=c, s=12)
|
|
183
|
+
ax.set_xticks([])
|
|
184
|
+
ax.set_yticks([])
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def report_figure(hexes, title, background="#ffffff", preset="screen", ucs="UCS",
|
|
188
|
+
lightness_weight=None,
|
|
189
|
+
observers=(Observer("deutan", 1.0), Observer("protan", 1.0),
|
|
190
|
+
Observer("tritan", 1.0)), path=None):
|
|
191
|
+
"""Ring disc, closest pairs and a plot, for normal vision and each observer."""
|
|
192
|
+
import matplotlib.pyplot as plt
|
|
193
|
+
from ..space import relative_luminance
|
|
194
|
+
fg = "#000000" if relative_luminance(hex_to_rgb(background))[0] > 0.18 else "#ffffff"
|
|
195
|
+
with plt.rc_context({"text.color": fg, "axes.edgecolor": fg, "axes.labelcolor": fg,
|
|
196
|
+
"xtick.color": fg, "ytick.color": fg}):
|
|
197
|
+
return _report_figure(hexes, title, background, preset, ucs, lightness_weight,
|
|
198
|
+
observers, path)
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def _report_figure(hexes, title, background, preset, ucs, lightness_weight, observers, path):
|
|
202
|
+
import matplotlib.pyplot as plt
|
|
203
|
+
view = viewing(preset, background, ucs=ucs, lightness_weight=lightness_weight)
|
|
204
|
+
rows = [Observer()] + list(observers)
|
|
205
|
+
d = distance_matrix(hex_to_rgb(hexes), Space(view))
|
|
206
|
+
grid = ring_layout(d)
|
|
207
|
+
fig, axes = plt.subplots(len(rows), 3, figsize=(10, 3.2 * len(rows)),
|
|
208
|
+
gridspec_kw=dict(width_ratios=[1, 1, 1.4]))
|
|
209
|
+
fig.patch.set_facecolor(background)
|
|
210
|
+
for r, obs in enumerate(rows):
|
|
211
|
+
shown = hexes if obs.kind == "normal" else simulate(hexes, obs)
|
|
212
|
+
space = Space(view, (obs,))
|
|
213
|
+
draw_rings(axes[r, 0], shown, grid, background)
|
|
214
|
+
draw_pairs(axes[r, 1], shown, closest_pairs(hexes, space), background)
|
|
215
|
+
draw_plot(axes[r, 2], shown, background)
|
|
216
|
+
axes[r, 0].set_title(str(obs), fontsize=9)
|
|
217
|
+
fig.suptitle(title, fontsize=11)
|
|
218
|
+
fig.tight_layout()
|
|
219
|
+
if path:
|
|
220
|
+
fig.savefig(path, dpi=110, facecolor=background)
|
|
221
|
+
plt.close(fig)
|
|
222
|
+
return fig
|