| 1 |
import helmlab from "../spaces/helmlab.js"; |
| 2 |
import getColor from "../getColor.js"; |
| 3 |
|
| 4 |
// Helmlab MetricSpace weighted distance (v21, 72 params). |
| 5 |
// Optimized on COMBVD human color-difference judgments. |
| 6 |
// |
| 7 |
// Formula: |
| 8 |
// SL = 1 + sl * (L_avg - 0.5)² |
| 9 |
// SC = 1 + sc * C_avg |
| 10 |
// raw = (ΔL²/SL² + wC * Δab²/SC²) ^ (p/2) |
| 11 |
// compressed = raw / (1 + c * raw) |
| 12 |
// ΔE = compressed ^ q |
| 13 |
|
| 14 |
const sl = -0.9155125151657894; |
| 15 |
const sc = 2.9268353744941558; |
| 16 |
const wC = 3.966003089807536; |
| 17 |
const p = 1.9737081170404969; |
| 18 |
const compress = 52.473130649294724; |
| 19 |
const q = 0.47897301074925214; |
| 20 |
|
| 21 |
/** |
| 22 |
* @param {import("../types.js").ColorTypes} color |
| 23 |
* @param {import("../types.js").ColorTypes} sample |
| 24 |
* @returns {number} |
| 25 |
*/ |
| 26 |
export default function (color, sample) { |
| 27 |
[color, sample] = getColor([color, sample]); |
| 28 |
|
| 29 |
let [L1, a1, b1] = helmlab.from(color); |
| 30 |
let [L2, a2, b2] = helmlab.from(sample); |
| 31 |
|
| 32 |
let ΔL = L1 - L2; |
| 33 |
let Δa = a1 - a2; |
| 34 |
let Δb = b1 - b2; |
| 35 |
|
| 36 |
// Pair-dependent weighting |
| 37 |
let Lavg = (L1 + L2) * 0.5; |
| 38 |
let SL = 1 + sl * (Lavg - 0.5) ** 2; |
| 39 |
|
| 40 |
let C1 = Math.sqrt(a1 ** 2 + b1 ** 2); |
| 41 |
let C2 = Math.sqrt(a2 ** 2 + b2 ** 2); |
| 42 |
let Cavg = (C1 + C2) * 0.5; |
| 43 |
let SC = 1 + sc * Cavg; |
| 44 |
|
| 45 |
// Weighted Minkowski distance |
| 46 |
let raw = (ΔL ** 2 / SL ** 2 + wC * (Δa ** 2 + Δb ** 2) / SC ** 2) ** (p / 2); |
| 47 |
|
| 48 |
// Monotonic compression |
| 49 |
let compressed = raw / (1 + compress * raw); |
| 50 |
|
| 51 |
return compressed ** q; |
| 52 |
} |