Skip to content

Commit 8fb0884

Browse files
committed
feat: Support hex color
1 parent 3914d78 commit 8fb0884

File tree

4 files changed

+71
-15
lines changed

4 files changed

+71
-15
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ npx iconfont
9292

9393
// 多色:红色+橘色
9494
<iconfont name="alipay" color="{{['red', 'orange']}}" size="300" />
95+
96+
// 不同格式的颜色写法
97+
<iconfont name="alipay" color="{{['#333', 'rgb(50, 124, 39)']}}" />
9598
```
9699

97100
# 更新图标

src/libs/generateComponent.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ import {
1616
const ATTRIBUTE_FILL_MAP = ['path'];
1717

1818
export const generateComponent = (data: XmlData, config: Config) => {
19-
const svgTemplates: string[] = [];
19+
const svgTemplates: string[] = [
20+
'<wxs src="./iconfont.wxs" module="helper" />\n',
21+
];
2022
const names: string[] = [];
2123
const saveDir = path.resolve(config.save_dir);
2224
const fileName = basename(config.save_dir) || 'iconfont';
@@ -53,6 +55,7 @@ export const generateComponent = (data: XmlData, config: Config) => {
5355

5456
fs.writeFileSync(path.join(saveDir, fileName + '.js'), jsFile);
5557
fs.writeFileSync(path.join(saveDir, fileName + '.json'), getTemplate('icon.json'));
58+
fs.writeFileSync(path.join(saveDir, fileName + '.wxs'), getTemplate('icon.wxs'));
5659

5760
console.log(`\n${colors.green('√')} All icons have been putted into dir: ${colors.green(config.save_dir)}\n`);
5861
};
@@ -97,8 +100,7 @@ const addAttribute = (domName: string, sub: XmlData['svg']['symbol'][number]['pa
97100
if (attributeName === 'fill') {
98101
const color = replaceHexToRgb(sub.$[attributeName]);
99102

100-
template += ` ${attributeName}='{{colorIsString ? color || '${color}' : color[${counter.colorIndex}] || '${color}'}}'`;
101-
//template += ` ${attributeName}='{{typeof color == 'string' ? color : color[${counter.colorIndex}] || '${color}'}}'`;
103+
template += ` ${attributeName}='{{helper.getColor(colors, ${counter.colorIndex}, '${color}')}}'`;
102104
counter.colorIndex += 1;
103105
} else {
104106
template += ` ${attributeName}='${sub.$[attributeName]}'`;

src/templates/icon.js.template

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,50 @@ Component({
1111
// string || string[]
1212
color: {
1313
type: null,
14-
},
14+
observer: function (color, originalColor) {
15+
if (color !== originalColor) {
16+
this.setData({
17+
colors: this.fixColor()
18+
});
19+
}
20+
}
21+
}
1522
},
1623
data: {
17-
colorIsString: false,
18-
},
19-
attached: function() {
20-
this.setData({
21-
colorIsString: typeof this.properties.color === 'string',
22-
});
23-
},
24-
moved: function() {
25-
this.setData({
26-
colorIsString: typeof this.properties.color === 'string',
27-
});
24+
colors: ''
2825
},
26+
methods: {
27+
fixColor: function() {
28+
var color = this.data.color;
29+
var self = this;
30+
31+
if (typeof color === 'string') {
32+
return color.indexOf('#') === 0 ? this.hex2rgb(color) : color;
33+
}
34+
35+
return color.map(function (item) {
36+
return item.indexOf('#') === 0 ? self.hex2rgb(item) : item;
37+
});
38+
},
39+
hex2rgb: function(hex) {
40+
var rgb = [];
41+
42+
//去除前缀 # 号
43+
hex = hex.substr(1);
44+
45+
if (hex.length === 3) {
46+
// 处理 '#abc' 成 '#aabbcc'
47+
hex = hex.replace(/(.)/g, '$1$1');
48+
}
49+
50+
hex.replace(/../g, function(color) {
51+
// 按16进制将字符串转换为数字
52+
rgb.push(parseInt(color, 0x10));
53+
54+
return color;
55+
});
56+
57+
return 'rgb(' + rgb.join(',') + ')';
58+
}
59+
}
2960
})

src/templates/icon.wxs.template

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
*
3+
* @param {string | string[]} color
4+
* @param {number} arrayIndex
5+
* @param {string} defaultColor
6+
* @return {string}
7+
*/
8+
var getColor = function(color, arrayIndex, defaultColor) {
9+
if (!color) {
10+
return defaultColor;
11+
}
12+
13+
if (typeof color === 'string') {
14+
return color;
15+
}
16+
17+
return color[arrayIndex] || defaultColor;
18+
};
19+
20+
module.exports.getColor = getColor;

0 commit comments

Comments
 (0)