| 1 |
import ColorSpace from "./ColorSpace.js"; |
| 2 |
import { isString, isInstance } from "./util.js"; |
| 3 |
import parse from "./parse.js"; |
| 4 |
|
| 5 |
/** @import { ColorTypes, ParseOptions as GetColorOptions, PlainColorObject } from "./types.js" */ |
| 6 |
|
| 7 |
/** |
| 8 |
* Resolves a color reference (object or string) to a plain color object |
| 9 |
* @overload |
| 10 |
* @param {ColorTypes} color |
| 11 |
* @param {GetColorOptions} [options] |
| 12 |
* @returns {PlainColorObject} |
| 13 |
*/ |
| 14 |
/** |
| 15 |
* @overload |
| 16 |
* @param {ColorTypes[]} color |
| 17 |
* @param {GetColorOptions} [options] |
| 18 |
* @returns {PlainColorObject[]} |
| 19 |
*/ |
| 20 |
export default function getColor (color, options) { |
| 21 |
if (Array.isArray(color)) { |
| 22 |
return color.map(c => getColor(c, options)); |
| 23 |
} |
| 24 |
|
| 25 |
if (!color) { |
| 26 |
throw new TypeError("Empty color reference"); |
| 27 |
} |
| 28 |
|
| 29 |
if (isString(color)) { |
| 30 |
color = parse(color, options); |
| 31 |
} |
| 32 |
|
| 33 |
// Object fixup |
| 34 |
let space = color.space || color.spaceId; |
| 35 |
|
| 36 |
if (typeof space === "string") { |
| 37 |
// Convert string id to color space object |
| 38 |
color.space = ColorSpace.get(space); |
| 39 |
} |
| 40 |
|
| 41 |
if (color.alpha === undefined) { |
| 42 |
color.alpha = 1; |
| 43 |
} |
| 44 |
|
| 45 |
return color; |
| 46 |
} |