Skip to content

Commit 0ab458b

Browse files
committed
CAM16: Small improvement from Schlömer paper not originally included
1 parent 3890453 commit 0ab458b

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

coloraide/spaces/cam16_jmh.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,21 @@
2222
M16 = CAT16.MATRIX
2323
MI6_INV = alg.inv(M16)
2424

25+
# Calculate `[Ra', Ga', Ba']` from `[P2', a, b]`
2526
M1 = [
2627
[460.0, 451.0, 288.0],
2728
[460.0, -891.0, -261.0],
2829
[460.0, -220.0, -6300.0]
2930
]
3031

32+
# Calculate `[P2' a, b, u]` from `[Ra', Ga', Ba']`
33+
M2 = [
34+
[2.0, 1.0, 1 / 20],
35+
[1.0, -12 / 11, 1 / 11],
36+
[1.0 / 9, 1/9, -2 / 9],
37+
[1.0, 1.0, 21 / 20]
38+
]
39+
3140
ADAPTED_COEF = 0.42
3241
ADAPTED_COEF_INV = 1 / ADAPTED_COEF
3342

@@ -261,7 +270,7 @@ def cam16_to_xyz_d65(
261270
# Calculate red-green and yellow-blue components
262271
p1 = 5e4 / 13 * env.nc * env.ncb * et
263272
p2 = A / env.nbb
264-
r = 23 * (p2 + 0.305) * alg.zdiv(t, 23 * p1 + t * (11 * cos_h + 108 * sin_h))
273+
r = alg.zdiv(23 * (p2 + 0.305) * t, 23 * p1 + t * (11 * cos_h + 108 * sin_h))
265274
a = r * cos_h
266275
b = r * sin_h
267276

@@ -283,22 +292,19 @@ def xyz_d65_to_cam16(xyzd65: Vector, env: Environment, calc_hue_quadrature: bool
283292
env.fl
284293
)
285294

295+
p1, a, b, u = alg.matmul(M2, rgb_a, dims=alg.D2_D1)
296+
286297
# Calculate hue from red-green and yellow-blue components
287-
a = rgb_a[0] + (-12 * rgb_a[1] + rgb_a[2]) / 11
288-
b = (rgb_a[0] + rgb_a[1] - 2 * rgb_a[2]) / 9
289298
h_rad = math.atan2(b, a) % math.tau
290299

291300
# Eccentricity
292301
et = 0.25 * (math.cos(h_rad + 2) + 3.8)
293302

294-
t = (
295-
5e4 / 13 * env.nc * env.ncb *
296-
alg.zdiv(et * math.sqrt(a ** 2 + b ** 2), rgb_a[0] + rgb_a[1] + 1.05 * rgb_a[2] + 0.305)
297-
)
303+
t = 5e4 / 13 * env.nc * env.ncb * alg.zdiv(et * math.hypot(a, b), u + 0.305)
298304
alpha = alg.spow(t, 0.9) * math.pow(1.64 - math.pow(0.29, env.n), 0.73)
299305

300306
# Achromatic response
301-
A = env.nbb * (2 * rgb_a[0] + rgb_a[1] + 0.05 * rgb_a[2])
307+
A = env.nbb * p1
302308

303309
J_root = alg.spow(A / env.a_w, 0.5 * env.c * env.z)
304310

0 commit comments

Comments
 (0)