| 1 |
import jzczhz from "../spaces/jzczhz.js"; |
| 2 |
import getColor from "../getColor.js"; |
| 3 |
import { isNone } from "../util.js"; |
| 4 |
|
| 5 |
/** |
| 6 |
* More accurate color-difference formulae |
| 7 |
* than the simple 1976 Euclidean distance in Lab |
| 8 |
* |
| 9 |
* Uses JzCzHz, which has improved perceptual uniformity |
| 10 |
* and thus a simple Euclidean root-sum of ΔL² ΔC² ΔH² |
| 11 |
* gives good results. |
| 12 |
* @param {import("../types.js").ColorTypes} color |
| 13 |
* @param {import("../types.js").ColorTypes} sample |
| 14 |
* @returns {number} |
| 15 |
*/ |
| 16 |
export default function (color, sample) { |
| 17 |
[color, sample] = getColor([color, sample]); |
| 18 |
|
| 19 |
// Given this color as the reference |
| 20 |
// and a sample, |
| 21 |
// calculate deltaE in JzCzHz. |
| 22 |
let [Jz1, Cz1, Hz1] = jzczhz.from(color); |
| 23 |
let [Jz2, Cz2, Hz2] = jzczhz.from(sample); |
| 24 |
|
| 25 |
// Lightness and Chroma differences |
| 26 |
// sign does not matter as they are squared. |
| 27 |
let ΔJ = Jz1 - Jz2; |
| 28 |
let ΔC = Cz1 - Cz2; |
| 29 |
|
| 30 |
// length of chord for ΔH |
| 31 |
if (isNone(Hz1) && isNone(Hz2)) { |
| 32 |
// both undefined hues |
| 33 |
Hz1 = 0; |
| 34 |
Hz2 = 0; |
| 35 |
} |
| 36 |
else if (isNone(Hz1)) { |
| 37 |
// one undefined, set to the defined hue |
| 38 |
Hz1 = Hz2; |
| 39 |
} |
| 40 |
else if (isNone(Hz2)) { |
| 41 |
Hz2 = Hz1; |
| 42 |
} |
| 43 |
|
| 44 |
let Δh = Hz1 - Hz2; |
| 45 |
let ΔH = 2 * Math.sqrt(Cz1 * Cz2) * Math.sin((Δh / 2) * (Math.PI / 180)); |
| 46 |
|
| 47 |
return Math.sqrt(ΔJ ** 2 + ΔC ** 2 + ΔH ** 2); |
| 48 |
} |