| 1 |
import ColorSpace from "./ColorSpace.js"; |
| 2 |
import getColor from "./getColor.js"; |
| 3 |
|
| 4 |
/** @import { ColorTypes, Coords, PlainColorObject } from "./types.js" */ |
| 5 |
|
| 6 |
/** |
| 7 |
* Set all coordinates of a color at once, in its own color space or another. |
| 8 |
* Modifies the color in place. |
| 9 |
* @overload |
| 10 |
* @param {ColorTypes} color |
| 11 |
* @param {Coords} coords Array of coordinates |
| 12 |
* @param {number} [alpha] |
| 13 |
* @returns {PlainColorObject} |
| 14 |
*/ |
| 15 |
/** |
| 16 |
* @overload |
| 17 |
* @param {ColorTypes} color |
| 18 |
* @param {string | ColorSpace} space The color space of the provided coordinates. |
| 19 |
* @param {Coords} coords Array of coordinates |
| 20 |
* @param {number} [alpha] |
| 21 |
* @returns {PlainColorObject} |
| 22 |
*/ |
| 23 |
export default function setAll (color, space, coords, alpha) { |
| 24 |
color = getColor(color); |
| 25 |
|
| 26 |
if (Array.isArray(space)) { |
| 27 |
// Space is omitted |
| 28 |
[space, coords, alpha] = [color.space, space, coords]; |
| 29 |
} |
| 30 |
|
| 31 |
space = ColorSpace.get(space); // Make sure we have a ColorSpace object |
| 32 |
color.coords = space === color.space ? coords.slice() : space.to(color.space, coords); |
| 33 |
|
| 34 |
if (alpha !== undefined) { |
| 35 |
color.alpha = alpha; |
| 36 |
} |
| 37 |
|
| 38 |
return color; |
| 39 |
} |
| 40 |
|
| 41 |
/** @type {"color"} */ |
| 42 |
setAll.returns = "color"; |