| 1 |
import lab from "../spaces/lab.js"; |
| 2 |
import lch from "../spaces/lch.js"; |
| 3 |
import getColor from "../getColor.js"; |
| 4 |
import { isNone } from "../util.js"; |
| 5 |
|
| 6 |
// More accurate color-difference formulae |
| 7 |
// than the simple 1976 Euclidean distance in Lab |
| 8 |
|
| 9 |
// CMC by the Color Measurement Committee of the |
| 10 |
// Bradford Society of Dyeists and Colorsts, 1994. |
| 11 |
// Uses LCH rather than Lab, |
| 12 |
// with different weights for L, C and H differences |
| 13 |
// A nice increase in accuracy for modest increase in complexity |
| 14 |
const π = Math.PI; |
| 15 |
const d2r = π / 180; |
| 16 |
|
| 17 |
/** |
| 18 |
* @param {import("../types.js").ColorTypes} color |
| 19 |
* @param {import("../types.js").ColorTypes} sample |
| 20 |
* @param {{ l?: number | undefined; c?: number | undefined }} options |
| 21 |
* @returns {number} |
| 22 |
*/ |
| 23 |
export default function (color, sample, { l = 2, c = 1 } = {}) { |
| 24 |
[color, sample] = getColor([color, sample]); |
| 25 |
|
| 26 |
// Given this color as the reference |
| 27 |
// and a sample, |
| 28 |
// calculate deltaE CMC. |
| 29 |
|
| 30 |
// This implementation assumes the parametric |
| 31 |
// weighting factors l:c are 2:1 |
| 32 |
// which is typical for non-textile uses. |
| 33 |
|
| 34 |
let [L1, a1, b1] = lab.from(color); |
| 35 |
let [, C1, H1] = lch.from(lab, [L1, a1, b1]); |
| 36 |
let [L2, a2, b2] = lab.from(sample); |
| 37 |
let C2 = lch.from(lab, [L2, a2, b2])[1]; |
| 38 |
|
| 39 |
// let [L1, a1, b1] = color.getAll(lab); |
| 40 |
// let C1 = color.get("lch.c"); |
| 41 |
// let H1 = color.get("lch.h"); |
| 42 |
// let [L2, a2, b2] = sample.getAll(lab); |
| 43 |
// let C2 = sample.get("lch.c"); |
| 44 |
|
| 45 |
// Check for negative Chroma, |
| 46 |
// which might happen through |
| 47 |
// direct user input of LCH values |
| 48 |
|
| 49 |
if (C1 < 0) { |
| 50 |
C1 = 0; |
| 51 |
} |
| 52 |
if (C2 < 0) { |
| 53 |
C2 = 0; |
| 54 |
} |
| 55 |
|
| 56 |
// we don't need H2 as ΔH is calculated from Δa, Δb and ΔC |
| 57 |
|
| 58 |
// Lightness and Chroma differences |
| 59 |
// These are (color - sample), unlike deltaE2000 |
| 60 |
let ΔL = L1 - L2; |
| 61 |
let ΔC = C1 - C2; |
| 62 |
|
| 63 |
let Δa = a1 - a2; |
| 64 |
let Δb = b1 - b2; |
| 65 |
|
| 66 |
// weighted Hue difference, less for larger Chroma difference |
| 67 |
|
| 68 |
let H2 = Δa ** 2 + Δb ** 2 - ΔC ** 2; |
| 69 |
// due to roundoff error it is possible that, for zero a and b, |
| 70 |
// ΔC > Δa + Δb is 0, resulting in attempting |
| 71 |
// to take the square root of a negative number |
| 72 |
|
| 73 |
// trying instead the equation from Industrial Color Physics |
| 74 |
// By Georg A. Klein |
| 75 |
|
| 76 |
// let ΔH = ((a1 * b2) - (a2 * b1)) / Math.sqrt(0.5 * ((C2 * C1) + (a2 * a1) + (b2 * b1))); |
| 77 |
// console.log({ΔH}); |
| 78 |
// This gives the same result to 12 decimal places |
| 79 |
// except it sometimes NaNs when trying to root a negative number |
| 80 |
|
| 81 |
// let ΔH = Math.sqrt(H2); we never actually use the root, it gets squared again!! |
| 82 |
|
| 83 |
// positional corrections to the lack of uniformity of CIELAB |
| 84 |
// These are all trying to make JND ellipsoids more like spheres |
| 85 |
|
| 86 |
// SL Lightness crispening factor, depends entirely on L1 not L2 |
| 87 |
let SL = 0.511; // linear portion of the Y to L transfer function |
| 88 |
if (L1 >= 16) { |
| 89 |
// cubic portion |
| 90 |
SL = (0.040975 * L1) / (1 + 0.01765 * L1); |
| 91 |
} |
| 92 |
|
| 93 |
// SC Chroma factor |
| 94 |
let SC = (0.0638 * C1) / (1 + 0.0131 * C1) + 0.638; |
| 95 |
|
| 96 |
// Cross term T for blue non-linearity |
| 97 |
let T; |
| 98 |
if (isNone(H1)) { |
| 99 |
H1 = 0; |
| 100 |
} |
| 101 |
|
| 102 |
if (H1 >= 164 && H1 <= 345) { |
| 103 |
T = 0.56 + Math.abs(0.2 * Math.cos((H1 + 168) * d2r)); |
| 104 |
} |
| 105 |
else { |
| 106 |
T = 0.36 + Math.abs(0.4 * Math.cos((H1 + 35) * d2r)); |
| 107 |
} |
| 108 |
// console.log({T}); |
| 109 |
|
| 110 |
// SH Hue factor also depends on C1, |
| 111 |
let C4 = Math.pow(C1, 4); |
| 112 |
let F = Math.sqrt(C4 / (C4 + 1900)); |
| 113 |
let SH = SC * (F * T + 1 - F); |
| 114 |
|
| 115 |
// Finally calculate the deltaE, term by term as root sume of squares |
| 116 |
let dE = (ΔL / (l * SL)) ** 2; |
| 117 |
dE += (ΔC / (c * SC)) ** 2; |
| 118 |
dE += H2 / SH ** 2; |
| 119 |
// dE += (ΔH / SH) ** 2; |
| 120 |
return Math.sqrt(dE); |
| 121 |
// Yay!!! |
| 122 |
} |