UNPKG

@wordpress/components

Version:
1 lines • 3.96 kB
{"version":3,"file":"colors.cjs","names":["extend","namesPlugin","memoize"],"sources":["../../src/utils/colors.ts"],"sourcesContent":["import memoize from 'memize';\nimport { colord, extend } from 'colord';\nimport namesPlugin from 'colord/plugins/names';\n\nlet colorComputationNode: HTMLDivElement;\n\nextend( [ namesPlugin ] );\n\n/**\n * @return The HTML element for color computation.\n */\nfunction getColorComputationNode() {\n\tif ( typeof document === 'undefined' ) {\n\t\treturn;\n\t}\n\n\tif ( ! colorComputationNode ) {\n\t\t// Create a temporary element for style computation.\n\t\tconst el = document.createElement( 'div' );\n\t\tel.setAttribute( 'data-g2-color-computation-node', '' );\n\t\t// Inject for window computed style.\n\t\tdocument.body.appendChild( el );\n\t\tcolorComputationNode = el;\n\t}\n\n\treturn colorComputationNode;\n}\n\n/**\n * @param value\n *\n * @return Whether the value is a valid color.\n */\nfunction isColor( value: string | unknown ) {\n\tif ( typeof value !== 'string' ) {\n\t\treturn false;\n\t}\n\tconst test = colord( value );\n\n\treturn 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 backgroundColor The background color to compute.\n *\n * @return The computed background color.\n */\nfunction _getComputedBackgroundColor( backgroundColor: string | unknown ) {\n\tif ( typeof backgroundColor !== 'string' ) {\n\t\treturn '';\n\t}\n\n\tif ( isColor( backgroundColor ) ) {\n\t\treturn backgroundColor;\n\t}\n\n\tif ( ! backgroundColor.includes( 'var(' ) ) {\n\t\treturn '';\n\t}\n\tif ( typeof document === 'undefined' ) {\n\t\treturn '';\n\t}\n\n\t// Attempts to gracefully handle CSS variables color values.\n\tconst el = getColorComputationNode();\n\tif ( ! el ) {\n\t\treturn '';\n\t}\n\n\tel.style.background = backgroundColor;\n\t// Grab the style.\n\tconst computedColor = window?.getComputedStyle( el ).background;\n\t// Reset.\n\tel.style.background = '';\n\n\treturn computedColor || '';\n}\n\nconst getComputedBackgroundColor = memoize( _getComputedBackgroundColor );\n\n/**\n * Get the text shade optimized for readability, based on a background color.\n *\n * @param backgroundColor The background color.\n *\n * @return The optimized text color (black or white).\n */\nexport function getOptimalTextColor(\n\tbackgroundColor: string | unknown\n): string {\n\tconst background = getComputedBackgroundColor( backgroundColor );\n\n\treturn colord( background ).isLight() ? '#000000' : '#ffffff';\n}\n\n/**\n * Get the text shade optimized for readability, based on a background color.\n *\n * @param backgroundColor The background color.\n *\n * @return The optimized text shade (dark or light).\n */\nexport function getOptimalTextShade(\n\tbackgroundColor: string | unknown\n): string {\n\tconst result = getOptimalTextColor( backgroundColor );\n\n\treturn result === '#000000' ? 'dark' : 'light';\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,IAAI;CAEJA,GAAAA,OAAAA,OAAAA,CAAQ,CAAEC,qBAAAA,OAAY,CAAE;;;;AAKxB,SAAS,0BAA0B;CAClC,IAAK,OAAO,aAAa,aACxB;CAGD,IAAK,CAAE,sBAAuB;EAE7B,MAAM,KAAK,SAAS,cAAe,KAAM;EACzC,GAAG,aAAc,kCAAkC,EAAG;EAEtD,SAAS,KAAK,YAAa,EAAG;EAC9B,uBAAuB;CACxB;CAEA,OAAO;AACR;;;;;;AAOA,SAAS,QAAS,OAA0B;CAC3C,IAAK,OAAO,UAAU,UACrB,OAAO;CAIR,QAAA,GAFa,OAAA,OAAA,CAAQ,KAEX,CAAC,CAAC,QAAQ;AACrB;;;;;;;;;AAUA,SAAS,4BAA6B,iBAAoC;CACzE,IAAK,OAAO,oBAAoB,UAC/B,OAAO;CAGR,IAAK,QAAS,eAAgB,GAC7B,OAAO;CAGR,IAAK,CAAE,gBAAgB,SAAU,MAAO,GACvC,OAAO;CAER,IAAK,OAAO,aAAa,aACxB,OAAO;CAIR,MAAM,KAAK,wBAAwB;CACnC,IAAK,CAAE,IACN,OAAO;CAGR,GAAG,MAAM,aAAa;CAEtB,MAAM,gBAAgB,QAAQ,iBAAkB,EAAG,CAAC,CAAC;CAErD,GAAG,MA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