diff --git a/src/chromaticity.js b/src/chromaticity.js index 752fe5fdd..f93547579 100644 --- a/src/chromaticity.js +++ b/src/chromaticity.js @@ -3,12 +3,14 @@ import getAll from "./getAll.js"; // Chromaticity coordinates export function uv (color) { + // Assumes getAll() calls getColor() on color let [X, Y, Z] = getAll(color, xyz_d65); let denom = X + 15 * Y + 3 * Z; return [4 * X / denom, 9 * Y / denom]; } export function xy (color) { + // Assumes getAll() calls getColor() on color let [X, Y, Z] = getAll(color, xyz_d65); let sum = X + Y + Z; return [X / sum, Y / sum]; diff --git a/src/distance.js b/src/distance.js index d41459504..6f8c92653 100644 --- a/src/distance.js +++ b/src/distance.js @@ -1,9 +1,12 @@ import ColorSpace from "./space.js"; +import getColor from "./getColor.js"; /** * Euclidean distance of colors in an arbitrary color space */ export default function distance (color1, color2, space = "lab") { + [color1, color2] = getColor([color1, color2]); + space = ColorSpace.get(space); let coords1 = space.from(color1); diff --git a/src/get.js b/src/get.js index ee72bb787..59e1184b9 100644 --- a/src/get.js +++ b/src/get.js @@ -1,7 +1,10 @@ import ColorSpace from "./space.js"; import getAll from "./getAll.js"; +import getColor from "./getColor.js"; export default function get (color, prop) { + color = getColor(color); + let {space, index} = ColorSpace.resolveCoord(prop, color.space); let coords = getAll(color, space); return coords[index]; diff --git a/src/getAll.js b/src/getAll.js index c9d15e15c..05a803c86 100644 --- a/src/getAll.js +++ b/src/getAll.js @@ -1,4 +1,5 @@ import ColorSpace from "./space.js"; +import getColor from "./getColor.js"; /** * Get the coordinates of a color in any color space @@ -7,6 +8,8 @@ import ColorSpace from "./space.js"; * @returns {number[]} The color coordinates in the given color space */ export default function getAll (color, space) { + color = getColor(color); + if (!space || color.space.equals(space)) { // No conversion needed return color.coords.slice(); diff --git a/types/src/chromaticity.d.ts b/types/src/chromaticity.d.ts index 2d79bfeef..17f40c2da 100644 --- a/types/src/chromaticity.d.ts +++ b/types/src/chromaticity.d.ts @@ -1,7 +1,7 @@ -import Color, { ColorObject } from "./color.js"; +import Color, { ColorTypes } from "./color.js"; -export function uv (color: Color | ColorObject): [number, number]; +export function uv (color: ColorTypes): [number, number]; -export function xy (color: Color | ColorObject): [number, number]; +export function xy (color: ColorTypes): [number, number]; export function register (color: typeof Color): void; diff --git a/types/src/distance.d.ts b/types/src/distance.d.ts index 5684be876..60a368afe 100644 --- a/types/src/distance.d.ts +++ b/types/src/distance.d.ts @@ -1,8 +1,8 @@ -import Color, { ColorObject } from "./color.js"; +import { ColorTypes } from "./color.js"; import ColorSpace from "./space.js"; export default function distance ( - color1: Color | ColorObject, - color2: Color | ColorObject, + color1: ColorTypes, + color2: ColorTypes, space?: string | ColorSpace ): number; diff --git a/types/src/get.d.ts b/types/src/get.d.ts index b2eca227c..1ee99d55e 100644 --- a/types/src/get.d.ts +++ b/types/src/get.d.ts @@ -1,4 +1,4 @@ -import Color, { ColorObject } from "./color.js"; +import { ColorTypes } from "./color.js"; import { Ref } from "./space.js"; -export default function get (color: Color | ColorObject, prop: Ref): number; +export default function get (color: ColorTypes, prop: Ref): number; diff --git a/types/src/getAll.d.ts b/types/src/getAll.d.ts index 0ac081906..48c6a1b68 100644 --- a/types/src/getAll.d.ts +++ b/types/src/getAll.d.ts @@ -1,7 +1,7 @@ -import Color, { ColorObject } from "./color.js"; +import { ColorTypes, Coords } from "./color.js"; import ColorSpace from "./space.js"; export default function getAll ( - color: Color | ColorObject, + color: ColorTypes, space?: string | ColorSpace -): [number, number, number]; +): Coords; diff --git a/types/test/chromaticity.ts b/types/test/chromaticity.ts index dfd64e044..50447f96c 100644 --- a/types/test/chromaticity.ts +++ b/types/test/chromaticity.ts @@ -3,17 +3,18 @@ import { uv, xy, register } from "colorjs.io/src/chromaticity"; // @ts-expect-error uv(); -// @ts-expect-error -uv("red"); +uv("red"); // $ExpectType [number, number] uv(new Color("red")); // $ExpectType [number, number] +new Color("red").uv(); // $ExpectType [number, number] // @ts-expect-error xy(); -// @ts-expect-error -xy("red"); +xy("red"); // $ExpectType [number, number] xy(new Color("red")); // $ExpectType [number, number] +new Color("red").xy(); // $ExpectType [number, number] + // @ts-expect-error register(); diff --git a/types/test/distance.ts b/types/test/distance.ts index 04fabaa2f..7ec7dba85 100644 --- a/types/test/distance.ts +++ b/types/test/distance.ts @@ -13,3 +13,6 @@ distance(c1); distance(c1, c2); // $ExpectType number distance(c1, c2, space); // $ExpectType number distance(c1, c2, "srgb"); // $ExpectType number +distance("red", "blue"); // $ExpectType number + +c1.distance(c2); // $ExpectType number diff --git a/types/test/get.ts b/types/test/get.ts index 89cc8ce4c..609fbe5b9 100644 --- a/types/test/get.ts +++ b/types/test/get.ts @@ -9,3 +9,6 @@ get(new Color("red")); get(new Color("red"), "p3.r"); // $ExpectType number get(new Color("red"), ["p3", "r"]); // $ExpectType number get(new Color("red"), { space: "p3", coordId: "r" }); // $ExpectType number +get("red", "p3.r"); // $ExpectType number + +new Color("red").get("p3.r"); // $ExpectType number diff --git a/types/test/getAll.ts b/types/test/getAll.ts index 6fce16a2d..86386c7e9 100644 --- a/types/test/getAll.ts +++ b/types/test/getAll.ts @@ -5,6 +5,9 @@ import sRGB from "colorjs.io/src/spaces/srgb"; // @ts-expect-error getAll(); -getAll(new Color("red")); // $ExpectType [number, number, number] -getAll(new Color("red"), "srgb"); // $ExpectType [number, number, number] -getAll(new Color("red"), sRGB); // $ExpectType [number, number, number] +getAll(new Color("red")); // $ExpectType Coords +getAll(new Color("red"), "srgb"); // $ExpectType Coords +getAll(new Color("red"), sRGB); // $ExpectType Coords +getAll("red", sRGB); // $ExpectType Coords + +new Color("red").getAll(); // $ExpectType Coords