UNPKG

@wordpress/components

Version:
102 lines (101 loc) • 3.59 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); // \0rolldown/runtime.js var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) { key = keys[i]; if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: ((k) => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); let memize = require("memize"); memize = __toESM(memize); let colord = require("colord"); let colord_plugins_names = require("colord/plugins/names"); colord_plugins_names = __toESM(colord_plugins_names); // packages/components/src/utils/colors.ts let colorComputationNode; (0, colord.extend)([colord_plugins_names.default]); /** * @return The HTML element for color computation. */ function getColorComputationNode() { if (typeof document === "undefined") return; if (!colorComputationNode) { const el = document.createElement("div"); el.setAttribute("data-g2-color-computation-node", ""); document.body.appendChild(el); colorComputationNode = el; } return colorComputationNode; } /** * @param value * * @return Whether the value is a valid color. */ function isColor(value) { if (typeof value !== "string") return false; return (0, colord.colord)(value).isValid(); } /** * Retrieves the computed background color. This is useful for getting the * value of a CSS variable color. * * @param backgroundColor The background color to compute. * * @return The computed background color. */ function _getComputedBackgroundColor(backgroundColor) { if (typeof backgroundColor !== "string") return ""; if (isColor(backgroundColor)) return backgroundColor; if (!backgroundColor.includes("var(")) return ""; if (typeof document === "undefined") return ""; const el = getColorComputationNode(); if (!el) return ""; el.style.background = backgroundColor; const computedColor = window?.getComputedStyle(el).background; el.style.background = ""; return computedColor || ""; } const getComputedBackgroundColor = (0, memize.default)(_getComputedBackgroundColor); /** * Get the text shade optimized for readability, based on a background color. * * @param backgroundColor The background color. * * @return The optimized text color (black or white). */ function getOptimalTextColor(backgroundColor) { const background = getComputedBackgroundColor(backgroundColor); return (0, colord.colord)(background).isLight() ? "#000000" : "#ffffff"; } /** * Get the text shade optimized for readability, based on a background color. * * @param backgroundColor The background color. * * @return The optimized text shade (dark or light). */ function getOptimalTextShade(backgroundColor) { return getOptimalTextColor(backgroundColor) === "#000000" ? "dark" : "light"; } exports.getOptimalTextColor = getOptimalTextColor; exports.getOptimalTextShade = getOptimalTextShade; //# sourceMappingURL=colors.cjs.map