Skip to content

Commit

Permalink
Allow any color type when calling individual deltaE functions
Browse files Browse the repository at this point in the history
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
  • Loading branch information
lloydk committed Feb 25, 2024
1 parent 1483025 commit e3f867b
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 26 deletions.
4 changes: 0 additions & 4 deletions src/deltaE.js
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/deltaE/deltaE2000.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion src/deltaE/deltaE76.js
Original file line number Diff line number Diff line change
@@ -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");
}
4 changes: 4 additions & 0 deletions src/deltaE/deltaECMC.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions src/deltaE/deltaEHCT.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));

Expand Down
4 changes: 4 additions & 0 deletions src/deltaE/deltaEITP.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/deltaE/deltaEJz.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions src/deltaE/deltaEOK.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions types/src/deltaE/deltaE2000.d.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
6 changes: 3 additions & 3 deletions types/src/deltaE/deltaE76.d.ts
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 3 additions & 3 deletions types/src/deltaE/deltaECMC.d.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
6 changes: 3 additions & 3 deletions types/src/deltaE/deltaEHCT.d.ts
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 3 additions & 3 deletions types/src/deltaE/deltaEITP.d.ts
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 3 additions & 3 deletions types/src/deltaE/deltaEJz.d.ts
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 3 additions & 3 deletions types/src/deltaE/deltaEOK.d.ts
Original file line number Diff line number Diff line change
@@ -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;
31 changes: 31 additions & 0 deletions types/test/deltaE.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

0 comments on commit e3f867b

Please sign in to comment.