Skip to content

Commit

Permalink
Make color space optional in getAll() + colorSpace.equals() impro…
Browse files Browse the repository at this point in the history
…vements (color-js#413)

* `colorSpace.equals()` improvements

- JSDoc
- Handle comparison with id

* `getAll()`: Make color space optional

This is to set the stage for option 3 here: color-js#388 (comment) but also implements a performance improvement when the color passed *happens* to be in the same color space as the one specified.

* Update types for getAll

---------

Co-authored-by: Jonny Gerig Meyer <[email protected]>
  • Loading branch information
LeaVerou and jgerigmeyer authored Feb 10, 2024
1 parent a8bc6fd commit 64780c3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
13 changes: 9 additions & 4 deletions src/getAll.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import ColorSpace from "./space.js";

/**
* Get the coordinates of a color in another color space
*
* @param {string | ColorSpace} space
* @returns {number[]}
* Get the coordinates of a color in any color space
* @param {Color} color
* @param {string | ColorSpace} [space = color.space] The color space to convert to. Defaults to the color's current space
* @returns {number[]} The color coordinates in the given color space
*/
export default function getAll (color, space) {
if (!space || color.space.equals(space)) {
// No conversion needed
return color.coords.slice();
}

space = ColorSpace.get(space);
return space.from(color);
}
9 changes: 7 additions & 2 deletions src/space.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,18 @@ export default class ColorSpace {
return null;
}

// We cannot rely on simple === because then ColorSpace objects cannot be proxied
/**
* Check if this color space is the same as another color space reference.
* Allows proxying color space objects and comparing color spaces with ids.
* @param {string | ColorSpace} space ColorSpace object or id to compare to
* @returns {boolean}
*/
equals (space) {
if (!space) {
return false;
}

return this === space || this.id === space.id;
return this === space || this.id === space || this.id === space.id;
}

to (space, coords) {
Expand Down
2 changes: 1 addition & 1 deletion types/src/getAll.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import ColorSpace from "./space.js";

export default function getAll (
color: Color | ColorObject,
space: string | ColorSpace
space?: string | ColorSpace
): [number, number, number];
3 changes: 1 addition & 2 deletions types/test/getAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import sRGB from "colorjs.io/src/spaces/srgb";

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

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]

0 comments on commit 64780c3

Please sign in to comment.