| 1 |
import defaults from "../defaults.js"; |
| 2 |
import lab from "../spaces/lab.js"; |
| 3 |
import lch from "../spaces/lch.js"; |
| 4 |
import getColor from "../getColor.js"; |
| 5 |
|
| 6 |
// deltaE2000 is a statistically significant improvement |
| 7 |
// and is recommended by the CIE and Idealliance |
| 8 |
// especially for color differences less than 10 deltaE76 |
| 9 |
// but is wicked complicated |
| 10 |
// and many implementations have small errors! |
| 11 |
// DeltaE2000 is also discontinuous; in case this |
| 12 |
// matters to you, use deltaECMC instead. |
| 13 |
|
| 14 |
const Gfactor = 25 ** 7; |
| 15 |
const π = Math.PI; |
| 16 |
const r2d = 180 / π; |
| 17 |
const d2r = π / 180; |
| 18 |
|
| 19 |
function pow7 (x) { |
| 20 |
// Faster than x ** 7 or Math.pow(x, 7) |
| 21 |
|
| 22 |
const x2 = x * x; |
| 23 |
const x7 = x2 * x2 * x2 * x; |
| 24 |
|
| 25 |
return x7; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* @param {import("../types.js").ColorTypes} color |
| 30 |
* @param {import("../types.js").ColorTypes} sample |
| 31 |
* @param {{ kL?: number | undefined; kC?: number | undefined; kH?: number | undefined }} options |
| 32 |
* @returns {number} |
| 33 |
*/ |
| 34 |
export default function (color, sample, { kL = 1, kC = 1, kH = 1 } = {}) { |
| 35 |
[color, sample] = getColor([color, sample]); |
| 36 |
|
| 37 |
// Given this color as the reference |
| 38 |
// and the function parameter as the sample, |
| 39 |
// calculate deltaE 2000. |
| 40 |
|
| 41 |
// This implementation assumes the parametric |
| 42 |
// weighting factors kL, kC and kH |
| 43 |
// for the influence of viewing conditions |
| 44 |
// are all 1, as sadly seems typical. |
| 45 |
// kL should be increased for lightness texture or noise |
| 46 |
// and kC increased for chroma noise |
| 47 |
|
| 48 |
let [L1, a1, b1] = lab.from(color); |
| 49 |
let C1 = lch.from(lab, [L1, a1, b1])[1]; |
| 50 |
let [L2, a2, b2] = lab.from(sample); |
| 51 |
let C2 = lch.from(lab, [L2, a2, b2])[1]; |
| 52 |
|
| 53 |
// Check for negative Chroma, |
| 54 |
// which might happen through |
| 55 |
// direct user input of LCH values |
| 56 |
|
| 57 |
if (C1 < 0) { |
| 58 |
C1 = 0; |
| 59 |
} |
| 60 |
if (C2 < 0) { |
| 61 |
C2 = 0; |
| 62 |
} |
| 63 |
|
| 64 |
let Cbar = (C1 + C2) / 2; // mean Chroma |
| 65 |
|
| 66 |
// calculate a-axis asymmetry factor from mean Chroma |
| 67 |
// this turns JND ellipses for near-neutral colors back into circles |
| 68 |
let C7 = pow7(Cbar); |
| 69 |
|
| 70 |
let G = 0.5 * (1 - Math.sqrt(C7 / (C7 + Gfactor))); |
| 71 |
|
| 72 |
// scale a axes by asymmetry factor |
| 73 |
// this by the way is why there is no Lab2000 colorspace |
| 74 |
let adash1 = (1 + G) * a1; |
| 75 |
let adash2 = (1 + G) * a2; |
| 76 |
|
| 77 |
// calculate new Chroma from scaled a and original b axes |
| 78 |
let Cdash1 = Math.sqrt(adash1 ** 2 + b1 ** 2); |
| 79 |
let Cdash2 = Math.sqrt(adash2 ** 2 + b2 ** 2); |
| 80 |
|
| 81 |
// calculate new hues, with zero hue for true neutrals |
| 82 |
// and in degrees, not radians |
| 83 |
|
| 84 |
let h1 = adash1 === 0 && b1 === 0 ? 0 : Math.atan2(b1, adash1); |
| 85 |
let h2 = adash2 === 0 && b2 === 0 ? 0 : Math.atan2(b2, adash2); |
| 86 |
|
| 87 |
if (h1 < 0) { |
| 88 |
h1 += 2 * π; |
| 89 |
} |
| 90 |
if (h2 < 0) { |
| 91 |
h2 += 2 * π; |
| 92 |
} |
| 93 |
|
| 94 |
h1 *= r2d; |
| 95 |
h2 *= r2d; |
| 96 |
|
| 97 |
// Lightness and Chroma differences; sign matters |
| 98 |
let ΔL = L2 - L1; |
| 99 |
let ΔC = Cdash2 - Cdash1; |
| 100 |
|
| 101 |
// Hue difference, getting the sign correct |
| 102 |
let hdiff = h2 - h1; |
| 103 |
let hsum = h1 + h2; |
| 104 |
let habs = Math.abs(hdiff); |
| 105 |
let Δh; |
| 106 |
|
| 107 |
if (Cdash1 * Cdash2 === 0) { |
| 108 |
Δh = 0; |
| 109 |
} |
| 110 |
else if (habs <= 180) { |
| 111 |
Δh = hdiff; |
| 112 |
} |
| 113 |
else if (hdiff > 180) { |
| 114 |
Δh = hdiff - 360; |
| 115 |
} |
| 116 |
else if (hdiff < -180) { |
| 117 |
Δh = hdiff + 360; |
| 118 |
} |
| 119 |
else { |
| 120 |
defaults.warn("the unthinkable has happened"); |
| 121 |
} |
| 122 |
|
| 123 |
// weighted Hue difference, more for larger Chroma |
| 124 |
let ΔH = 2 * Math.sqrt(Cdash2 * Cdash1) * Math.sin((Δh * d2r) / 2); |
| 125 |
|
| 126 |
// calculate mean Lightness and Chroma |
| 127 |
let Ldash = (L1 + L2) / 2; |
| 128 |
let Cdash = (Cdash1 + Cdash2) / 2; |
| 129 |
let Cdash7 = pow7(Cdash); |
| 130 |
|
| 131 |
// Compensate for non-linearity in the blue region of Lab. |
| 132 |
// Four possibilities for hue weighting factor, |
| 133 |
// depending on the angles, to get the correct sign |
| 134 |
let hdash; |
| 135 |
if (Cdash1 * Cdash2 === 0) { |
| 136 |
hdash = hsum; // which should be zero |
| 137 |
} |
| 138 |
else if (habs <= 180) { |
| 139 |
hdash = hsum / 2; |
| 140 |
} |
| 141 |
else if (hsum < 360) { |
| 142 |
hdash = (hsum + 360) / 2; |
| 143 |
} |
| 144 |
else { |
| 145 |
hdash = (hsum - 360) / 2; |
| 146 |
} |
| 147 |
|
| 148 |
// positional corrections to the lack of uniformity of CIELAB |
| 149 |
// These are all trying to make JND ellipsoids more like spheres |
| 150 |
|
| 151 |
// SL Lightness crispening factor |
| 152 |
// a background with L=50 is assumed |
| 153 |
let lsq = (Ldash - 50) ** 2; |
| 154 |
let SL = 1 + (0.015 * lsq) / Math.sqrt(20 + lsq); |
| 155 |
|
| 156 |
// SC Chroma factor, similar to those in CMC and deltaE 94 formulae |
| 157 |
let SC = 1 + 0.045 * Cdash; |
| 158 |
|
| 159 |
// Cross term T for blue non-linearity |
| 160 |
let T = 1; |
| 161 |
T -= 0.17 * Math.cos((hdash - 30) * d2r); |
| 162 |
T += 0.24 * Math.cos(2 * hdash * d2r); |
| 163 |
T += 0.32 * Math.cos((3 * hdash + 6) * d2r); |
| 164 |
T -= 0.2 * Math.cos((4 * hdash - 63) * d2r); |
| 165 |
|
| 166 |
// SH Hue factor depends on Chroma, |
| 167 |
// as well as adjusted hue angle like deltaE94. |
| 168 |
let SH = 1 + 0.015 * Cdash * T; |
| 169 |
|
| 170 |
// RT Hue rotation term compensates for rotation of JND ellipses |
| 171 |
// and Munsell constant hue lines |
| 172 |
// in the medium-high Chroma blue region |
| 173 |
// (Hue 225 to 315) |
| 174 |
let Δθ = 30 * Math.exp(-1 * ((hdash - 275) / 25) ** 2); |
| 175 |
let RC = 2 * Math.sqrt(Cdash7 / (Cdash7 + Gfactor)); |
| 176 |
let RT = -1 * Math.sin(2 * Δθ * d2r) * RC; |
| 177 |
|
| 178 |
// Finally calculate the deltaE, term by term as root sume of squares |
| 179 |
let dE = (ΔL / (kL * SL)) ** 2; |
| 180 |
dE += (ΔC / (kC * SC)) ** 2; |
| 181 |
dE += (ΔH / (kH * SH)) ** 2; |
| 182 |
dE += RT * (ΔC / (kC * SC)) * (ΔH / (kH * SH)); |
| 183 |
return Math.sqrt(dE); |
| 184 |
// Yay!!! |
| 185 |
} |