From 809f83a4d234bb43bb13441f00f17565f7d7f887 Mon Sep 17 00:00:00 2001 From: Lloyd Kupchanko Date: Sat, 17 Feb 2024 08:09:24 -0700 Subject: [PATCH] Allow any color type when calling individual deltaE functions The types for the deltaE functions didn't allow all ColorTypes as parameters. For example, the types didn't allow "red" to be passed as a parameter. Also, calling a deletaE method with a color such as "red" would cause an exception to be thrown. e.g. let x = deltaE2000("red", "white") // Exception thrown --- src/deltaE.js | 4 ---- src/deltaE/deltaE2000.js | 4 ++++ src/deltaE/deltaE76.js | 3 ++- src/deltaE/deltaECMC.js | 4 ++++ src/deltaE/deltaEHCT.js | 4 ++++ src/deltaE/deltaEITP.js | 4 ++++ src/deltaE/deltaEJz.js | 4 ++++ src/deltaE/deltaEOK.js | 4 ++++ types/src/deltaE/deltaE2000.d.ts | 6 +++--- types/src/deltaE/deltaE76.d.ts | 6 +++--- types/src/deltaE/deltaECMC.d.ts | 6 +++--- types/src/deltaE/deltaEHCT.d.ts | 6 +++--- types/src/deltaE/deltaEITP.d.ts | 6 +++--- types/src/deltaE/deltaEJz.d.ts | 6 +++--- types/src/deltaE/deltaEOK.d.ts | 6 +++--- types/test/deltaE.ts | 31 +++++++++++++++++++++++++++++++ 16 files changed, 78 insertions(+), 26 deletions(-) diff --git a/src/deltaE.js b/src/deltaE.js index a04bc64c2..645011ae2 100644 --- a/src/deltaE.js +++ b/src/deltaE.js @@ -1,4 +1,3 @@ -import getColor from "./getColor.js"; import defaults from "./defaults.js"; import {isString} from "./util.js"; import deltaEMethods from "./deltaE/index.js"; @@ -10,9 +9,6 @@ export default function deltaE (c1, c2, o = {}) { let {method = defaults.deltaE, ...rest} = o; - c1 = getColor(c1); - c2 = getColor(c2); - for (let m in deltaEMethods) { if ("deltae" + method.toLowerCase() === m.toLowerCase()) { return deltaEMethods[m](c1, c2, rest); diff --git a/src/deltaE/deltaE2000.js b/src/deltaE/deltaE2000.js index 2af81fb4b..9ceab9a3a 100644 --- a/src/deltaE/deltaE2000.js +++ b/src/deltaE/deltaE2000.js @@ -1,5 +1,6 @@ import lab from "../spaces/lab.js"; import lch from "../spaces/lch.js"; +import getColor from "../getColor.js"; // deltaE2000 is a statistically significant improvement // and is recommended by the CIE and Idealliance @@ -24,6 +25,9 @@ function pow7 (x) { } export default function (color, sample, {kL = 1, kC = 1, kH = 1} = {}) { + color = getColor(color); + sample = getColor(sample); + // Given this color as the reference // and the function parameter as the sample, // calculate deltaE 2000. diff --git a/src/deltaE/deltaE76.js b/src/deltaE/deltaE76.js index 7162dba2c..99c344956 100644 --- a/src/deltaE/deltaE76.js +++ b/src/deltaE/deltaE76.js @@ -1,5 +1,6 @@ import distance from "../distance.js"; +import getColor from "../getColor.js"; export default function deltaE76 (color, sample) { - return distance(color, sample, "lab"); + return distance(getColor(color), getColor(sample), "lab"); } diff --git a/src/deltaE/deltaECMC.js b/src/deltaE/deltaECMC.js index b66f0c00d..07c3e45e3 100644 --- a/src/deltaE/deltaECMC.js +++ b/src/deltaE/deltaECMC.js @@ -1,5 +1,6 @@ import lab from "../spaces/lab.js"; import lch from "../spaces/lch.js"; +import getColor from "../getColor.js"; // More accurate color-difference formulae // than the simple 1976 Euclidean distance in Lab @@ -13,6 +14,9 @@ const π = Math.PI; const d2r = π / 180; export default function (color, sample, {l = 2, c = 1} = {}) { + color = getColor(color); + sample = getColor(sample); + // Given this color as the reference // and a sample, // calculate deltaE CMC. diff --git a/src/deltaE/deltaEHCT.js b/src/deltaE/deltaEHCT.js index e104a531c..45ec7ed4c 100644 --- a/src/deltaE/deltaEHCT.js +++ b/src/deltaE/deltaEHCT.js @@ -1,5 +1,6 @@ import hct from "../spaces/hct.js"; import {viewingConditions} from "../spaces/hct.js"; +import getColor from "../getColor.js"; const rad2deg = 180 / Math.PI; const deg2rad = Math.PI / 180; @@ -39,6 +40,9 @@ function convertUcsAb (coords) { * @return {number[]} */ export default function (color, sample) { + color = getColor(color); + sample = getColor(sample); + let [ t1, a1, b1 ] = convertUcsAb(hct.from(color)); let [ t2, a2, b2 ] = convertUcsAb(hct.from(sample)); diff --git a/src/deltaE/deltaEITP.js b/src/deltaE/deltaEITP.js index 84d221b79..28e7638f5 100644 --- a/src/deltaE/deltaEITP.js +++ b/src/deltaE/deltaEITP.js @@ -1,10 +1,14 @@ import ictcp from "../spaces/ictcp.js"; +import getColor from "../getColor.js"; // Delta E in ICtCp space, // which the ITU calls Delta E ITP, which is shorter // formulae from ITU Rec. ITU-R BT.2124-0 export default function (color, sample) { + color = getColor(color); + sample = getColor(sample); + // Given this color as the reference // and a sample, // calculate deltaE in ICtCp diff --git a/src/deltaE/deltaEJz.js b/src/deltaE/deltaEJz.js index c1ceb7aad..e5878651a 100644 --- a/src/deltaE/deltaEJz.js +++ b/src/deltaE/deltaEJz.js @@ -1,4 +1,5 @@ import jzczhz from "../spaces/jzczhz.js"; +import getColor from "../getColor.js"; // More accurate color-difference formulae // than the simple 1976 Euclidean distance in Lab @@ -8,6 +9,9 @@ import jzczhz from "../spaces/jzczhz.js"; // gives good results. export default function (color, sample) { + color = getColor(color); + sample = getColor(sample); + // Given this color as the reference // and a sample, // calculate deltaE in JzCzHz. diff --git a/src/deltaE/deltaEOK.js b/src/deltaE/deltaEOK.js index 8a408624d..db145bbf6 100644 --- a/src/deltaE/deltaEOK.js +++ b/src/deltaE/deltaEOK.js @@ -2,8 +2,12 @@ // than the simple 1976 Euclidean distance in CIE Lab import oklab from "../spaces/oklab.js"; +import getColor from "../getColor.js"; export default function (color, sample) { + color = getColor(color); + sample = getColor(sample); + // Given this color as the reference // and a sample, // calculate deltaEOK, term by term as root sum of squares diff --git a/types/src/deltaE/deltaE2000.d.ts b/types/src/deltaE/deltaE2000.d.ts index 0581b0b0b..866939b97 100644 --- a/types/src/deltaE/deltaE2000.d.ts +++ b/types/src/deltaE/deltaE2000.d.ts @@ -1,8 +1,8 @@ -import Color, { ColorObject } from "../color.js"; +import { ColorTypes } from "../color.js"; export default function ( - color: Color | ColorObject, - sample: Color | ColorObject, + color: ColorTypes, + sample: ColorTypes, options?: { kL?: number | undefined; kC?: number | undefined; diff --git a/types/src/deltaE/deltaE76.d.ts b/types/src/deltaE/deltaE76.d.ts index cf5329137..2d1833c7b 100644 --- a/types/src/deltaE/deltaE76.d.ts +++ b/types/src/deltaE/deltaE76.d.ts @@ -1,5 +1,5 @@ -import Color, { ColorObject } from "../color.js"; +import { ColorTypes } from "../color.js"; export default function ( - color: Color | ColorObject, - sample: Color | ColorObject + color: ColorTypes, + sample: ColorTypes ): number; diff --git a/types/src/deltaE/deltaECMC.d.ts b/types/src/deltaE/deltaECMC.d.ts index 368fefa27..09b940b66 100644 --- a/types/src/deltaE/deltaECMC.d.ts +++ b/types/src/deltaE/deltaECMC.d.ts @@ -1,7 +1,7 @@ -import Color, { ColorObject } from "../color.js"; +import { ColorTypes } from "../color.js"; export default function ( - color: Color | ColorObject, - sample: Color | ColorObject, + color: ColorTypes, + sample: ColorTypes, options?: { l?: number | undefined; c?: number | undefined; diff --git a/types/src/deltaE/deltaEHCT.d.ts b/types/src/deltaE/deltaEHCT.d.ts index cf5329137..2d1833c7b 100644 --- a/types/src/deltaE/deltaEHCT.d.ts +++ b/types/src/deltaE/deltaEHCT.d.ts @@ -1,5 +1,5 @@ -import Color, { ColorObject } from "../color.js"; +import { ColorTypes } from "../color.js"; export default function ( - color: Color | ColorObject, - sample: Color | ColorObject + color: ColorTypes, + sample: ColorTypes ): number; diff --git a/types/src/deltaE/deltaEITP.d.ts b/types/src/deltaE/deltaEITP.d.ts index cf5329137..2d1833c7b 100644 --- a/types/src/deltaE/deltaEITP.d.ts +++ b/types/src/deltaE/deltaEITP.d.ts @@ -1,5 +1,5 @@ -import Color, { ColorObject } from "../color.js"; +import { ColorTypes } from "../color.js"; export default function ( - color: Color | ColorObject, - sample: Color | ColorObject + color: ColorTypes, + sample: ColorTypes ): number; diff --git a/types/src/deltaE/deltaEJz.d.ts b/types/src/deltaE/deltaEJz.d.ts index cf5329137..2d1833c7b 100644 --- a/types/src/deltaE/deltaEJz.d.ts +++ b/types/src/deltaE/deltaEJz.d.ts @@ -1,5 +1,5 @@ -import Color, { ColorObject } from "../color.js"; +import { ColorTypes } from "../color.js"; export default function ( - color: Color | ColorObject, - sample: Color | ColorObject + color: ColorTypes, + sample: ColorTypes ): number; diff --git a/types/src/deltaE/deltaEOK.d.ts b/types/src/deltaE/deltaEOK.d.ts index cf5329137..2d1833c7b 100644 --- a/types/src/deltaE/deltaEOK.d.ts +++ b/types/src/deltaE/deltaEOK.d.ts @@ -1,5 +1,5 @@ -import Color, { ColorObject } from "../color.js"; +import { ColorTypes } from "../color.js"; export default function ( - color: Color | ColorObject, - sample: Color | ColorObject + color: ColorTypes, + sample: ColorTypes ): number; diff --git a/types/test/deltaE.ts b/types/test/deltaE.ts index 698c51684..329067363 100644 --- a/types/test/deltaE.ts +++ b/types/test/deltaE.ts @@ -1,9 +1,19 @@ import Color from "colorjs.io/src/color"; import deltaE from "colorjs.io/src/deltaE"; +import deltaE76 from "colorjs.io/src/deltaE/deltaE76"; +import deltaE2000 from "colorjs.io/src/deltaE/deltaE2000"; +import deltaECMC from "colorjs.io/src/deltaE/deltaECMC"; +import deltaEHCT from "colorjs.io/src/deltaE/deltaEHCT"; +import deltaEITP from "colorjs.io/src/deltaE/deltaEITP"; +import deltaEJz from "colorjs.io/src/deltaE/deltaEJz"; +import deltaEOK from "colorjs.io/src/deltaE/deltaEOK"; +import type { ColorObject } from "colorjs.io/src/color"; const c1 = new Color("red"); const c2 = new Color("blue"); +const blue: ColorObject = {spaceId: "srgb", coords: [0, 0, 1]}; + // @ts-expect-error deltaE(); // @ts-expect-error @@ -13,3 +23,24 @@ deltaE(c1, c2); deltaE(c1, c2, "2000"); // $ExpectType number deltaE(c1, c2, { method: "2000" }); // $ExpectType number + +deltaE76(c1, c2); // $ExpectType number +deltaE76("red", blue); // $ExpectType number + +deltaE2000(c1, c2); // $ExpectType number +deltaE2000("red", blue); // $ExpectType number + +deltaECMC(c1, c2); // $ExpectType number +deltaECMC("red", blue); // $ExpectType number + +deltaEHCT(c1, c2); // $ExpectType number +deltaEHCT("red", blue); // $ExpectType number + +deltaEITP(c1, c2); // $ExpectType number +deltaEITP("red", blue); // $ExpectType number + +deltaEJz(c1, c2); // $ExpectType number +deltaEJz("red", blue); // $ExpectType number + +deltaEOK(c1, c2); // $ExpectType number +deltaEOK("red", blue); // $ExpectType number