UNPKG

@wordpress/block-editor

Version:
8 lines (7 loc) 4.67 kB
{ "version": 3, "sources": ["../../../src/components/colors/utils.js"], "sourcesContent": ["/**\n * External dependencies\n */\nimport { colord, extend } from 'colord';\nimport namesPlugin from 'colord/plugins/names';\nimport a11yPlugin from 'colord/plugins/a11y';\n\n/**\n * WordPress dependencies\n */\nimport { privateApis as componentsPrivateApis } from '@wordpress/components';\n\n/**\n * Internal dependencies\n */\nimport { unlock } from '../../lock-unlock';\n\nextend( [ namesPlugin, a11yPlugin ] );\n\nconst { kebabCase } = unlock( componentsPrivateApis );\n\n/**\n * Provided an array of color objects as set by the theme or by the editor defaults,\n * and the values of the defined color or custom color returns a color object describing the color.\n *\n * @param {Array} colors Array of color objects as set by the theme or by the editor defaults.\n * @param {?string} definedColor A string containing the color slug.\n * @param {?string} customColor A string containing the customColor value.\n *\n * @return {?Object} If definedColor is passed and the name is found in colors,\n * the color object exactly as set by the theme or editor defaults is returned.\n * Otherwise, an object that just sets the color is defined.\n */\nexport const getColorObjectByAttributeValues = (\n\tcolors,\n\tdefinedColor,\n\tcustomColor\n) => {\n\tif ( definedColor ) {\n\t\tconst colorObj = colors?.find(\n\t\t\t( color ) => color.slug === definedColor\n\t\t);\n\n\t\tif ( colorObj ) {\n\t\t\treturn colorObj;\n\t\t}\n\t}\n\treturn {\n\t\tcolor: customColor,\n\t};\n};\n\n/**\n * Provided an array of color objects as set by the theme or by the editor defaults, and a color value returns the color object matching that value or undefined.\n *\n * @param {Array} colors Array of color objects as set by the theme or by the editor defaults.\n * @param {?string} colorValue A string containing the color value.\n *\n * @return {?Object} Color object included in the colors array whose color property equals colorValue.\n * Returns undefined if no color object matches this requirement.\n */\nexport const getColorObjectByColorValue = ( colors, colorValue ) => {\n\treturn colors?.find( ( color ) => color.color === colorValue );\n};\n\n/**\n * Returns a class based on the context a color is being used and its slug.\n *\n * @param {string} colorContextName Context/place where color is being used e.g: background, text etc...\n * @param {string} colorSlug Slug of the color.\n *\n * @return {?string} String with the class corresponding to the color in the provided context.\n * Returns undefined if either colorContextName or colorSlug are not provided.\n */\nexport function getColorClassName( colorContextName, colorSlug ) {\n\tif ( ! colorContextName || ! colorSlug ) {\n\t\treturn undefined;\n\t}\n\n\treturn `has-${ kebabCase( colorSlug ) }-${ colorContextName }`;\n}\n\n/**\n * Given an array of color objects and a color value returns the color value of the most readable color in the array.\n *\n * @param {Array} colors Array of color objects as set by the theme or by the editor defaults.\n * @param {?string} colorValue A string containing the color value.\n *\n * @return {string} String with the color value of the most readable color.\n */\nexport function getMostReadableColor( colors, colorValue ) {\n\tconst colordColor = colord( colorValue );\n\tconst getColorContrast = ( { color } ) => colordColor.contrast( color );\n\n\tconst maxContrast = Math.max( ...colors.map( getColorContrast ) );\n\treturn colors.find( ( color ) => getColorContrast( color ) === maxContrast )\n\t\t.color;\n}\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,oBAA+B;AAC/B,mBAAwB;AACxB,kBAAuB;AAKvB,wBAAqD;AAKrD,yBAAuB;AAAA,IAEvB,sBAAQ,CAAE,aAAAA,SAAa,YAAAC,OAAW,CAAE;AAEpC,IAAM,EAAE,UAAU,QAAI,2BAAQ,kBAAAC,WAAsB;AAc7C,IAAM,kCAAkC,CAC9C,QACA,cACA,gBACI;AACJ,MAAK,cAAe;AACnB,UAAM,WAAW,QAAQ;AAAA,MACxB,CAAE,UAAW,MAAM,SAAS;AAAA,IAC7B;AAEA,QAAK,UAAW;AACf,aAAO;AAAA,IACR;AAAA,EACD;AACA,SAAO;AAAA,IACN,OAAO;AAAA,EACR;AACD;AAWO,IAAM,6BAA6B,CAAE,QAAQ,eAAgB;AACnE,SAAO,QAAQ,KAAM,CAAE,UAAW,MAAM,UAAU,UAAW;AAC9D;AAWO,SAAS,kBAAmB,kBAAkB,WAAY;AAChE,MAAK,CAAE,oBAAoB,CAAE,WAAY;AACxC,WAAO;AAAA,EACR;AAEA,SAAO,OAAQ,UAAW,SAAU,CAAE,IAAK,gBAAiB;AAC7D;AAUO,SAAS,qBAAsB,QAAQ,YAAa;AAC1D,QAAM,kBAAc,sBAAQ,UAAW;AACvC,QAAM,mBAAmB,CAAE,EAAE,MAAM,MAAO,YAAY,SAAU,KAAM;AAEtE,QAAM,cAAc,KAAK,IAAK,GAAG,OAAO,IAAK,gBAAiB,CAAE;AAChE,SAAO,OAAO,KAAM,CAAE,UAAW,iBAAkB,KAAM,MAAM,WAAY,EACzE;AACH;", "names": ["namesPlugin", "a11yPlugin", "componentsPrivateApis"] }