| 1 |
import oklab from "../spaces/oklab.js"; |
| 2 |
import getColor from "../getColor.js"; |
| 3 |
|
| 4 |
/** |
| 5 |
* More accurate color-difference formulae |
| 6 |
* than the simple 1976 Euclidean distance in CIE Lab |
| 7 |
* The Oklab a and b axes are scaled relative to the L axis, for better uniformity |
| 8 |
* Björn Ottosson said: |
| 9 |
* "I've recently done some tests with color distance datasets as implemented |
| 10 |
* in Colorio and on both the Combvd dataset and the OSA-UCS dataset a |
| 11 |
* scale factor of slightly more than 2 for a and b would give the best results |
| 12 |
* (2.016 works best for Combvd and 2.045 for the OSA-UCS dataset)." |
| 13 |
* @see {@link <https://github.com/w3c/csswg-drafts/issues/6642#issuecomment-945714988>} |
| 14 |
* @param {import("../types.js").ColorTypes} color |
| 15 |
* @param {import("../types.js").ColorTypes} sample |
| 16 |
* @returns {number} |
| 17 |
*/ |
| 18 |
export default function (color, sample) { |
| 19 |
[color, sample] = getColor([color, sample]); |
| 20 |
|
| 21 |
// Given this color as the reference |
| 22 |
// and a sample, |
| 23 |
// calculate deltaEOK2, term by term as root sum of squares |
| 24 |
let abscale = 2; |
| 25 |
let [L1, a1, b1] = oklab.from(color); |
| 26 |
let [L2, a2, b2] = oklab.from(sample); |
| 27 |
let ΔL = L1 - L2; |
| 28 |
let Δa = abscale * (a1 - a2); |
| 29 |
let Δb = abscale * (b1 - b2); |
| 30 |
return Math.sqrt(ΔL ** 2 + Δa ** 2 + Δb ** 2); |
| 31 |
} |