diff --git a/src/getAll.js b/src/getAll.js index d1b17890f..f7848492c 100644 --- a/src/getAll.js +++ b/src/getAll.js @@ -1,23 +1,46 @@ import ColorSpace from "./ColorSpace.js"; import getColor from "./getColor.js"; +import { toPrecision } from "./util.js"; /** @typedef {import("./types.js").ColorTypes} ColorTypes */ /** @typedef {import("./types.js").Coords} Coords */ +/** + * Options for {@link getAll} + * @typedef GetAllOptions + * @property {string | ColorSpace | undefined} [space] + * The color space to convert to. Defaults to the color's current space + * @property {number | undefined} [precision] + * The number of significant digits to round the coordinates to + */ + /** * Get the coordinates of a color in any color space + * @overload * @param {ColorTypes} color - * @param {string | ColorSpace} [space] The color space to convert to. Defaults to the color's current space + * @param {string | ColorSpace} [options=color.space] The color space to convert to. Defaults to the color's current space * @returns {Coords} The color coordinates in the given color space */ -export default function getAll (color, space) { +/** + * @overload + * @param {ColorTypes} color + * @param {GetAllOptions} [options] + * @returns {Coords} The color coordinates in the given color space + */ +export default function getAll (color, options) { color = getColor(color); + let space = ColorSpace.get(options, options?.space); + let precision = options?.precision; + + let coords; if (!space || color.space.equals(space)) { // No conversion needed - return color.coords.slice(); + coords = color.coords.slice(); + } + else { + coords = space.from(color); } - space = ColorSpace.get(space); - return space.from(color); + return precision === undefined ? coords : coords.map(coord => toPrecision(coord, precision)); } diff --git a/test/coords.js b/test/coords.js index 560e292fb..a8fe84d87 100644 --- a/test/coords.js +++ b/test/coords.js @@ -28,6 +28,20 @@ export default { }, expect: [1, 0, 0], }, + { + name: "color.getAll({precision: 1})", + run () { + return this.data.red_oklch.getAll({precision: 1}); + }, + expect: [.6, .3, 30], + }, + { + name: "color.getAll({space: 'oklch', precision: 1})", + run () { + return this.data.red.getAll({space: "oklch", precision: 1}); + }, + expect: [.6, .3, 30], + }, { name: "color.alpha", run () { diff --git a/types/test/getAll.ts b/types/test/getAll.ts index 86386c7e9..be400b0d9 100644 --- a/types/test/getAll.ts +++ b/types/test/getAll.ts @@ -8,6 +8,17 @@ getAll(); getAll(new Color("red")); // $ExpectType Coords getAll(new Color("red"), "srgb"); // $ExpectType Coords getAll(new Color("red"), sRGB); // $ExpectType Coords +getAll(new Color("red"), {space: "srgb"}); // $ExpectType Coords +getAll(new Color("red"), {space: sRGB}); // $ExpectType Coords +getAll(new Color("red"), {space: "srgb", precision: 1}); // $ExpectType Coords +getAll(new Color("red"), {space: sRGB, precision: 1}); // $ExpectType Coords +getAll(new Color("red"), {precision: 1}); // $ExpectType Coords getAll("red", sRGB); // $ExpectType Coords +getAll("red", {space: sRGB}); // $ExpectType Coords +getAll("red", {space: sRGB, precision: 1}); // $ExpectType Coords +getAll("red", {precision: 1}); // $ExpectType Coords new Color("red").getAll(); // $ExpectType Coords +new Color("red").getAll({space: "srgb"}); // $ExpectType Coords +new Color("red").getAll({space: "srgb", precision: 1}); // $ExpectType Coords +new Color("red").getAll({precision: 1}); // $ExpectType Coords