From f26f29d84e0a68831a6af14dab3eec5500496d2e Mon Sep 17 00:00:00 2001 From: spring <2396852758@qq.com> Date: 星期三, 28 五月 2025 16:48:52 +0800 Subject: [PATCH] 初始化项目 --- uni_modules/wu-ui-tools/libs/function/color/index.js | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 158 insertions(+), 0 deletions(-) diff --git a/uni_modules/wu-ui-tools/libs/function/color/index.js b/uni_modules/wu-ui-tools/libs/function/color/index.js new file mode 100644 index 0000000..fe6a1e4 --- /dev/null +++ b/uni_modules/wu-ui-tools/libs/function/color/index.js @@ -0,0 +1,158 @@ +import Color from './color'; + +/** + * 杞崲棰滆壊鏍煎紡銆� + * @param {Object} params - 鍙傛暟瀵硅薄銆� + * @param {string} color - 杈撳叆鐨勯鑹诧紝榛樿涓� '#fff'銆� + * @param {string} format - 闇�瑕佽浆鎹㈢殑鏍煎紡锛堟敮鎸� 'rgb', 'hex', 'hsl', 'hsv', 'hwb'锛夈�� + * @param {string} type - 杞崲鍚庣殑绫诲瀷锛堟敮鎸� 'string', 'object', 'array', 'round'锛夈�� + * @returns {string|Object|Array} 杞崲鍚庣殑棰滆壊琛ㄧず銆� + */ +function convertFormat(color = '#fff', format = 'rgb', type = 'string') { + let colorObj = Color(color); + // 濡傛灉鏍煎紡瀛樺湪 + if (colorObj[format]) { + // hex 鏃犳硶鐩存帴杞崲涓� 闄tring绫诲瀷澶栫殑浠讳綍绫诲瀷 + // 鎵�浠ヨ浆涓簉gb 鍚� 鑾峰彇鍏朵粬绫诲瀷 + if(format == 'hex' && type != 'string') format = 'rgb'; + // 绫诲瀷鍚嶇О + let typeName = ''; + switch (type) { + case 'string': + typeName = 'toString'; + break; + case 'object': + typeName = 'object'; + break; + case 'array': + typeName = 'array'; + break; + case 'round': + typeName = 'round'; + break; + default: + throw Error('Unsupported target type:' + type) + } + return colorObj[format]()[typeName](); + } else { + throw Error('Unsupported target format: ' + format); + } +} + +/** + * 璁$畻涓や釜棰滆壊涔嬮棿鐨勬笎鍙樺�笺�� + * @param {string} startColor - 寮�濮嬬殑棰滆壊锛岄粯璁や负榛戣壊銆� + * @param {string} endColor - 缁撴潫鐨勯鑹诧紝榛樿涓虹櫧鑹层�� + * @param {number} step - 娓愬彉鐨勬鏁帮紝榛樿涓�10銆� + * @returns {Array<string>} 涓や釜棰滆壊涔嬮棿鐨勬笎鍙橀鑹叉暟缁勩�� + */ +function gradient(startColor = 'rgb(0, 0, 0)', endColor = 'rgb(255, 255, 255)', step = 10) { + const startRGB = convertFormat(startColor, 'rgb', 'array') // 杞崲涓簉gb鏁扮粍妯″紡 + const startR = startRGB[0] + const startG = startRGB[1] + const startB = startRGB[2] + + const endRGB = convertFormat(endColor, 'rgb', 'array') + const endR = endRGB[0] + const endG = endRGB[1] + const endB = endRGB[2] + + const sR = (endR - startR) / step // 鎬诲樊鍊� + const sG = (endG - startG) / step + const sB = (endB - startB) / step + const colorArr = [] + for (let i = 0; i < step; i++) { + // 璁$畻姣忎竴姝ョ殑hex鍊� + let hex = convertFormat(`rgb(${Math.round((sR * i + startR))},${Math.round((sG * i + startG))},${Math.round((sB + * i + startB))})`, 'hex') + // 纭繚绗竴涓鑹插�间负startColor鐨勫�� + if (i === 0) hex = convertFormat(startColor, 'hex') + // 纭繚鏈�鍚庝竴涓鑹插�间负endColor鐨勫�� + if (i === step - 1) hex = convertFormat(endColor, 'hex') + colorArr.push(hex) + } + return colorArr +} + + + +export default { + /** + * 鏍煎紡杞崲銆� + */ + convertFormat, + + /** + * 璁$畻涓や釜棰滆壊涔嬮棿鐨勬笎鍙樺�笺�� + */ + gradient, + + /** + * 澧炲姞棰滆壊鐨勪寒搴︺�� + * @param {string} color - 杈撳叆鐨勯鑹层�� + * @param {number} value - 澧炲姞鐨勪寒搴﹀�硷紙0-1锛夈�� + * @returns {string} 璋冩暣鍚庣殑棰滆壊銆� + */ + lighten: (color, value, format = 'rgb', type = 'string') => convertFormat(Color(color).lighten(value), format, type), + + /** + * 鍑忓皯棰滆壊鐨勪寒搴︺�� + * @param {string} color - 杈撳叆鐨勯鑹层�� + * @param {number} value - 鍑忓皯鐨勪寒搴﹀�硷紙0-1锛夈�� + * @returns {string} 璋冩暣鍚庣殑棰滆壊銆� + */ + darken: (color, value, format = 'rgb', type = 'string') => convertFormat(Color(color).darken(value), format, type), + + /** + * 澧炲姞棰滆壊鐨勯ケ鍜屽害銆� + * @param {string} color - 杈撳叆鐨勯鑹层�� + * @param {number} value - 澧炲姞鐨勯ケ鍜屽害鍊硷紙0-1锛夈�� + * @returns {string} 璋冩暣鍚庣殑棰滆壊銆� + */ + saturate: (color, value, format = 'rgb', type = 'string') => convertFormat(Color(color).saturate(value), format, type), + + /** + * 鍑忓皯棰滆壊鐨勯ケ鍜屽害銆� + * @param {string} color - 杈撳叆鐨勯鑹层�� + * @param {number} value - 鍑忓皯鐨勯ケ鍜屽害鍊硷紙0-1锛夈�� + * @returns {string} 璋冩暣鍚庣殑棰滆壊銆� + */ + desaturate: (color, value, format = 'rgb', type = 'string') => convertFormat(Color(color).desaturate(value), format, type), + + /** + * 鏃嬭浆棰滆壊鐨勮壊鐩搞�� + * @param {string} color - 杈撳叆鐨勯鑹层�� + * @param {number} degrees - 鏃嬭浆鐨勫害鏁般�� + * @returns {string} 璋冩暣鍚庣殑棰滆壊銆� + */ + rotate: (color, degrees, format = 'rgb', type = 'string') => convertFormat(Color(color).rotate(degrees), format, type), + + /** + * 璋冩暣棰滆壊鐨勯�忔槑搴︺�� + * @param {string} color - 杈撳叆鐨勯鑹层�� + * @param {number} value - 閫忔槑搴﹀�硷紙0-1锛屽叾涓� 1 鏄笉閫忔槑锛夈�� + * @returns {string} 璋冩暣鍚庣殑棰滆壊銆� + */ + adjustAlpha: (color, value, format = 'rgb', type = 'string') => convertFormat(Color(color).alpha(value), format, type), + + /** + * 鑾峰彇棰滆壊鐨勪寒搴︺�� + * @param {string} color - 杈撳叆鐨勯鑹层�� + * @returns {number} 棰滆壊鐨勪寒搴﹀�硷紙0-1锛夈�� + */ + luminosity: (color, format) => Color(color).luminosity(), + + /** + * 鍒ゆ柇棰滆壊鏄惁涓烘殫鑹层�� + * @param {string} color - 杈撳叆鐨勯鑹层�� + * @returns {boolean} 濡傛灉鏄殫鑹插垯杩斿洖 true锛屽惁鍒欒繑鍥� false銆� + */ + isDark: (color, format) => Color(color).isDark(), + + /** + * 鍒ゆ柇棰滆壊鏄惁涓轰寒鑹层�� + * @param {string} color - 杈撳叆鐨勯鑹层�� + * @returns {boolean} 濡傛灉鏄寒鑹插垯杩斿洖 true锛屽惁鍒欒繑鍥� false銆� + */ + isLight: (color, format) => Color(color).isLight() +}; \ No newline at end of file -- Gitblit v1.9.3