Skip to content

Commit 7cd44a1

Browse files
committed
release v1.0.6 | bugfix: support color transparent
1 parent a66f5c7 commit 7cd44a1

File tree

5 files changed

+51
-27
lines changed

5 files changed

+51
-27
lines changed

dist/darkmode.js

Lines changed: 24 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/darkmode.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mp-darkmode",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "微信公众平台图文 Dark Mode 转换算法",
55
"main": "dist/darkmode.min.js",
66
"dependencies": {

src/modules/sdk.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import Color from 'color';
2121
import ColorName from 'color-name';
2222
ColorName.windowtext = [0, 0, 0]; // 补上这个colorName
23+
ColorName.transparent = [255, 255, 255, 0]; // 支持透明,暂定用白色透明度0来表示
2324

2425
import {
2526
CLASS_PREFIX,
@@ -53,11 +54,16 @@ import {
5354
hasTableClass
5455
} from './domUtils';
5556

56-
const colorNameReg = new RegExp(Object.keys(ColorName).map(colorName => `(^|[\\s,()]+)${colorName}([\\s,()]+|$)`).join('|'), 'ig'); // 生成正则表达式来匹配这些colorName
57-
const colorReg = /rgba?\([^)]+\)/i;
58-
const colorRegGlobal = /rgba?\([^)]+\)/ig;
57+
const colorNameReg = new RegExp(Object.keys(ColorName).map(colorName => `\\b${colorName}\\b`).join('|'), 'ig'); // 生成正则表达式来匹配这些colorName
58+
const colorReg = /\brgba?\([^)]+\)/i;
59+
const colorRegGlobal = /\brgba?\([^)]+\)/ig;
5960
const removeImportant = value => value.replace(IMPORTANT_REGEXP, ''); // 清除!important
60-
const parseColor = value => removeImportant(value).replace(colorNameReg, match => `rgb(${ColorName[match.replace(/(^[\s,()]+)|([\s,()]+$)/g, '').toLowerCase()].toString()})`); // 处理颜色,包括清除!important和转换英文定义颜色
61+
const parseColor = (value, parseTransparent) => removeImportant(value).replace(colorNameReg, match => { // 处理颜色,包括清除!important和转换英文定义颜色
62+
if (!parseTransparent && match === 'transparent') return match; // 如果不转换transparent,直接返回transparent
63+
64+
const color = ColorName[match.toLowerCase()];
65+
return `${color.length > 3 ? 'rgba' : 'rgb'}(${color.toString()})`;
66+
});
6167
const BG_COLOR_DELIMITER = '|';
6268

6369
// 计算mix颜色
@@ -383,9 +389,6 @@ export default class SDK {
383389
const oldValue = value;
384390
let cssChange = false;
385391

386-
// 将英文定义颜色转换为rgb格式
387-
value = parseColor(value);
388-
389392
// 找出色值来处理
390393
const isBgColor = /^background/.test(key);
391394
const isTextShadow = key === 'text-shadow';
@@ -396,14 +399,20 @@ export default class SDK {
396399
let extStyle = '';
397400
let gradientMixColor;
398401

402+
// 将英文定义颜色转换为rgb格式
403+
value = parseColor(value, isGradient); // 渐变需要处理透明
404+
399405
if (colorReg.test(value)) {
400406
if (isGradient) {
401407
// 把原渐变色取出来
402-
value.replace(colorRegGlobal, match => gradientColors.push(match));
408+
let matches = colorRegGlobal.exec(value);
409+
while (matches) {
410+
gradientColors.push(matches[0]);
411+
matches = colorRegGlobal.exec(value);
412+
}
403413

404414
// 计算出一个mix颜色
405-
gradientMixColor = mixColor([].concat(gradientColors));
406-
// console.log(value, gradientColors, 'mix:', gradientMixColor) ;
415+
gradientMixColor = mixColor(gradientColors);
407416
}
408417
let replaceIndex = 0;
409418
value = value.replace(colorRegGlobal, match => {

test/demo.html

Lines changed: 6 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)