Skip to content

Commit

Permalink
Space should be optional in setAll(), fixes #519
Browse files Browse the repository at this point in the history
  • Loading branch information
LeaVerou committed May 22, 2024
1 parent d699db0 commit 77c09ad
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
18 changes: 16 additions & 2 deletions src/setAll.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import ColorSpace from "./space.js";
import getColor from "./getColor.js";

/**
* Set all coordinates of a color at once, in its own color space or another.
* Modifies the color in place.
* @param {Color} color
* @param {ColorSpace | string} [space=color.space] The color space of the provided coordinates.
* @param {Array<number>} coords Array of coordinates
* @returns {Color}
*/
export default function setAll (color, space, coords) {
color = getColor(color);

space = ColorSpace.get(space);
color.coords = space.to(color.space, coords);
if (Array.isArray(space)) {
// Space is omitted
[space, coords, alpha] = [color.space, space, coords];

Check warning on line 17 in src/setAll.js

View workflow job for this annotation

GitHub Actions / Lint & Test Types

'alpha' is not defined
}

space = ColorSpace.get(space); // Make sure we have a ColorSpace object
color.coords = space === color.space ? coords.slice() : space.to(color.space, coords);

return color;
}

Expand Down
21 changes: 20 additions & 1 deletion test/coords.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,25 @@ export default {
},
expect: 1,
},
{
name: "color.setAll(newCoords)",
run () {
let color = new Color("srgb", [0, 1, 0]);
color.setAll([1, 0, 1]);
return [...color.coords];
},
expect: [1, 0, 1],
},
{
name: "color.setAll(space, newCoords)",
run () {
let color = this.data.red_oklch.clone();
color.setAll("srgb", [1, 0, 1]);
return [...color.coords];
},
// https://colorjs.io/apps/convert/?color=%23f0f&precision=4
expect: [0.7016738591017413, 0.32249098770537216, 328.36341517499017],
},
{
name: "color.coords[index] = value",
run () {
Expand Down Expand Up @@ -149,7 +168,7 @@ export default {
expect: 13.480970445148008,
},
{
name: "color.set(coordsObject)",
name: "color.set(object_with_coords)",
run () {
let color = this.data.red.clone();
color.set({"lch.c": 13, "lch.l": 40});
Expand Down

0 comments on commit 77c09ad

Please sign in to comment.