-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (35 loc) · 1.15 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//颜色格式转换工具
/**
* @param {string} colorStr
* @returns {string} color
* ### example
* ```
* xxxx.colorConvert('#ffffff') // rgb(255,255,255)
*
* xxxx.colorConvert('rgba(255,255,255,1)') // #ffffffff
* ```
*/
function colorConvert (colorStr) {
if (!colorStr) { return colorStr; }
const rgx = /^(#)([0-9A-Fa-f]{6,8})|^(rgb|rgba)\(([0-9]{1,3})\,\s*([0-9]{1,3})\,\s*([0-9]{1,3})\,?\s*([01]?\.?\d*)?\)$/;
const result = colorStr.match(rgx);
if (!result) { return colorStr; }
const latest = [];
if (result[1] === '#') {
const strArr = result[2].split('');
for (let i = 0; i < strArr.length; i+= 2) {
latest.push(parseInt('0x' + strArr[i] + strArr[i + 1]));
}
return strArr.length > 6? `rgba(${latest.slice(0,3).join(',')},${latest[3]/255})`: `rgb(${latest.join(',')})`;
} else if(result[3] === 'rgb' || result[3] === 'rgba') {
for (let i = 4; i <= 6; i++) {
latest.push((+result[i]).toString(16));
}
return '#'+ (result[7] !== undefined ? (latest.join('') + Math.round(255 * +result[7]).toString(16)):latest.join(''));
}
}
if ('module' in window) {
module.exports = {
colorConvert
}
}