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/__init__.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""cmapkit: colour maps designed in CAM16-UCS.
|
|
2
|
+
|
|
3
|
+
The shared modules give the colour space and simulated observers (space), the
|
|
4
|
+
lightness limits for each background (bounds) and the calibration charts
|
|
5
|
+
(calibrate). The subpackage mccp chooses categorical palettes (maximum
|
|
6
|
+
contrast colour picking); its main functions are available here.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
__version__ = "0.1.0"
|
|
10
|
+
|
|
11
|
+
from . import mccp
|
|
12
|
+
from .mccp import Anchor, Palette, default_anchors, palette
|
|
13
|
+
from .space import Observer, Viewing, viewing
|
|
14
|
+
from .mpl import cycler, recolor, use
|
|
15
|
+
|
|
16
|
+
__all__ = ["Anchor", "Palette", "Observer", "Viewing", "cycler", "default_anchors", "mccp",
|
|
17
|
+
"palette", "recolor", "use", "viewing"]
|
cmapkit/_machado.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"""Colour-vision-deficiency simulation matrices of Machado et al. (2009).
|
|
2
|
+
|
|
3
|
+
Precomputed matrices at severities 0.0, 0.1, ..., 1.0, as published with the
|
|
4
|
+
paper and as tabulated in colour-science (BSD-3-Clause) as
|
|
5
|
+
``CVD_MATRICES_MACHADO2010``. They act on linear sRGB values.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
|
|
10
|
+
MACHADO_2009 = {
|
|
11
|
+
"protan": np.array([
|
|
12
|
+
[[1.000000, 0.000000, -0.000000], [0.000000, 1.000000, 0.000000], [-0.000000, -0.000000, 1.000000]], # 0.0
|
|
13
|
+
[[0.856167, 0.182038, -0.038205], [0.029342, 0.955115, 0.015544], [-0.002880, -0.001563, 1.004443]], # 0.1
|
|
14
|
+
[[0.734766, 0.334872, -0.069637], [0.051840, 0.919198, 0.028963], [-0.004928, -0.004209, 1.009137]], # 0.2
|
|
15
|
+
[[0.630323, 0.465641, -0.095964], [0.069181, 0.890046, 0.040773], [-0.006308, -0.007724, 1.014032]], # 0.3
|
|
16
|
+
[[0.539009, 0.579343, -0.118352], [0.082546, 0.866121, 0.051332], [-0.007136, -0.011959, 1.019095]], # 0.4
|
|
17
|
+
[[0.458064, 0.679578, -0.137642], [0.092785, 0.846313, 0.060902], [-0.007494, -0.016807, 1.024301]], # 0.5
|
|
18
|
+
[[0.385450, 0.769005, -0.154455], [0.100526, 0.829802, 0.069673], [-0.007442, -0.022190, 1.029632]], # 0.6
|
|
19
|
+
[[0.319627, 0.849633, -0.169261], [0.106241, 0.815969, 0.077790], [-0.007025, -0.028051, 1.035076]], # 0.7
|
|
20
|
+
[[0.259411, 0.923008, -0.182420], [0.110296, 0.804340, 0.085364], [-0.006276, -0.034346, 1.040622]], # 0.8
|
|
21
|
+
[[0.203876, 0.990338, -0.194214], [0.112975, 0.794542, 0.092483], [-0.005222, -0.041043, 1.046265]], # 0.9
|
|
22
|
+
[[0.152286, 1.052583, -0.204868], [0.114503, 0.786281, 0.099216], [-0.003882, -0.048116, 1.051998]], # 1.0
|
|
23
|
+
]),
|
|
24
|
+
"deutan": np.array([
|
|
25
|
+
[[1.000000, 0.000000, -0.000000], [0.000000, 1.000000, 0.000000], [-0.000000, -0.000000, 1.000000]], # 0.0
|
|
26
|
+
[[0.866435, 0.177704, -0.044139], [0.049567, 0.939063, 0.011370], [-0.003453, 0.007233, 0.996220]], # 0.1
|
|
27
|
+
[[0.760729, 0.319078, -0.079807], [0.090568, 0.889315, 0.020117], [-0.006027, 0.013325, 0.992702]], # 0.2
|
|
28
|
+
[[0.675425, 0.433850, -0.109275], [0.125303, 0.847755, 0.026942], [-0.007950, 0.018572, 0.989378]], # 0.3
|
|
29
|
+
[[0.605511, 0.528560, -0.134071], [0.155318, 0.812366, 0.032316], [-0.009376, 0.023176, 0.986200]], # 0.4
|
|
30
|
+
[[0.547494, 0.607765, -0.155259], [0.181692, 0.781742, 0.036566], [-0.010410, 0.027275, 0.983136]], # 0.5
|
|
31
|
+
[[0.498864, 0.674741, -0.173604], [0.205199, 0.754872, 0.039929], [-0.011131, 0.030969, 0.980162]], # 0.6
|
|
32
|
+
[[0.457771, 0.731899, -0.189670], [0.226409, 0.731012, 0.042579], [-0.011595, 0.034333, 0.977261]], # 0.7
|
|
33
|
+
[[0.422823, 0.781057, -0.203881], [0.245752, 0.709602, 0.044646], [-0.011843, 0.037423, 0.974421]], # 0.8
|
|
34
|
+
[[0.392952, 0.823610, -0.216562], [0.263559, 0.690210, 0.046232], [-0.011910, 0.040281, 0.971630]], # 0.9
|
|
35
|
+
[[0.367322, 0.860646, -0.227968], [0.280085, 0.672501, 0.047413], [-0.011820, 0.042940, 0.968881]], # 1.0
|
|
36
|
+
]),
|
|
37
|
+
"tritan": np.array([
|
|
38
|
+
[[1.000000, 0.000000, -0.000000], [0.000000, 1.000000, 0.000000], [-0.000000, -0.000000, 1.000000]], # 0.0
|
|
39
|
+
[[0.926670, 0.092514, -0.019184], [0.021191, 0.964503, 0.014306], [0.008437, 0.054813, 0.936750]], # 0.1
|
|
40
|
+
[[0.895720, 0.133330, -0.029050], [0.029997, 0.945400, 0.024603], [0.013027, 0.104707, 0.882266]], # 0.2
|
|
41
|
+
[[0.905871, 0.127791, -0.033662], [0.026856, 0.941251, 0.031893], [0.013410, 0.148296, 0.838294]], # 0.3
|
|
42
|
+
[[0.948035, 0.089490, -0.037526], [0.014364, 0.946792, 0.038844], [0.010853, 0.193991, 0.795156]], # 0.4
|
|
43
|
+
[[1.017277, 0.027029, -0.044306], [-0.006113, 0.958479, 0.047634], [0.006379, 0.248708, 0.744913]], # 0.5
|
|
44
|
+
[[1.104996, -0.046633, -0.058363], [-0.032137, 0.971635, 0.060503], [0.001336, 0.317922, 0.680742]], # 0.6
|
|
45
|
+
[[1.193214, -0.109812, -0.083402], [-0.058496, 0.979410, 0.079086], [-0.002346, 0.403492, 0.598854]], # 0.7
|
|
46
|
+
[[1.257728, -0.139648, -0.118081], [-0.078003, 0.975409, 0.102594], [-0.003316, 0.501214, 0.502102]], # 0.8
|
|
47
|
+
[[1.278864, -0.125333, -0.153531], [-0.084748, 0.957674, 0.127074], [-0.000989, 0.601151, 0.399838]], # 0.9
|
|
48
|
+
[[1.255528, -0.076749, -0.178779], [-0.078411, 0.930809, 0.147602], [0.004733, 0.691367, 0.303900]], # 1.0
|
|
49
|
+
]),
|
|
50
|
+
}
|
cmapkit/bounds.py
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
"""Lightness bounds that depend on hue.
|
|
2
|
+
|
|
3
|
+
How light or dark a colour may be before it stops working on a given
|
|
4
|
+
background depends on its hue, largely because of the shape of the sRGB
|
|
5
|
+
gamut: yellows and greens keep their colourfulness at high lightness, blues
|
|
6
|
+
and violets at low lightness. The bounds are tabulated against CAM16 hue,
|
|
7
|
+
interpolated around the hue circle, and blended towards conservative values
|
|
8
|
+
for colours of low chroma, whose hue is weak or undefined.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from dataclasses import dataclass
|
|
12
|
+
|
|
13
|
+
import numpy as np
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass(frozen=True)
|
|
17
|
+
class HueBounds:
|
|
18
|
+
hues: tuple # CAM16 hue angles of the table, degrees, increasing
|
|
19
|
+
light: tuple # largest admissible J' at each hue
|
|
20
|
+
dark: tuple # smallest admissible J' at each hue
|
|
21
|
+
grey_light: float # largest admissible J' for a colour of zero chroma
|
|
22
|
+
grey_dark: float # smallest admissible J' for a colour of zero chroma
|
|
23
|
+
chroma_ref: float = 20.0 # chroma at which the hue values apply in full
|
|
24
|
+
source: str = ""
|
|
25
|
+
|
|
26
|
+
def _interp(self, h, table):
|
|
27
|
+
hs = np.asarray(self.hues, float)
|
|
28
|
+
vs = np.asarray(table, float)
|
|
29
|
+
hs = np.concatenate([hs - 360, hs, hs + 360])
|
|
30
|
+
vs = np.concatenate([vs, vs, vs])
|
|
31
|
+
return np.interp(h, hs, vs)
|
|
32
|
+
|
|
33
|
+
def limits(self, u):
|
|
34
|
+
"""(J'_min, J'_max) for CAM16-UCS coordinates u (..., 3), whose first
|
|
35
|
+
coordinate may be scaled by 1/K_L (only a', b' are used)."""
|
|
36
|
+
a, b = u[..., 1], u[..., 2]
|
|
37
|
+
c = np.hypot(a, b)
|
|
38
|
+
h = np.degrees(np.arctan2(b, a)) % 360
|
|
39
|
+
w = np.clip(c / self.chroma_ref, 0.0, 1.0)
|
|
40
|
+
jmax = self.grey_light + w * (self._interp(h, self.light) - self.grey_light)
|
|
41
|
+
jmin = self.grey_dark + w * (self._interp(h, self.dark) - self.grey_dark)
|
|
42
|
+
return jmin, jmax
|
|
43
|
+
|
|
44
|
+
def slack(self, u, k_l):
|
|
45
|
+
"""Slack of the lower and upper bound, in J' units: (2, ...)."""
|
|
46
|
+
J = k_l * u[..., 0]
|
|
47
|
+
jmin, jmax = self.limits(u)
|
|
48
|
+
return np.stack([J - jmin, jmax - J])
|
|
49
|
+
|
|
50
|
+
def slack_grad(self, u, k_l, eps=1e-4):
|
|
51
|
+
"""Derivative of the slack with respect to u: (2, ..., 3)."""
|
|
52
|
+
g = np.zeros((2,) + u.shape)
|
|
53
|
+
g[0, ..., 0] = k_l
|
|
54
|
+
g[1, ..., 0] = -k_l
|
|
55
|
+
for ax in (1, 2):
|
|
56
|
+
up = u.copy()
|
|
57
|
+
dn = u.copy()
|
|
58
|
+
up[..., ax] += eps
|
|
59
|
+
dn[..., ax] -= eps
|
|
60
|
+
lo_u, hi_u = self.limits(up)
|
|
61
|
+
lo_d, hi_d = self.limits(dn)
|
|
62
|
+
g[0, ..., ax] = -(lo_u - lo_d) / (2 * eps)
|
|
63
|
+
g[1, ..., ax] = (hi_u - hi_d) / (2 * eps)
|
|
64
|
+
return g
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
# Judged by eye by one observer on a white background under the "screen"
|
|
68
|
+
# preset, at the strongest colourfulness sRGB allows (2026-11-22): first on
|
|
69
|
+
# absolute lightness rows, then corrected on rows relative to these bounds.
|
|
70
|
+
SRGB_WHITE = HueBounds(
|
|
71
|
+
hues=(0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330),
|
|
72
|
+
light=(70, 75, 80, 85, 90, 90, 90, 90, 80, 70, 70, 70),
|
|
73
|
+
dark=(35, 35, 50, 55, 55, 50, 55, 55, 50, 40, 35, 35),
|
|
74
|
+
grey_light=70.0,
|
|
75
|
+
grey_dark=50.0,
|
|
76
|
+
source="one observer, white background, screen preset, two passes",
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
# Black and dark grey (#333333) backgrounds with white text, judged by the
|
|
80
|
+
# same observer in the same way (2026-11-22). The light-side value at hue 300
|
|
81
|
+
# on black was missing from the first reading; 80 was filled in and then
|
|
82
|
+
# confirmed by the observer.
|
|
83
|
+
SRGB_BLACK = HueBounds(
|
|
84
|
+
hues=(0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330),
|
|
85
|
+
light=(85, 80, 85, 90, 95, 95, 95, 95, 85, 80, 80, 85),
|
|
86
|
+
dark=(50, 50, 55, 65, 65, 60, 65, 65, 60, 55, 50, 50),
|
|
87
|
+
grey_light=80.0,
|
|
88
|
+
grey_dark=65.0,
|
|
89
|
+
source="one observer, black background, screen preset",
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
SRGB_GREY33 = HueBounds(
|
|
93
|
+
hues=(0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330),
|
|
94
|
+
light=(85, 85, 85, 90, 95, 95, 95, 95, 85, 80, 80, 85),
|
|
95
|
+
dark=(45, 45, 55, 65, 65, 55, 60, 60, 55, 50, 50, 45),
|
|
96
|
+
grey_light=80.0,
|
|
97
|
+
grey_dark=65.0,
|
|
98
|
+
source="one observer, #333333 background, screen preset",
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
# Backgrounds for which the sRGB mode is calibrated.
|
|
102
|
+
SRGB_BOUNDS = {"#ffffff": SRGB_WHITE, "#000000": SRGB_BLACK, "#333333": SRGB_GREY33}
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _lstar(hexcolour):
|
|
106
|
+
from .space import hex_to_rgb, relative_luminance
|
|
107
|
+
y = float(relative_luminance(hex_to_rgb(hexcolour))[0])
|
|
108
|
+
return 116 * np.cbrt(y) - 16 if y > 216 / 24389 else y * 24389 / 27
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _jbg(hexcolour, preset="screen"):
|
|
112
|
+
"""Lightness J' of a background in its own viewing conditions."""
|
|
113
|
+
from .space import Space, hex_to_rgb, viewing
|
|
114
|
+
return float(Space(viewing(preset, hexcolour)).coords(hex_to_rgb(hexcolour))[0, 0, 0])
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _interp_hue(h, hues, table):
|
|
118
|
+
hs = np.asarray(hues, float)
|
|
119
|
+
vs = np.asarray(table, float)
|
|
120
|
+
return np.interp(h, np.concatenate([hs - 360, hs, hs + 360]), np.concatenate([vs, vs, vs]))
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
@dataclass(frozen=True)
|
|
124
|
+
class BandBounds:
|
|
125
|
+
"""Two-sided lightness bounds for one background.
|
|
126
|
+
|
|
127
|
+
A colour lies either darker or lighter than the background (its side).
|
|
128
|
+
On the darker side it must lie between the floor, below which it reads as
|
|
129
|
+
black, and j_bg - margin; on the lighter side, between j_bg + margin and
|
|
130
|
+
the ceiling, above which it reads as white. Floor, ceiling and margin
|
|
131
|
+
depend on hue and are blended towards their grey values at low chroma.
|
|
132
|
+
"""
|
|
133
|
+
|
|
134
|
+
hues: tuple
|
|
135
|
+
floor: tuple
|
|
136
|
+
ceil: tuple
|
|
137
|
+
margin: tuple
|
|
138
|
+
grey_floor: float
|
|
139
|
+
grey_ceil: float
|
|
140
|
+
grey_margin: float
|
|
141
|
+
j_bg: float
|
|
142
|
+
chroma_ref: float = 20.0
|
|
143
|
+
source: str = ""
|
|
144
|
+
|
|
145
|
+
def _values(self, u):
|
|
146
|
+
a, b = u[..., 1], u[..., 2]
|
|
147
|
+
w = np.clip(np.hypot(a, b) / self.chroma_ref, 0.0, 1.0)
|
|
148
|
+
h = np.degrees(np.arctan2(b, a)) % 360
|
|
149
|
+
blend = lambda grey, tab: grey + w * (_interp_hue(h, self.hues, tab) - grey) # noqa: E731
|
|
150
|
+
return (blend(self.grey_floor, self.floor), blend(self.grey_ceil, self.ceil),
|
|
151
|
+
blend(self.grey_margin, self.margin))
|
|
152
|
+
|
|
153
|
+
def side_of(self, u, k_l):
|
|
154
|
+
return np.where(k_l * u[..., 0] >= self.j_bg, 1.0, -1.0)
|
|
155
|
+
|
|
156
|
+
def slack(self, u, k_l, side):
|
|
157
|
+
"""Slack of the two conditions on the colour's side, J' units: (2, n)."""
|
|
158
|
+
J = k_l * u[..., 0]
|
|
159
|
+
f, c, m = self._values(u)
|
|
160
|
+
lower = np.where(side > 0, J - (self.j_bg + m), J - f)
|
|
161
|
+
upper = np.where(side > 0, c - J, (self.j_bg - m) - J)
|
|
162
|
+
return np.stack([lower, upper])
|
|
163
|
+
|
|
164
|
+
def slack_grad(self, u, k_l, side, eps=1e-4):
|
|
165
|
+
g = np.zeros((2,) + u.shape)
|
|
166
|
+
g[0, ..., 0] = k_l
|
|
167
|
+
g[1, ..., 0] = -k_l
|
|
168
|
+
for ax in (1, 2):
|
|
169
|
+
up, dn = u.copy(), u.copy()
|
|
170
|
+
up[..., ax] += eps
|
|
171
|
+
dn[..., ax] -= eps
|
|
172
|
+
fu, cu, mu = self._values(up)
|
|
173
|
+
fd, cd, md = self._values(dn)
|
|
174
|
+
df, dc, dm = (fu - fd) / (2 * eps), (cu - cd) / (2 * eps), (mu - md) / (2 * eps)
|
|
175
|
+
g[0, ..., ax] = np.where(side > 0, -dm, -df)
|
|
176
|
+
g[1, ..., ax] = np.where(side > 0, dc, -dm)
|
|
177
|
+
return g
|
|
178
|
+
|
|
179
|
+
def feasible(self, u, k_l):
|
|
180
|
+
"""True where a colour satisfies the conditions on either side."""
|
|
181
|
+
ok = [self.slack(u, k_l, np.full(u.shape[:-1], s)).min(axis=0) >= 0 for s in (-1, 1)]
|
|
182
|
+
return ok[0] | ok[1]
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def bounds_for(background, j_bg=None):
|
|
186
|
+
"""Two-sided bounds for a background given as '#rrggbb'.
|
|
187
|
+
|
|
188
|
+
floor: where colours read as black, from the dark side on white.
|
|
189
|
+
ceil: where colours read as white, from the light side on black and on
|
|
190
|
+
#333333, interpolated between them in background L* and held beyond.
|
|
191
|
+
margin: the least lightness difference from the background, from the
|
|
192
|
+
background side of each calibration (white: 100 - light; black and
|
|
193
|
+
#333333: dark - J' of the background), interpolated in background L*.
|
|
194
|
+
On the three calibrated backgrounds this reproduces the measured tables.
|
|
195
|
+
Elsewhere it is an educated guess, and its source says so."""
|
|
196
|
+
cal = {k: (_lstar(k), _jbg(k), v) for k, v in SRGB_BOUNDS.items()}
|
|
197
|
+
Lw, Jw, W = cal["#ffffff"]
|
|
198
|
+
Lk, Jk, K = cal["#000000"]
|
|
199
|
+
Lg, Jg, G = cal["#333333"]
|
|
200
|
+
L = _lstar(background)
|
|
201
|
+
j_bg = _jbg(background) if j_bg is None else j_bg
|
|
202
|
+
margins = [(Lk, np.subtract(K.dark, Jk), K.grey_dark - Jk),
|
|
203
|
+
(Lg, np.subtract(G.dark, Jg), G.grey_dark - Jg),
|
|
204
|
+
(Lw, np.subtract(100.0, W.light), 100.0 - W.grey_light)]
|
|
205
|
+
xs = [m[0] for m in margins]
|
|
206
|
+
margin = tuple(float(np.interp(L, xs, [m[1][i] for m in margins]))
|
|
207
|
+
for i in range(len(W.hues)))
|
|
208
|
+
grey_margin = float(np.interp(L, xs, [m[2] for m in margins]))
|
|
209
|
+
ceil = tuple(float(np.interp(L, [Lk, Lg], [k, g])) for k, g in zip(K.light, G.light))
|
|
210
|
+
grey_ceil = float(np.interp(L, [Lk, Lg], [K.grey_light, G.grey_light]))
|
|
211
|
+
calibrated = background in SRGB_BOUNDS
|
|
212
|
+
src = (SRGB_BOUNDS[background].source if calibrated else
|
|
213
|
+
f"interpolated for {background} (L* {L:.1f}) from the calibrations on "
|
|
214
|
+
"#000000, #333333 and #ffffff")
|
|
215
|
+
return BandBounds(hues=W.hues, floor=W.dark, ceil=ceil, margin=margin,
|
|
216
|
+
grey_floor=W.grey_dark, grey_ceil=grey_ceil, grey_margin=grey_margin,
|
|
217
|
+
j_bg=j_bg, source=src)
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
def image_bounds(view, floor_from="#ffffff", ceil_from="#333333"):
|
|
221
|
+
"""Floor and ceiling, without a background margin, for colours in an
|
|
222
|
+
image viewed under `view`.
|
|
223
|
+
|
|
224
|
+
The floor is the dark limit measured on white and the ceiling the light
|
|
225
|
+
limit measured on #333333 (the values bounds_for uses for a mid-grey
|
|
226
|
+
background). The measurements are J' values in the viewing conditions of
|
|
227
|
+
their own backgrounds, so they are carried over through the colours
|
|
228
|
+
themselves: at each tabulated hue, the colour shown at the limit (the
|
|
229
|
+
strongest colourfulness sRGB allows there) is re-expressed in `view`. The
|
|
230
|
+
grey values are carried over through the grey of the same J'."""
|
|
231
|
+
from .calibrate import HUES, _gamut, ring_at_lightness
|
|
232
|
+
from .space import Space, hex_to_rgb, viewing
|
|
233
|
+
|
|
234
|
+
target = Space(view)
|
|
235
|
+
|
|
236
|
+
def carry(table, grey, background, side):
|
|
237
|
+
src = viewing("screen", background)
|
|
238
|
+
gamut = _gamut(src)
|
|
239
|
+
out = []
|
|
240
|
+
for k, (hue, J) in enumerate(zip(table.hues, getattr(table, side))):
|
|
241
|
+
assert HUES[k] == hue
|
|
242
|
+
c = ring_at_lightness(J, src, 1.0, gamut=gamut)[k]
|
|
243
|
+
out.append(float(target.coords(hex_to_rgb(c))[0, 0, 0]))
|
|
244
|
+
rgb, Js = gamut[0], gamut[1]
|
|
245
|
+
greys = rgb[np.ptp(rgb, axis=1) == 0]
|
|
246
|
+
jg = Space(src).coords(greys)[0, :, 0]
|
|
247
|
+
g = greys[np.argmin(np.abs(jg - grey))]
|
|
248
|
+
return tuple(out), float(target.coords(g)[0, 0, 0])
|
|
249
|
+
|
|
250
|
+
F, gF = carry(SRGB_BOUNDS[floor_from], SRGB_BOUNDS[floor_from].grey_dark, floor_from, "dark")
|
|
251
|
+
C, gC = carry(SRGB_BOUNDS[ceil_from], SRGB_BOUNDS[ceil_from].grey_light, ceil_from, "light")
|
|
252
|
+
# With j_bg = 0 every colour lies on the light side, and a margin equal to
|
|
253
|
+
# the floor makes the lower condition J' >= F(h).
|
|
254
|
+
return BandBounds(hues=SRGB_WHITE.hues, floor=F, ceil=C, margin=F, grey_floor=gF,
|
|
255
|
+
grey_ceil=gC, grey_margin=gF, j_bg=0.0,
|
|
256
|
+
source=f"image: floor from {floor_from}, ceiling from {ceil_from}, "
|
|
257
|
+
"carried over to the image's viewing conditions")
|
cmapkit/calibrate.py
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
"""Calibration pages for judging limits by eye.
|
|
2
|
+
|
|
3
|
+
Each page shows rows of colours at a series of lightness values J'. Within a
|
|
4
|
+
row, colours span a ring of hues at the strongest colourfulness sRGB allows
|
|
5
|
+
at that lightness and hue, and again at half of it. The reader picks the row
|
|
6
|
+
at which the colours stop being satisfactory.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import numpy as np
|
|
10
|
+
|
|
11
|
+
from .space import Space, hex_to_rgb, relative_luminance, rgb_to_hex, viewing
|
|
12
|
+
|
|
13
|
+
HUES = np.arange(0, 360, 30) # twelve hues
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _gamut(view, steps=64):
|
|
17
|
+
g = np.linspace(0, 1, steps)
|
|
18
|
+
rgb = np.stack(np.meshgrid(g, g, g, indexing="ij"), -1).reshape(-1, 3)
|
|
19
|
+
u = Space(view).coords(rgb)[0]
|
|
20
|
+
J = u[:, 0]
|
|
21
|
+
C = np.hypot(u[:, 1], u[:, 2])
|
|
22
|
+
h = np.degrees(np.arctan2(u[:, 2], u[:, 1])) % 360
|
|
23
|
+
return rgb, J, C, h
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def ring_at_lightness(J_target, view, frac=1.0, tol=1.0, hue_tol=6.0, gamut=None):
|
|
27
|
+
"""One colour per hue at lightness J_target, with chroma frac times the
|
|
28
|
+
largest available at that lightness and hue. None where no colour fits."""
|
|
29
|
+
rgb, J, C, h = gamut if gamut is not None else _gamut(view)
|
|
30
|
+
out = []
|
|
31
|
+
for hue in HUES:
|
|
32
|
+
dh = np.abs((h - hue + 180) % 360 - 180)
|
|
33
|
+
m = (np.abs(J - J_target) < tol) & (dh < hue_tol)
|
|
34
|
+
if not m.any():
|
|
35
|
+
out.append(None)
|
|
36
|
+
continue
|
|
37
|
+
cmax = C[m].max()
|
|
38
|
+
idx = np.flatnonzero(m)
|
|
39
|
+
k = idx[np.argmin(np.abs(C[idx] - frac * cmax) + 0.2 * dh[idx])]
|
|
40
|
+
out.append(rgb_to_hex(rgb[k])[0])
|
|
41
|
+
return out
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def lightness_page(side="high", background="#ffffff", text="#000000", levels=None,
|
|
45
|
+
path=None, preset="screen"):
|
|
46
|
+
"""Rows of colours at increasing (side='high') or decreasing ('low')
|
|
47
|
+
lightness. Each row: thin lines, thick lines, markers and patches, at full
|
|
48
|
+
and half colourfulness."""
|
|
49
|
+
import matplotlib.pyplot as plt
|
|
50
|
+
from matplotlib.patches import Rectangle
|
|
51
|
+
|
|
52
|
+
view = viewing(preset, background)
|
|
53
|
+
gamut = _gamut(view)
|
|
54
|
+
jb = Space(view).coords(hex_to_rgb(background))[0, 0, 0]
|
|
55
|
+
dark_bg = relative_luminance(hex_to_rgb(background))[0] < 0.18
|
|
56
|
+
# The side facing the background limits visibility; the other side is
|
|
57
|
+
# where colours turn into the text colour, shown as a reference line.
|
|
58
|
+
toward_text = (side == "low") != dark_bg
|
|
59
|
+
if levels is None:
|
|
60
|
+
# Rows run from 5 to 50 units in from the lighter (or darker) of the
|
|
61
|
+
# background and text colours, in steps of 5, on a grid of 5.
|
|
62
|
+
jt = Space(view).coords(hex_to_rgb(text))[0, 0, 0]
|
|
63
|
+
if side == "high":
|
|
64
|
+
top = 5 * np.floor((max(jb, jt) - 5) / 5)
|
|
65
|
+
levels = np.arange(top - 45, top + 1, 5)
|
|
66
|
+
else:
|
|
67
|
+
bottom = 5 * np.ceil((min(jb, jt) + 5) / 5)
|
|
68
|
+
levels = np.arange(bottom + 45, bottom - 1, -5)
|
|
69
|
+
rows = len(levels)
|
|
70
|
+
fig, ax = plt.subplots(figsize=(13, 0.85 * rows * 2 + 1))
|
|
71
|
+
fig.patch.set_facecolor(background)
|
|
72
|
+
ax.set_facecolor(background)
|
|
73
|
+
x = np.linspace(0, 1, 40)
|
|
74
|
+
y = 0.0
|
|
75
|
+
for J in levels:
|
|
76
|
+
for frac, tag in ((1.0, "full"), (0.5, "half")):
|
|
77
|
+
cols = ring_at_lightness(J, view, frac, gamut=gamut)
|
|
78
|
+
for k, c in enumerate(cols):
|
|
79
|
+
if c is None:
|
|
80
|
+
continue
|
|
81
|
+
x0 = k * 1.0
|
|
82
|
+
ax.plot(x0 + 0.05 + 0.4 * x, y + 0.15 + 0.08 * np.sin(8 * x), color=c, lw=0.8)
|
|
83
|
+
ax.plot(x0 + 0.05 + 0.4 * x, y - 0.05 + 0.08 * np.sin(8 * x), color=c, lw=2.0)
|
|
84
|
+
ax.scatter(x0 + np.array([0.55, 0.62, 0.69]), [y + 0.12, y - 0.02, y + 0.12],
|
|
85
|
+
s=8, color=c, linewidths=0)
|
|
86
|
+
ax.add_patch(Rectangle((x0 + 0.76, y - 0.2), 0.2, 0.42, facecolor=c,
|
|
87
|
+
edgecolor="none"))
|
|
88
|
+
if toward_text:
|
|
89
|
+
ax.plot(len(HUES) + 0.05 + 0.4 * x, y + 0.05 + 0.08 * np.sin(8 * x),
|
|
90
|
+
color=text, lw=2.0)
|
|
91
|
+
label = f"J' {J:g} (ΔJ' {abs(jb - J):.0f}) {tag}"
|
|
92
|
+
ax.text(-0.15, y, label, ha="right", va="center", fontsize=8, color=text)
|
|
93
|
+
y -= 0.6
|
|
94
|
+
y -= 0.25
|
|
95
|
+
ax.set_xlim(-2.6, len(HUES) + (0.6 if toward_text else 0))
|
|
96
|
+
ax.set_ylim(y + 0.3, 0.5)
|
|
97
|
+
ax.axis("off")
|
|
98
|
+
extreme = "lightest" if side == "high" else "darkest"
|
|
99
|
+
if toward_text:
|
|
100
|
+
title = (f"Text side: {extreme} row where colours still read as colours, not as the "
|
|
101
|
+
"text colour (line at right)")
|
|
102
|
+
else:
|
|
103
|
+
title = f"Background side: {extreme} row where every colour is still clearly visible"
|
|
104
|
+
title += f" [background {background}]"
|
|
105
|
+
ax.set_title(title, fontsize=10, color=text)
|
|
106
|
+
fig.tight_layout()
|
|
107
|
+
if path:
|
|
108
|
+
fig.savefig(path, dpi=120, facecolor=background)
|
|
109
|
+
plt.close(fig)
|
|
110
|
+
return fig
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def colour_at(J_target, hue, view=None, frac=1.0, tol=1.0, hue_tol=6.0, gamut=None):
|
|
114
|
+
"""The colour at lightness J_target and the given hue with chroma frac
|
|
115
|
+
times the largest available; None if none fits."""
|
|
116
|
+
rgb, J, C, h = gamut if gamut is not None else _gamut(view)
|
|
117
|
+
dh = np.abs((h - hue + 180) % 360 - 180)
|
|
118
|
+
m = (np.abs(J - J_target) < tol) & (dh < hue_tol)
|
|
119
|
+
if not m.any():
|
|
120
|
+
return None
|
|
121
|
+
idx = np.flatnonzero(m)
|
|
122
|
+
cmax = C[idx].max()
|
|
123
|
+
k = idx[np.argmin(np.abs(C[idx] - frac * cmax) + 0.2 * dh[idx])]
|
|
124
|
+
return rgb_to_hex(rgb[k])[0]
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def bounds_page(bounds, side="high", offsets=None, background="#ffffff", text="#000000",
|
|
128
|
+
path=None, preset="screen"):
|
|
129
|
+
"""Rows at a fixed offset from each hue's own bound. If the bounds are
|
|
130
|
+
right, the boundary between acceptable and unacceptable is the row at
|
|
131
|
+
offset 0 in every column."""
|
|
132
|
+
import matplotlib.pyplot as plt
|
|
133
|
+
from matplotlib.patches import Rectangle
|
|
134
|
+
|
|
135
|
+
view = viewing(preset, background)
|
|
136
|
+
gamut = _gamut(view)
|
|
137
|
+
dark_bg = relative_luminance(hex_to_rgb(background))[0] < 0.18
|
|
138
|
+
toward_text = (side == "low") != dark_bg
|
|
139
|
+
hues = np.asarray(bounds.hues, float)
|
|
140
|
+
base = np.asarray(bounds.light if side == "high" else bounds.dark, float)
|
|
141
|
+
if offsets is None:
|
|
142
|
+
offsets = (np.arange(-15, 15, 5) if side == "high" else np.arange(15, -15, -5))
|
|
143
|
+
# Light side: rows go from darker (safe) to lighter. Dark side: from
|
|
144
|
+
# lighter (safe) to darker.
|
|
145
|
+
rows = list(offsets) if side == "high" else list(offsets)
|
|
146
|
+
fig, ax = plt.subplots(figsize=(13, 0.62 * len(rows) + 1.4))
|
|
147
|
+
fig.patch.set_facecolor(background)
|
|
148
|
+
ax.set_facecolor(background)
|
|
149
|
+
x = np.linspace(0, 1, 40)
|
|
150
|
+
for r, off in enumerate(rows):
|
|
151
|
+
y = -0.7 * r
|
|
152
|
+
for k, (hue, b) in enumerate(zip(hues, base)):
|
|
153
|
+
c = colour_at(b + off, hue, frac=1.0, gamut=gamut)
|
|
154
|
+
if c is None:
|
|
155
|
+
continue
|
|
156
|
+
ax.plot(k + 0.05 + 0.4 * x, y + 0.15 + 0.08 * np.sin(8 * x), color=c, lw=0.8)
|
|
157
|
+
ax.plot(k + 0.05 + 0.4 * x, y - 0.05 + 0.08 * np.sin(8 * x), color=c, lw=2.0)
|
|
158
|
+
ax.scatter(k + np.array([0.55, 0.62, 0.69]), [y + 0.12, y - 0.02, y + 0.12], s=8,
|
|
159
|
+
color=c, linewidths=0)
|
|
160
|
+
ax.add_patch(Rectangle((k + 0.76, y - 0.2), 0.2, 0.42, facecolor=c,
|
|
161
|
+
edgecolor="none"))
|
|
162
|
+
ax.text(k + 0.5, y - 0.3, f"{b + off:g}", fontsize=6, ha="center", color="#888888")
|
|
163
|
+
if toward_text:
|
|
164
|
+
ax.plot(len(hues) + 0.05 + 0.4 * x, y + 0.05 + 0.08 * np.sin(8 * x), color=text,
|
|
165
|
+
lw=2.0)
|
|
166
|
+
mark = " (present bound)" if off == 0 else ""
|
|
167
|
+
ax.text(-0.15, y, f"bound {off:+g}{mark}", ha="right", va="center", fontsize=8,
|
|
168
|
+
color=text)
|
|
169
|
+
for k, hue in enumerate(hues):
|
|
170
|
+
ax.text(k + 0.5, 0.45, f"h {hue:g}", ha="center", fontsize=7, color=text)
|
|
171
|
+
ax.set_xlim(-2.4, len(hues) + (0.6 if toward_text else 0))
|
|
172
|
+
ax.set_ylim(-0.7 * len(rows) + 0.2, 0.65)
|
|
173
|
+
ax.axis("off")
|
|
174
|
+
title = (("Light" if side == "high" else "Dark") + " side, relative to each hue's bound"
|
|
175
|
+
f" (grey numbers: J') [background {background}]")
|
|
176
|
+
ax.set_title(title, fontsize=10, color=text)
|
|
177
|
+
fig.tight_layout()
|
|
178
|
+
if path:
|
|
179
|
+
fig.savefig(path, dpi=120, facecolor=background)
|
|
180
|
+
plt.close(fig)
|
|
181
|
+
return fig
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"""hdrmaps: colour maps for images of high dynamic range.
|
|
2
|
+
|
|
3
|
+
A map for such an image must be long (a large total colour difference from
|
|
4
|
+
one end to the other), so that small differences in the data show at every
|
|
5
|
+
level, and uniform, so that equal differences look equal. The maps of this
|
|
6
|
+
subpackage are paths through the colours of a large MCCP palette, made
|
|
7
|
+
smooth and kept from coming near themselves by a curve optimiser, and
|
|
8
|
+
uniform for observers of normal and deficient colour vision. The length a
|
|
9
|
+
map needs is set by the noise of the image (noise.py). The method and its
|
|
10
|
+
limits are given in docs/hdrmaps.md.
|
|
11
|
+
|
|
12
|
+
The main functions:
|
|
13
|
+
|
|
14
|
+
- names, info, path_map, colormap: the shipped maps (library.py);
|
|
15
|
+
- render: an image in colour through a map, without blur or, on request,
|
|
16
|
+
with a blur matched to its noise (library.py);
|
|
17
|
+
- assess: the length, uniformity and separation of any map for each
|
|
18
|
+
observer (measure.py);
|
|
19
|
+
- scale, noise_sd, noise_range, required_length, blur_for_length,
|
|
20
|
+
map_length: the scaling of an image and the noise model (noise.py).
|
|
21
|
+
|
|
22
|
+
The modules paths and curve make new maps (tools/make_hdrmaps.py shows
|
|
23
|
+
how).
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
from .library import colormap, info, names, path_map, render
|
|
27
|
+
from .measure import assess
|
|
28
|
+
from .noise import blur_for_length, map_length, noise_range, noise_sd, required_length, scale
|
|
29
|
+
from .paths import IMAGE_VIEW, PATH_VIEW
|
|
30
|
+
|
|
31
|
+
__all__ = ["IMAGE_VIEW", "PATH_VIEW", "assess", "blur_for_length", "colormap", "info",
|
|
32
|
+
"map_length", "names", "noise_range", "noise_sd", "path_map", "render",
|
|
33
|
+
"required_length", "scale"]
|