UNPKG

colorjs.io

Version:

Let’s get serious about color

49 lines (42 loc) 1.27 kB
import jzczhz from "../spaces/jzczhz.js"; import getColor from "../getColor.js"; import { isNone } from "../util.js"; /** * More accurate color-difference formulae * than the simple 1976 Euclidean distance in Lab * * Uses JzCzHz, which has improved perceptual uniformity * and thus a simple Euclidean root-sum of ΔL² ΔC² ΔH² * gives good results. * @param {import("../types.js").ColorTypes} color * @param {import("../types.js").ColorTypes} sample * @returns {number} */ export default function (color, sample) { [color, sample] = getColor([color, sample]); // Given this color as the reference // and a sample, // calculate deltaE in JzCzHz. let [Jz1, Cz1, Hz1] = jzczhz.from(color); let [Jz2, Cz2, Hz2] = jzczhz.from(sample); // Lightness and Chroma differences // sign does not matter as they are squared. let ΔJ = Jz1 - Jz2; let ΔC = Cz1 - Cz2; // length of chord for ΔH if (isNone(Hz1) && isNone(Hz2)) { // both undefined hues Hz1 = 0; Hz2 = 0; } else if (isNone(Hz1)) { // one undefined, set to the defined hue Hz1 = Hz2; } else if (isNone(Hz2)) { Hz2 = Hz1; } let Δh = Hz1 - Hz2; let ΔH = 2 * Math.sqrt(Cz1 * Cz2) * Math.sin((Δh / 2) * (Math.PI / 180)); return Math.sqrt(ΔJ ** 2 + ΔC ** 2 + ΔH ** 2); }