diff --git a/src/setAll.js b/src/setAll.js index 54afeeba1..7c8aba4ae 100644 --- a/src/setAll.js +++ b/src/setAll.js @@ -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} 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]; + } + + 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; } diff --git a/test/coords.js b/test/coords.js index e3227a4c6..dc6c369cd 100644 --- a/test/coords.js +++ b/test/coords.js @@ -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 () { @@ -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});