| 1 |
// @ts-nocheck |
| 2 |
|
| 3 |
import * as util from "./util.js"; |
| 4 |
import ColorSpace from "./ColorSpace.js"; |
| 5 |
import defaults from "./defaults.js"; |
| 6 |
import getColor from "./getColor.js"; |
| 7 |
import to from "./to.js"; |
| 8 |
import checkInGamut from "./inGamut.js"; |
| 9 |
import toGamut from "./toGamut.js"; |
| 10 |
import clone from "./clone.js"; |
| 11 |
|
| 12 |
/** @import { ColorTypes, ParseOptions, PlainColorObject } from "./types.js" */ |
| 13 |
|
| 14 |
// Type re-exports |
| 15 |
/** @typedef {import("./types.js").SerializeOptions} SerializeOptions */ |
| 16 |
|
| 17 |
/** |
| 18 |
* Generic toString() method, outputs a color(spaceId ...coords) function, a functional syntax, or custom formats defined by the color space |
| 19 |
* @param {ColorTypes} color |
| 20 |
* @param {SerializeOptions & Record<string, any>} options |
| 21 |
* @returns {string} |
| 22 |
*/ |
| 23 |
export default function serialize (color, options = {}) { |
| 24 |
let { |
| 25 |
precision = defaults.precision, |
| 26 |
format, |
| 27 |
inGamut = true, |
| 28 |
coords: coordFormat, |
| 29 |
alpha: alphaFormat, |
| 30 |
commas, |
| 31 |
} = options; |
| 32 |
let ret; |
| 33 |
|
| 34 |
let colorWithMeta = /** @type {PlainColorObject & ParseOptions} */ (getColor(color)); |
| 35 |
|
| 36 |
let formatId = format; |
| 37 |
let parseMeta = colorWithMeta.parseMeta; |
| 38 |
|
| 39 |
if (parseMeta && !format) { |
| 40 |
if (parseMeta.format.canSerialize()) { |
| 41 |
format = parseMeta.format; |
| 42 |
formatId = parseMeta.formatId; |
| 43 |
} |
| 44 |
|
| 45 |
coordFormat ??= parseMeta.types; |
| 46 |
alphaFormat ??= parseMeta.alphaType; |
| 47 |
commas ??= parseMeta.commas; |
| 48 |
} |
| 49 |
|
| 50 |
if (formatId) { |
| 51 |
// A format is explicitly specified |
| 52 |
format = colorWithMeta.space.getFormat(format) ?? ColorSpace.findFormat(formatId); |
| 53 |
} |
| 54 |
|
| 55 |
if (!format) { |
| 56 |
// No format specified, or format not found |
| 57 |
format = colorWithMeta.space.getFormat("default") ?? ColorSpace.DEFAULT_FORMAT; |
| 58 |
formatId = format.name; |
| 59 |
} |
| 60 |
|
| 61 |
if (format && format.space && format.space !== colorWithMeta.space) { |
| 62 |
// Format specified belongs to a different color space, |
| 63 |
// need to convert to it first |
| 64 |
colorWithMeta = to(colorWithMeta, format.space); |
| 65 |
} |
| 66 |
|
| 67 |
// The assignment to coords and inGamut needs to stay in the order they are now |
| 68 |
// The order of the assignment was changed as a workaround for a bug in Next.js |
| 69 |
// See this issue for details: https://github.com/color-js/color.js/issues/260 |
| 70 |
|
| 71 |
let coords = colorWithMeta.coords.slice(); // clone so we can manipulate it |
| 72 |
|
| 73 |
inGamut ||= format.toGamut; |
| 74 |
|
| 75 |
if (inGamut && !checkInGamut(colorWithMeta)) { |
| 76 |
// FIXME what happens if the color contains none values? |
| 77 |
coords = toGamut(clone(colorWithMeta), inGamut === true ? undefined : inGamut).coords; |
| 78 |
} |
| 79 |
|
| 80 |
if (format.type === "custom") { |
| 81 |
if (format.serialize) { |
| 82 |
ret = format.serialize(coords, colorWithMeta.alpha, options); |
| 83 |
} |
| 84 |
else { |
| 85 |
throw new TypeError( |
| 86 |
`format ${formatId} can only be used to parse colors, not for serialization`, |
| 87 |
); |
| 88 |
} |
| 89 |
} |
| 90 |
else { |
| 91 |
// Functional syntax |
| 92 |
let name = format.name || "color"; |
| 93 |
|
| 94 |
let args = format.serializeCoords(coords, precision, coordFormat); |
| 95 |
|
| 96 |
if (name === "color") { |
| 97 |
// If output is a color() function, add colorspace id as first argument |
| 98 |
let cssId = |
| 99 |
format.id || format.ids?.[0] || colorWithMeta.space.cssId || colorWithMeta.space.id; |
| 100 |
args.unshift(cssId); |
| 101 |
} |
| 102 |
|
| 103 |
// Serialize alpha? |
| 104 |
/** @type {string | number} */ |
| 105 |
let alpha = colorWithMeta.alpha; |
| 106 |
|
| 107 |
if (alphaFormat !== undefined && !(typeof alphaFormat === "object")) { |
| 108 |
alphaFormat = |
| 109 |
typeof alphaFormat === "string" ? { type: alphaFormat } : { include: alphaFormat }; |
| 110 |
} |
| 111 |
|
| 112 |
let alphaType = alphaFormat?.type ?? "<number>"; |
| 113 |
let serializeAlpha = |
| 114 |
alphaFormat?.include === true || |
| 115 |
format.alpha === true || |
| 116 |
(alphaFormat?.include !== false && format.alpha !== false && alpha < 1); |
| 117 |
let strAlpha = ""; |
| 118 |
|
| 119 |
commas ??= format.commas; |
| 120 |
|
| 121 |
if (serializeAlpha) { |
| 122 |
if (precision !== null) { |
| 123 |
let unit; |
| 124 |
|
| 125 |
if (alphaType === "<percentage>") { |
| 126 |
unit = "%"; |
| 127 |
alpha *= 100; |
| 128 |
} |
| 129 |
|
| 130 |
alpha = util.serializeNumber(alpha, { precision, unit }); |
| 131 |
} |
| 132 |
|
| 133 |
strAlpha = `${commas ? "," : " /"} ${alpha}`; |
| 134 |
} |
| 135 |
|
| 136 |
ret = `${name}(${args.join(commas ? ", " : " ")}${strAlpha})`; |
| 137 |
} |
| 138 |
|
| 139 |
return ret; |
| 140 |
} |