-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathgetColor.js
50 lines (42 loc) · 1.22 KB
/
getColor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import ColorSpace from "./ColorSpace.js";
import {isString, isInstance} from "./util.js";
import parse from "./parse.js";
// Type "imports"
/** @typedef {import("./types.js").ColorTypes} ColorTypes */
/** @typedef {import("./types.js").PlainColorObject} PlainColorObject */
/**
* Resolves a color reference (object or string) to a plain color object
* @overload
* @param {ColorTypes} color
* @param {object} [options]
* @param {boolean} [options.parseMeta] Optional object to hold parsing metadata
* @returns {PlainColorObject}
*/
/**
* @overload
* @param {ColorTypes[]} color
* @param {object} [options]
* @param {boolean} [options.parseMeta] Optional object to hold parsing metadata
* @returns {PlainColorObject[]}
*/
export default function getColor (color, options) {
if (Array.isArray(color)) {
return color.map(c => getColor(c, options));
}
if (!color) {
throw new TypeError("Empty color reference");
}
if (isString(color)) {
color = parse(color, options);
}
// Object fixup
let space = color.space || color.spaceId;
if (typeof space === "string") {
// Convert string id to color space object
color.space = ColorSpace.get(space);
}
if (color.alpha === undefined) {
color.alpha = 1;
}
return color;
}