Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow any color type when calling individual deltaE functions #451

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 3 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,8 @@ function pow7 (x) {
}

export default function (color, sample, {kL = 1, kC = 1, kH = 1} = {}) {
[color, sample] = getColor([color, sample]);

// Given this color as the reference
// and the function parameter as the sample,
// calculate deltaE 2000.
Expand Down
2 changes: 2 additions & 0 deletions src/deltaE/deltaE76.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import distance from "../distance.js";
import getColor from "../getColor.js";

export default function deltaE76 (color, sample) {
// Assume getColor() is called in the distance function
return distance(color, sample, "lab");
}
3 changes: 3 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,8 @@ const π = Math.PI;
const d2r = π / 180;

export default function (color, sample, {l = 2, c = 1} = {}) {
[color, sample] = getColor([color, sample]);

// Given this color as the reference
// and a sample,
// calculate deltaE CMC.
Expand Down
3 changes: 3 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,8 @@ function convertUcsAb (coords) {
* @return {number[]}
*/
export default function (color, sample) {
[color, sample] = getColor([color, sample]);

let [ t1, a1, b1 ] = convertUcsAb(hct.from(color));
let [ t2, a2, b2 ] = convertUcsAb(hct.from(sample));

Expand Down
3 changes: 3 additions & 0 deletions src/deltaE/deltaEITP.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
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, sample] = getColor([color, sample]);

// Given this color as the reference
// and a sample,
// calculate deltaE in ICtCp
Expand Down
3 changes: 3 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,8 @@ import jzczhz from "../spaces/jzczhz.js";
// gives good results.

export default function (color, sample) {
[color, sample] = getColor([color, sample]);

// Given this color as the reference
// and a sample,
// calculate deltaE in JzCzHz.
Expand Down
3 changes: 3 additions & 0 deletions src/deltaE/deltaEOK.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
// 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, sample] = getColor([color, 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