Debian Codegraph @

node/colorjs.io/src/getAll.js

raw
1 import ColorSpace from "./ColorSpace.js";
2 import getColor from "./getColor.js";
3 import { toPrecision } from "./util.js";
4
5 /** @import { ColorTypes, Coords } from "./types.js" */
6
7 /**
8 * Options for {@link getAll}
9 * @typedef GetAllOptions
10 * @property {string | ColorSpace | undefined} [space]
11 * The color space to convert to. Defaults to the color's current space
12 * @property {number | undefined} [precision]
13 * The number of significant digits to round the coordinates to
14 */
15
16 /**
17 * Get the coordinates of a color in any color space
18 * @overload
19 * @param {ColorTypes} color
20 * @param {string | ColorSpace} [options=color.space] The color space to convert to. Defaults to the color's current space
21 * @returns {Coords} The color coordinates in the given color space
22 */
23 /**
24 * @overload
25 * @param {ColorTypes} color
26 * @param {GetAllOptions} [options]
27 * @returns {Coords} The color coordinates in the given color space
28 */
29 export default function getAll (color, options) {
30 color = getColor(color);
31
32 let space = ColorSpace.get(options, options?.space);
33 let precision = options?.precision;
34
35 let coords;
36 if (!space || color.space.equals(space)) {
37 // No conversion needed
38 coords = color.coords.slice();
39 }
40 else {
41 coords = space.from(color);
42 }
43
44 return precision === undefined ? coords : coords.map(coord => toPrecision(coord, precision));
45 }