@@ -17,14 +17,14 @@ ColorName.transparent = [255, 255, 255, 0]; // 支持透明,暂定用白色透
17
17
18
18
import {
19
19
CLASS_PREFIX ,
20
+ DM_CLASSNAME_REGEXP ,
20
21
21
22
COLORATTR ,
22
23
BGCOLORATTR ,
23
24
ORIGINAL_COLORATTR ,
24
25
ORIGINAL_BGCOLORATTR ,
25
26
BGIMAGEATTR ,
26
-
27
- GRAY_MASK_COLOR ,
27
+ BG_COLOR_DELIMITER ,
28
28
29
29
WHITE_LIKE_COLOR_BRIGHTNESS ,
30
30
MIN_LIMIT_OFFSET_BRIGHTNESS ,
@@ -36,7 +36,10 @@ import {
36
36
37
37
TABLE_NAME ,
38
38
39
- IMPORTANT_REGEXP
39
+ IMPORTANT_REGEXP ,
40
+
41
+ SEMICOLON_PLACEHOLDER ,
42
+ SEMICOLON_PLACEHOLDER_REGEXP
40
43
} from './constant' ;
41
44
42
45
// Darkmode配置
@@ -56,11 +59,6 @@ import {
56
59
hasTableClass
57
60
} from './domUtils' ;
58
61
59
- const SEMICOLON_PLACEHOLDER = '<$#_SEMICOLON_#$>' ; // 分号占位符
60
- const SEMICOLON_PLACEHOLDER_REGEXP = / < \$ # _ S E M I C O L O N _ # \$ > / g;
61
-
62
- const DM_CLASSNAME_REGEXP = new RegExp ( `${ CLASS_PREFIX } \\d+` ) ;
63
-
64
62
const colorNameReg = new RegExp ( Object . keys ( ColorName ) . map ( colorName => `\\b${ colorName } \\b` ) . join ( '|' ) , 'ig' ) ; // 生成正则表达式来匹配这些colorName
65
63
const colorReg = / \b r g b a ? \( [ ^ ) ] + \) / i;
66
64
const colorRegGlobal = / \b r g b a ? \( [ ^ ) ] + \) / ig;
@@ -71,7 +69,6 @@ const parseColor = (value, parseTransparent) => removeImportant(value).replace(c
71
69
const color = ColorName [ match . toLowerCase ( ) ] ;
72
70
return `${ color . length > 3 ? 'rgba' : 'rgb' } (${ color . toString ( ) } )` ;
73
71
} ) ;
74
- const BG_COLOR_DELIMITER = '|' ;
75
72
76
73
// 计算mix颜色
77
74
const mixColor = colors => {
@@ -114,6 +111,25 @@ const adjustBrightnessByLimit = (limitBright, rgb) => {
114
111
return Color . rgb ( newTextR , newTextG , newTextB ) ;
115
112
} ;
116
113
114
+ // 计算亮度,用作对比度计算
115
+ const getLuminanace = rgb => {
116
+ const srgb = rgb . map ( val => {
117
+ val /= 255 ;
118
+ return val <= 0.03928 ? val / 12.92 : Math . pow ( ( val + 0.055 ) / 1.055 , 2.4 ) ;
119
+ } ) ;
120
+ return srgb [ 0 ] * 0.2126 + srgb [ 1 ] * 0.7152 + srgb [ 2 ] * 0.0722 ;
121
+ } ;
122
+
123
+ // 计算对比度 https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-procedure
124
+ const getContrast = ( rgb1 , rgb2 ) => {
125
+ const lum1 = getLuminanace ( rgb1 ) ;
126
+ const lum2 = getLuminanace ( rgb2 ) ;
127
+
128
+ // 亮色 / 暗色
129
+ if ( lum1 < lum2 ) return ( lum2 + 0.05 ) / ( lum1 + 0.05 ) ;
130
+ return ( lum1 + 0.05 ) / ( lum2 + 0.05 ) ;
131
+ }
132
+
117
133
export default class SDK {
118
134
_idx = 0 ; // 索引值
119
135
_defaultDarkTextColorRgb = Color ( config . defaultDarkTextColor ) . rgb ( ) . array ( ) ;
@@ -228,11 +244,10 @@ export default class SDK {
228
244
newColor = this . _adjustBackgroundBrightness ( color ) ;
229
245
230
246
if ( ! options . hasInlineColor ) {
231
- const parentTextColor = el [ COLORATTR ] || config . defaultLightTextColor ;
232
- const parentBgColorStr = newColor || color ;
247
+ const parentTextColor = el [ ORIGINAL_COLORATTR ] || config . defaultLightTextColor ;
233
248
const ret = this . _adjustBrightness ( Color ( parentTextColor ) , el , {
234
249
isTextColor : true ,
235
- parentElementBgColorStr : parentBgColorStr
250
+ parentElementBgColorStr : newColor || color
236
251
} , isUpdate ) ;
237
252
if ( ret . newColor ) {
238
253
extStyle += cssUtils . genCssKV ( 'color' , ret . newColor ) ;
@@ -366,7 +381,7 @@ export default class SDK {
366
381
} ) ;
367
382
}
368
383
369
- if ( nodeName === 'FONT' && ! hasInlineColor ) { // 如果是font标签且没有内联样式
384
+ if ( nodeName === 'FONT' && ! hasInlineColor ) { // 如果是font标签且没有内联文本颜色样式
370
385
this . _try ( ( ) => {
371
386
const color = el . getAttribute ( 'color' ) ; // 获取color的色值
372
387
if ( color ) { // 有色值,则当做内联样式来处理
@@ -485,13 +500,15 @@ export default class SDK {
485
500
486
501
// 对背景颜色和文字颜色做继承传递,用于文字亮度计算
487
502
if ( isBgColor || textColorIdx > 0 ) { // 不处理-webkit-text-stroke-color
488
- const attrName = isBgColor ? BGCOLORATTR : COLORATTR ;
489
- const originalAttrName = isBgColor ? ORIGINAL_BGCOLORATTR : ORIGINAL_COLORATTR ;
490
503
const retColorStr = retColor ? retColor . toString ( ) : match ;
491
504
replaceIndex === 0 && getChildrenAndIt ( el ) . forEach ( dom => {
492
- const originalAttrValue = dom [ originalAttrName ] || config . defaultLightBgColor ;
493
- dom [ attrName ] = retColorStr ;
494
- dom [ originalAttrName ] = originalAttrValue . split ( BG_COLOR_DELIMITER ) . concat ( match ) . join ( BG_COLOR_DELIMITER ) ;
505
+ if ( isBgColor ) {
506
+ dom [ BGCOLORATTR ] = retColorStr ;
507
+ dom [ ORIGINAL_BGCOLORATTR ] = ( dom [ ORIGINAL_BGCOLORATTR ] || config . defaultLightBgColor ) . split ( BG_COLOR_DELIMITER ) . concat ( match ) . join ( BG_COLOR_DELIMITER ) ;
508
+ } else {
509
+ dom [ COLORATTR ] = retColorStr ;
510
+ dom [ ORIGINAL_COLORATTR ] = match ;
511
+ }
495
512
496
513
// 如果设置背景颜色,取消背景图片的影响
497
514
if ( isBgColor && Color ( retColorStr ) . alpha ( ) >= IGNORE_ALPHA && dom [ BGIMAGEATTR ] ) {
@@ -501,7 +518,7 @@ export default class SDK {
501
518
}
502
519
503
520
retColor && ( cssChange = true ) ;
504
- replaceIndex += 1 ;
521
+ replaceIndex ++ ;
505
522
return retColor || match ;
506
523
}
507
524
return match ;
@@ -535,7 +552,6 @@ export default class SDK {
535
552
536
553
// background-image
537
554
if ( isBackgroundAttr ) {
538
- newValue = `linear-gradient(${ GRAY_MASK_COLOR } , ${ GRAY_MASK_COLOR } ),${ matches } ` ;
539
555
tmpCssKvStr = cssUtils . genCssKV ( key , `${ newValue } ,linear-gradient(${ imgBgColor } , ${ imgBgColor } )` ) ;
540
556
if ( elBackgroundPositionAttr ) {
541
557
newBackgroundPositionValue = `top left,${ elBackgroundPositionAttr } ` ;
@@ -555,7 +571,7 @@ export default class SDK {
555
571
} else {
556
572
// border-image元素,如果当前元素没有背景颜色,补背景颜色
557
573
if ( ! hasInlineBackground ) {
558
- tmpCssKvStr = cssUtils . genCssKV ( 'background-image' , `linear-gradient(${ GRAY_MASK_COLOR } , ${ GRAY_MASK_COLOR } ),linear-gradient( ${ imgBgColor } , ${ imgBgColor } )` ) ;
574
+ tmpCssKvStr = cssUtils . genCssKV ( 'background-image' , `linear-gradient(${ imgBgColor } , ${ imgBgColor } )` ) ;
559
575
if ( dmBgClassName ) { // 如果是文字底图,则直接加样式
560
576
bgCss += cssUtils . genCss ( dmBgClassName , tmpCssKvStr ) ;
561
577
} else { // 否则背景图入栈
@@ -568,7 +584,7 @@ export default class SDK {
568
584
569
585
// 没有设置自定义字体颜色,则使用非 Dark Mode 下默认字体颜色
570
586
if ( ! hasInlineColor ) {
571
- const textColor = mixColor ( ( el [ ORIGINAL_COLORATTR ] || config . defaultLightTextColor ) . split ( BG_COLOR_DELIMITER ) ) ;
587
+ const textColor = el [ ORIGINAL_COLORATTR ] || config . defaultLightTextColor ;
572
588
cssKV += cssUtils . genCssKV ( 'color' , textColor ) ;
573
589
getChildrenAndIt ( el ) . forEach ( dom => {
574
590
dom [ COLORATTR ] = textColor ;
@@ -592,8 +608,6 @@ export default class SDK {
592
608
} ) ) ;
593
609
594
610
if ( cssKV ) { // 有处理过或者是背景图片就加class以及css
595
- // TODO: 备份应该写到插件里
596
- el . setAttribute ( 'data-style' , styles . cssText ) ; // 备份内联样式到data-style里,供编辑器做反处理
597
611
if ( ! dmClassName ) {
598
612
dmClassName = `${ CLASS_PREFIX } ${ this . _idx ++ } ` ;
599
613
el . classList . add ( dmClassName ) ;
@@ -618,4 +632,8 @@ export default class SDK {
618
632
619
633
return css ;
620
634
}
635
+
636
+ getContrast ( color1 , color2 ) {
637
+ return getContrast ( Color ( color1 ) . rgb ( ) . array ( ) , Color ( color2 ) . rgb ( ) . array ( ) ) ;
638
+ }
621
639
} ;
0 commit comments