| 1 |
import ColorSpace from "./ColorSpace.js"; |
| 2 |
import getColor from "./getColor.js"; |
| 3 |
import get from "./get.js"; |
| 4 |
import getAll from "./getAll.js"; |
| 5 |
import setAll from "./setAll.js"; |
| 6 |
import { type } from "./util.js"; |
| 7 |
|
| 8 |
/** @import { ColorTypes, PlainColorObject, Ref } from "./types.js" */ |
| 9 |
|
| 10 |
/** |
| 11 |
* Set properties and return current instance |
| 12 |
* @overload |
| 13 |
* @param {ColorTypes} color |
| 14 |
* @param {Ref} prop |
| 15 |
* @param {number | ((coord: number) => number)} value |
| 16 |
* @returns {PlainColorObject} |
| 17 |
*/ |
| 18 |
/** |
| 19 |
* @overload |
| 20 |
* @param {ColorTypes} color |
| 21 |
* @param {Record<string, number | ((coord: number) => number)>} props |
| 22 |
* @returns {PlainColorObject} |
| 23 |
*/ |
| 24 |
export default function set (color, prop, value) { |
| 25 |
color = getColor(color); |
| 26 |
|
| 27 |
if (arguments.length === 2 && type(arguments[1]) === "object") { |
| 28 |
// Argument is an object literal |
| 29 |
let object = arguments[1]; |
| 30 |
for (let p in object) { |
| 31 |
set(color, p, object[p]); |
| 32 |
} |
| 33 |
} |
| 34 |
else { |
| 35 |
if (typeof value === "function") { |
| 36 |
value = value(get(color, prop)); |
| 37 |
} |
| 38 |
|
| 39 |
if (prop === "alpha") { |
| 40 |
color.alpha = value; |
| 41 |
} |
| 42 |
else { |
| 43 |
let { space, index } = ColorSpace.resolveCoord(prop, color.space); |
| 44 |
let coords = getAll(color, space); |
| 45 |
coords[index] = value; |
| 46 |
setAll(color, space, coords); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
return color; |
| 51 |
} |
| 52 |
|
| 53 |
/** @type {"color"} */ |
| 54 |
set.returns = "color"; |