@wordpress/components
Version:
UI components for WordPress.
8 lines (7 loc) • 4.6 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/utils/colors.js"],
"sourcesContent": ["/**\n * External dependencies\n */\nimport memoize from 'memize';\nimport { colord, extend } from 'colord';\nimport namesPlugin from 'colord/plugins/names';\n\n/** @type {HTMLDivElement} */\nlet colorComputationNode;\nextend([namesPlugin]);\n\n/**\n * Generating a CSS compliant rgba() color value.\n *\n * @param {string} hexValue The hex value to convert to rgba().\n * @param {number} alpha The alpha value for opacity.\n * @return {string} The converted rgba() color value.\n *\n * @example\n * rgba( '#000000', 0.5 )\n * // rgba(0, 0, 0, 0.5)\n */\nexport function rgba(hexValue = '', alpha = 1) {\n return colord(hexValue).alpha(alpha).toRgbString();\n}\n\n/**\n * @return {HTMLDivElement | undefined} The HTML element for color computation.\n */\nfunction getColorComputationNode() {\n if (typeof document === 'undefined') {\n return;\n }\n if (!colorComputationNode) {\n // Create a temporary element for style computation.\n const el = document.createElement('div');\n el.setAttribute('data-g2-color-computation-node', '');\n // Inject for window computed style.\n document.body.appendChild(el);\n colorComputationNode = el;\n }\n return colorComputationNode;\n}\n\n/**\n * @param {string | unknown} value\n *\n * @return {boolean} Whether the value is a valid color.\n */\nfunction isColor(value) {\n if (typeof value !== 'string') {\n return false;\n }\n const test = colord(value);\n return test.isValid();\n}\n\n/**\n * Retrieves the computed background color. This is useful for getting the\n * value of a CSS variable color.\n *\n * @param {string | unknown} backgroundColor The background color to compute.\n *\n * @return {string} The computed background color.\n */\nfunction _getComputedBackgroundColor(backgroundColor) {\n if (typeof backgroundColor !== 'string') {\n return '';\n }\n if (isColor(backgroundColor)) {\n return backgroundColor;\n }\n if (!backgroundColor.includes('var(')) {\n return '';\n }\n if (typeof document === 'undefined') {\n return '';\n }\n\n // Attempts to gracefully handle CSS variables color values.\n const el = getColorComputationNode();\n if (!el) {\n return '';\n }\n el.style.background = backgroundColor;\n // Grab the style.\n const computedColor = window?.getComputedStyle(el).background;\n // Reset.\n el.style.background = '';\n return computedColor || '';\n}\nconst getComputedBackgroundColor = memoize(_getComputedBackgroundColor);\n\n/**\n * Get the text shade optimized for readability, based on a background color.\n *\n * @param {string | unknown} backgroundColor The background color.\n *\n * @return {string} The optimized text color (black or white).\n */\nexport function getOptimalTextColor(backgroundColor) {\n const background = getComputedBackgroundColor(backgroundColor);\n return colord(background).isLight() ? '#000000' : '#ffffff';\n}\n\n/**\n * Get the text shade optimized for readability, based on a background color.\n *\n * @param {string | unknown} backgroundColor The background color.\n *\n * @return {string} The optimized text shade (dark or light).\n */\nexport function getOptimalTextShade(backgroundColor) {\n const result = getOptimalTextColor(backgroundColor);\n return result === '#000000' ? 'dark' : 'light';\n}"],
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,oBAAoB;AACpB,oBAA+B;AAC/B,mBAAwB;AAGxB,IAAI;AAAA,IACJ,sBAAO,CAAC,aAAAA,OAAW,CAAC;AAab,SAAS,KAAK,WAAW,IAAI,QAAQ,GAAG;AAC7C,aAAO,sBAAO,QAAQ,EAAE,MAAM,KAAK,EAAE,YAAY;AACnD;AAKA,SAAS,0BAA0B;AACjC,MAAI,OAAO,aAAa,aAAa;AACnC;AAAA,EACF;AACA,MAAI,CAAC,sBAAsB;AAEzB,UAAM,KAAK,SAAS,cAAc,KAAK;AACvC,OAAG,aAAa,kCAAkC,EAAE;AAEpD,aAAS,KAAK,YAAY,EAAE;AAC5B,2BAAuB;AAAA,EACzB;AACA,SAAO;AACT;AAOA,SAAS,QAAQ,OAAO;AACtB,MAAI,OAAO,UAAU,UAAU;AAC7B,WAAO;AAAA,EACT;AACA,QAAM,WAAO,sBAAO,KAAK;AACzB,SAAO,KAAK,QAAQ;AACtB;AAUA,SAAS,4BAA4B,iBAAiB;AACpD,MAAI,OAAO,oBAAoB,UAAU;AACvC,WAAO;AAAA,EACT;AACA,MAAI,QAAQ,eAAe,GAAG;AAC5B,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