Skip to content

Commit

Permalink
Audit function parameter and return types (2/2)
Browse files Browse the repository at this point in the history
Ensure that functions in the following modules have the correct types
and ensure that all color types can be passed as parameters:

- inGamut
- luminance
- set
- setAll
- space
- toGamut
  • Loading branch information
lloydk committed Feb 27, 2024
1 parent 57a88ed commit 2c93907
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 26 deletions.
7 changes: 6 additions & 1 deletion src/inGamut.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ const ε = .000075;
* Check if a color is in gamut of either its own or another color space
* @return {Boolean} Is the color in gamut?
*/
export default function inGamut (color, space = color.space, {epsilon = ε} = {}) {
export default function inGamut (color, space, {epsilon = ε} = {}) {
color = getColor(color);

if (!space) {
space = color.space;
}

space = ColorSpace.get(space);
let coords = color.coords;

Expand Down
2 changes: 2 additions & 0 deletions src/luminance.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import set from "./set.js";
import xyz_d65 from "./spaces/xyz-d65.js";

export function getLuminance (color) {
// Assume getColor() is called on color in get()
return get(color, [xyz_d65, "y"]);
}

export function setLuminance (color, value) {
// Assume getColor() is called on color in set()
set(color, [xyz_d65, "y"], value);
}

Expand Down
3 changes: 3 additions & 0 deletions src/setAll.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import ColorSpace from "./space.js";
import getColor from "./getColor.js";

export default function setAll (color, space, coords) {
color = getColor(color);

space = ColorSpace.get(space);
color.coords = space.to(color.space, coords);
return color;
Expand Down
18 changes: 15 additions & 3 deletions src/toGamut.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,20 @@ export default function toGamut (
color,
{
method = defaults.gamut_mapping,
space = color.space,
space = undefined,
deltaEMethod = "",
jnd = 2,
blackWhiteClamp = {},
} = {},
) {
color = getColor(color);

if (util.isString(arguments[1])) {
space = arguments[1];
}
else if (!space) {
space = color.space;
}

space = ColorSpace.get(space);

Expand All @@ -83,7 +88,7 @@ export default function toGamut (
// mapSpace: space with the coord we're reducing

if (inGamut(color, space, { epsilon: 0 })) {
return getColor(color);
return color;
}

let spaceColor;
Expand Down Expand Up @@ -219,9 +224,16 @@ const COLORS = {
* @param {ColorSpace|string} options.space
* @returns {Color}
*/
export function toGamutCSS (origin, { space = origin.space }) {
export function toGamutCSS (origin, {space} = {}) {
const JND = 0.02;
const ε = 0.0001;

origin = getColor(origin);

if (!space) {
space = origin.space;
}

space = ColorSpace.get(space);
const oklchSpace = ColorSpace.get("oklch");

Expand Down
6 changes: 3 additions & 3 deletions types/src/luminance.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Color, { ColorObject } from "./color.js";
import Color, { ColorTypes } from "./color.js";

export function getLuminance (color: Color | ColorObject): number;
export function getLuminance (color: ColorTypes): number;

export function setLuminance (
color: Color | ColorObject,
color: ColorTypes,
value: number | ((coord: number) => number),
): void;

Expand Down
6 changes: 3 additions & 3 deletions types/src/set.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import Color, { ColorTypes } from "./color.js";
import { ColorTypes, PlainColorObject } from "./color.js";
import { Ref } from "./space.js";

export default function set (
color: ColorTypes,
prop: Ref,
value: number | ((coord: number) => number)
): Color;
): PlainColorObject;
export default function set (
color: ColorTypes,
props: Record<string, number | ((coord: number) => number)>
): Color;
): PlainColorObject;
14 changes: 10 additions & 4 deletions types/src/setAll.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import Color, { ColorObject } from "./color.js";
import Color, { ColorTypes, PlainColorObject } from "./color.js";
import ColorSpace from "./space.js";

export default function setAll<T extends Color | ColorObject> (
color: T,
declare namespace setAll {
let returns: "color";
}

declare function setAll (
color: ColorTypes,
space: string | ColorSpace,
coords: [number, number, number]
): T;
): PlainColorObject;

export default setAll;
6 changes: 3 additions & 3 deletions types/src/space.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { White } from "./adapt.js";
import Color, { ColorConstructor, ColorObject, Coords } from "./color.js";
import Color, { ColorConstructor, ColorObject, Coords, PlainColorObject } from "./color.js";

export interface Format {
/** @default "function" */
Expand Down Expand Up @@ -127,7 +127,7 @@ export default class ColorSpace {
white: White;
gamutSpace: ColorSpace;

from (color: Color | ColorObject): Coords;
from (color: PlainColorObject): Coords;
from (space: string | ColorSpace, coords: Coords): Coords;

getFormat (format?: string | Format): Format | null;
Expand All @@ -136,7 +136,7 @@ export default class ColorSpace {

inGamut (coords: Coords, options?: { epsilon?: number }): boolean;

to (color: Color | ColorObject): Coords;
to (color: PlainColorObject): Coords;
to (space: string | ColorSpace, coords: Coords): Coords;

toString (): string;
Expand Down
8 changes: 6 additions & 2 deletions types/test/luminance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { getLuminance, setLuminance, register } from "colorjs.io/src/luminance";

// @ts-expect-error
getLuminance();
// @ts-expect-error
getLuminance("red");

getLuminance("red"); // $ExpectType number
getLuminance(new Color("red")); // $ExpectType number
new Color("red").luminance; // $ExpectType number


// @ts-expect-error
setLuminance();
Expand All @@ -15,8 +16,11 @@ setLuminance("red");

// @ts-expect-error
setLuminance(new Color("red"));

setLuminance(new Color("red"), 1);
setLuminance(new Color("red"), () => 1);
setLuminance("red", 1);
new Color("red").luminance = 1;

// @ts-expect-error
register();
Expand Down
10 changes: 6 additions & 4 deletions types/test/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ set();
// @ts-expect-error
set("red");

set("red", "foo", 123); // $ExpectType Color
set(new Color("red"), ["srgb", "bar"], (_: number) => 123); // $ExpectType Color
set(new Color("red"), [sRGB, "bar"], (_: number) => 123); // $ExpectType Color
set("red", "foo", 123); // $ExpectType PlainColorObject
set(new Color("red"), ["srgb", "bar"], (_: number) => 123); // $ExpectType PlainColorObject
set(new Color("red"), [sRGB, "bar"], (_: number) => 123); // $ExpectType PlainColorObject

// $ExpectType Color
// $ExpectType PlainColorObject
set("red", {
foo: 123,
bar: (_: number) => 123,
});

new Color("red").set("foo", 123); // $ExpectType Color
9 changes: 6 additions & 3 deletions types/test/setAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ setAll(new Color("red"), "srgb");
// @ts-expect-error
setAll(new Color("red"), sRGB);

setAll(new Color("red"), "srgb", [1, 2, 3]); // $ExpectType Color
setAll(new Color("red"), sRGB, [1, 2, 3]); // $ExpectType Color
// $ExpectType { coords: [number, number, number]; space: RGBColorSpace; alpha: number; }
setAll(new Color("red"), "srgb", [1, 2, 3]); // $ExpectType PlainColorObject
setAll(new Color("red"), sRGB, [1, 2, 3]); // $ExpectType PlainColorObject
// $ExpectType PlainColorObject
setAll(
{
coords: [1, 2, 3],
Expand All @@ -23,3 +23,6 @@ setAll(
"srgb_linear",
[4, 5, 6],
);

new Color("red").setAll("srgb", [1, 2, 3]); // $ExpectType Color
new Color("red").setAll(sRGB, [1, 2, 3]); // $ExpectType Color
12 changes: 12 additions & 0 deletions types/test/spaces.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Color from "colorjs.io/src";
import ColorSpace from "colorjs.io/src/space";

// @ts-expect-error
Expand Down Expand Up @@ -47,3 +48,14 @@ ColorSpace.registry["abc"]; // $ExpectType ColorSpace

ColorSpace.resolveCoord("p3.0", "p3");
ColorSpace.resolveCoord(["p3", "r"], "p3");

space.to(new Color("red")); // $ExpectType Coords
space.to({space: space, coords: [1, 2, 3], alpha: 1}); // $ExpectType Coords
space.to(space, [1, 2, 3]); // $ExpectType Coords
space.to("srgb", [1, 2, 3]); // $ExpectType Coords

space.from(new Color("red")); // $ExpectType Coords
space.from({space: space, coords: [1, 2, 3], alpha: 1}); // $ExpectType Coords
space.from(space, [1, 2, 3]); // $ExpectType Coords
space.from("srgb", [1, 2, 3]); // $ExpectType Coords

6 changes: 6 additions & 0 deletions types/test/toGamut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ toGamut(new Color("red")); // $ExpectType PlainColorObject
toGamut(new Color("red"), { method: "clip", space: "srgb" }); // $ExpectType PlainColorObject
toGamut(new Color("red"), { method: "clip", space: sRGB }); // $ExpectType PlainColorObject
toGamut(new Color("red"), "srgb"); // $ExpectType PlainColorObject


new Color("red").toGamut(); // $ExpectType Color
new Color("red").toGamut({ method: "clip", space: "srgb" }); // $ExpectType Color
new Color("red").toGamut({ method: "clip", space: sRGB }); // $ExpectType Color
new Color("red").toGamut("srgb"); // $ExpectType Color

0 comments on commit 2c93907

Please sign in to comment.