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

Audit function parameter and return types (1/2) #456

Merged
merged 1 commit 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
2 changes: 2 additions & 0 deletions src/chromaticity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
3 changes: 3 additions & 0 deletions src/distance.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/get.js
Original file line number Diff line number Diff line change
@@ -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];
Expand Down
3 changes: 3 additions & 0 deletions src/getAll.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ColorSpace from "./space.js";
import getColor from "./getColor.js";

/**
* Get the coordinates of a color in any color space
Expand All @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions types/src/chromaticity.d.ts
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 3 additions & 3 deletions types/src/distance.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";
import ColorSpace from "./space.js";

export default function distance (
color1: Color | ColorObject,
color2: Color | ColorObject,
color1: ColorTypes,
color2: ColorTypes,
space?: string | ColorSpace
): number;
4 changes: 2 additions & 2 deletions types/src/get.d.ts
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 3 additions & 3 deletions types/src/getAll.d.ts
Original file line number Diff line number Diff line change
@@ -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;
9 changes: 5 additions & 4 deletions types/test/chromaticity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions types/test/distance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions types/test/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 6 additions & 3 deletions types/test/getAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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