| 1 |
import { isNone, skipNone } from "./util.js"; |
| 2 |
import defaults from "./defaults.js"; |
| 3 |
import to from "./to.js"; |
| 4 |
import serialize from "./serialize.js"; |
| 5 |
import clone from "./clone.js"; |
| 6 |
import getColor from "./getColor.js"; |
| 7 |
import REC2020 from "./spaces/rec2020.js"; |
| 8 |
import P3 from "./spaces/p3.js"; |
| 9 |
import Lab from "./spaces/lab.js"; |
| 10 |
import sRGB from "./spaces/srgb.js"; |
| 11 |
|
| 12 |
/** @import ColorSpace from "./ColorSpace.js" */ |
| 13 |
/** @import { ColorTypes, PlainColorObject, DisplayOptions } from "./types.js" */ |
| 14 |
|
| 15 |
// Type re-exports |
| 16 |
/** @typedef {import("./types.js").Display} Display */ |
| 17 |
|
| 18 |
// Default space for CSS output. Code in Color.js makes this wider if there's a DOM available |
| 19 |
defaults.display_space = sRGB; |
| 20 |
|
| 21 |
let supportsNone; |
| 22 |
|
| 23 |
if (typeof CSS !== "undefined" && CSS.supports) { |
| 24 |
// Find widest supported color space for CSS |
| 25 |
for (let space of [Lab, REC2020, P3]) { |
| 26 |
let coords = space.getMinCoords(); |
| 27 |
let color = { space, coords, alpha: 1 }; |
| 28 |
let str = serialize(color); |
| 29 |
|
| 30 |
if (CSS.supports("color", str)) { |
| 31 |
defaults.display_space = space; |
| 32 |
break; |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Returns a serialization of the color that can actually be displayed in the browser. |
| 39 |
* If the default serialization can be displayed, it is returned. |
| 40 |
* Otherwise, if a `space` is given, the color is converted directly to it. |
| 41 |
* If not, the color is converted to the first supported of its space's `displaySpaces` (if any), |
| 42 |
* else the closest supported space in its base color space chain (which preserves its gamut), |
| 43 |
* else the default fallback space (Lab, REC2020, or P3, whichever is the widest supported). |
| 44 |
* In Node.js, this is basically equivalent to `serialize()` but returns a `String` object instead. |
| 45 |
* @param {ColorTypes} color |
| 46 |
* @param {DisplayOptions} [options] Options. Any properties beyond `space` and `supports` are passed to `serialize()`. |
| 47 |
* @returns {Display} String object containing the serialized color |
| 48 |
* with a color property containing the converted color (or the original, if no conversion was necessary) |
| 49 |
*/ |
| 50 |
export default function display ( |
| 51 |
color, |
| 52 |
{ space, supports = globalThis.CSS?.supports, ...options } = {}, |
| 53 |
) { |
| 54 |
color = getColor(color); |
| 55 |
|
| 56 |
let ret = /** @type {Display} */ (serialize(color, options)); |
| 57 |
|
| 58 |
if (!supports || supports("color", /** @type {string} */ (ret)) || !defaults.display_space) { |
| 59 |
ret = /** @type {Display} */ (new String(ret)); |
| 60 |
ret.color = /** @type {PlainColorObject} */ (color); |
| 61 |
} |
| 62 |
else { |
| 63 |
// If we're here, what we were about to output is not supported |
| 64 |
let fallbackColor = /** @type {PlainColorObject} */ (color); |
| 65 |
|
| 66 |
// First, check if the culprit is none values |
| 67 |
let hasNone = color.coords.some(isNone) || isNone(color.alpha); |
| 68 |
|
| 69 |
if (hasNone) { |
| 70 |
// Does the browser support none values? |
| 71 |
if (!(supportsNone ??= supports("color", "hsl(none 50% 50%)"))) { |
| 72 |
// Nope, try again without none |
| 73 |
fallbackColor = clone(/** @type {PlainColorObject} */ (color)); |
| 74 |
fallbackColor.coords = /** @type {[number, number, number]} */ ( |
| 75 |
fallbackColor.coords.map(skipNone) |
| 76 |
); |
| 77 |
fallbackColor.alpha = skipNone(fallbackColor.alpha); |
| 78 |
|
| 79 |
// @ts-expect-error This is set to the correct type later |
| 80 |
ret = serialize(fallbackColor, options); |
| 81 |
|
| 82 |
if (supports("color", /** @type {string} */ (ret))) { |
| 83 |
// We're done, now it's supported |
| 84 |
ret = /** @type {Display} */ (new String(ret)); |
| 85 |
ret.color = fallbackColor; |
| 86 |
return ret; |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
// If we're here, the color function is not supported. |
| 92 |
// With no explicit space, try the color's own `displaySpaces`, or else walk up its base |
| 93 |
// color space chain (closest first, skipping XYZ connection spaces), and use the first |
| 94 |
// space the browser supports, before falling back to the default space. |
| 95 |
if (space === undefined) { |
| 96 |
space = fallbackColor.space; |
| 97 |
let candidates = space.displaySpaces ?? space.bases; |
| 98 |
|
| 99 |
// Skip XYZ connection spaces (unless the space itself is one, so it still has a fallback). |
| 100 |
if (!space.displaySpaces && !space.id.startsWith("xyz-")) { |
| 101 |
candidates = candidates.filter(space => !space.id.startsWith("xyz-")); |
| 102 |
} |
| 103 |
|
| 104 |
for (let candidate of candidates) { |
| 105 |
let candidateColor = to(fallbackColor, candidate); |
| 106 |
let str = serialize(candidateColor, options); |
| 107 |
|
| 108 |
if (supports("color", /** @type {string} */ (str))) { |
| 109 |
ret = /** @type {Display} */ (new String(str)); |
| 110 |
ret.color = candidateColor; |
| 111 |
return ret; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
space = defaults.display_space; |
| 116 |
} |
| 117 |
|
| 118 |
// Fall back to fallback space |
| 119 |
fallbackColor = to(fallbackColor, /** @type {string | ColorSpace} */ (space)); |
| 120 |
ret = /** @type {Display} */ (new String(serialize(fallbackColor, options))); |
| 121 |
ret.color = fallbackColor; |
| 122 |
} |
| 123 |
|
| 124 |
return ret; |
| 125 |
} |