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

Make color space optional in getAll() + colorSpace.equals() improvements #413

Merged
merged 3 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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