Debian Codegraph @

node/colorjs.io/src/to.js

raw
1 import getColor from "./getColor.js";
2 import ColorSpace from "./ColorSpace.js";
3 import toGamut from "./toGamut.js";
4
5 /** @import { ColorTypes, PlainColorObject, ToGamutOptions } from "./types.js" */
6
7 /**
8 * Convert to color space and return a new color
9 * @param {ColorTypes} color
10 * @param {string | ColorSpace} space
11 * @param {{ inGamut?: boolean | ToGamutOptions | undefined }} options
12 * @returns {PlainColorObject}
13 */
14 export default function to (color, space, { inGamut } = {}) {
15 color = getColor(color);
16 space = ColorSpace.get(space);
17
18 let coords = space.from(color);
19 let ret = { space, coords, alpha: color.alpha };
20
21 if (inGamut) {
22 ret = toGamut(ret, inGamut === true ? undefined : inGamut);
23 }
24
25 return ret;
26 }
27
28 /** @type {"color"} */
29 to.returns = "color";