mp-darkmode 1.1.2 → 1.1.4-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/darkmode.js CHANGED
@@ -2312,7 +2312,7 @@ swizzle.wrap = function (fn) {
2312
2312
  /*!*************************!*\
2313
2313
  !*** ./src/darkmode.js ***!
2314
2314
  \*************************/
2315
- /*! exports provided: run, init, convertBg, extend, updateStyle */
2315
+ /*! exports provided: run, init, convertBg, extend, updateStyle, getContrast */
2316
2316
  /***/ (function(module, __webpack_exports__, __webpack_require__) {
2317
2317
 
2318
2318
  "use strict";
@@ -2322,6 +2322,7 @@ __webpack_require__.r(__webpack_exports__);
2322
2322
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "convertBg", function() { return convertBg; });
2323
2323
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "extend", function() { return extend; });
2324
2324
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "updateStyle", function() { return updateStyle; });
2325
+ /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "getContrast", function() { return getContrast; });
2325
2326
  /* harmony import */ var _modules_constant__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./modules/constant */ "./src/modules/constant.js");
2326
2327
  /* harmony import */ var _modules_config__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./modules/config */ "./src/modules/config.js");
2327
2328
  /* harmony import */ var _modules_global__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./modules/global */ "./src/modules/global.js");
@@ -2481,7 +2482,7 @@ function init() {
2481
2482
 
2482
2483
  if (['dark', 'light'].indexOf(opt.mode) > -1) {
2483
2484
  _modules_config__WEBPACK_IMPORTED_MODULE_1__["default"].set('string', opt, 'mode');
2484
- document.getElementsByTagName('html')[0].classList.add(_modules_constant__WEBPACK_IMPORTED_MODULE_0__["HTML_CLASS"]);
2485
+ opt.mode === 'dark' && document.getElementsByTagName('html')[0].classList.add(_modules_constant__WEBPACK_IMPORTED_MODULE_0__["HTML_CLASS"]);
2485
2486
  }
2486
2487
 
2487
2488
  _modules_config__WEBPACK_IMPORTED_MODULE_1__["default"].set('function', opt, 'begin');
@@ -2533,6 +2534,9 @@ function updateStyle(node, styles) {
2533
2534
  _modules_global__WEBPACK_IMPORTED_MODULE_2__["cssUtils"].writeStyle();
2534
2535
  }
2535
2536
  ;
2537
+ function getContrast(color1, color2) {
2538
+ return _modules_global__WEBPACK_IMPORTED_MODULE_2__["sdk"].getContrast(color1, color2);
2539
+ }
2536
2540
 
2537
2541
  /***/ }),
2538
2542
 
@@ -2743,7 +2747,7 @@ var config = {
2743
2747
  /*!*********************************!*\
2744
2748
  !*** ./src/modules/constant.js ***!
2745
2749
  \*********************************/
2746
- /*! exports provided: MEDIA_QUERY, CLASS_PREFIX, HTML_CLASS, COLORATTR, BGCOLORATTR, ORIGINAL_COLORATTR, ORIGINAL_BGCOLORATTR, BGIMAGEATTR, DEFAULT_LIGHT_TEXTCOLOR, DEFAULT_LIGHT_BGCOLOR, DEFAULT_DARK_TEXTCOLOR, DEFAULT_DARK_BGCOLOR, GRAY_MASK_COLOR, WHITE_LIKE_COLOR_BRIGHTNESS, MAX_LIMIT_BGCOLOR_BRIGHTNESS, MIN_LIMIT_OFFSET_BRIGHTNESS, HIGH_BGCOLOR_BRIGHTNESS, HIGH_BLACKWHITE_HSL_BRIGHTNESS, LOW_BLACKWHITE_HSL_BRIGHTNESS, IGNORE_ALPHA, PAGE_HEIGHT, TABLE_NAME, IMPORTANT_REGEXP */
2750
+ /*! exports provided: MEDIA_QUERY, CLASS_PREFIX, HTML_CLASS, COLORATTR, BGCOLORATTR, ORIGINAL_COLORATTR, ORIGINAL_BGCOLORATTR, BGIMAGEATTR, DEFAULT_LIGHT_TEXTCOLOR, DEFAULT_LIGHT_BGCOLOR, DEFAULT_DARK_TEXTCOLOR, DEFAULT_DARK_BGCOLOR, WHITE_LIKE_COLOR_BRIGHTNESS, MAX_LIMIT_BGCOLOR_BRIGHTNESS, MIN_LIMIT_OFFSET_BRIGHTNESS, HIGH_BGCOLOR_BRIGHTNESS, HIGH_BLACKWHITE_HSL_BRIGHTNESS, LOW_BLACKWHITE_HSL_BRIGHTNESS, IGNORE_ALPHA, PAGE_HEIGHT, TABLE_NAME, IMPORTANT_REGEXP */
2747
2751
  /***/ (function(module, __webpack_exports__, __webpack_require__) {
2748
2752
 
2749
2753
  "use strict";
@@ -2760,7 +2764,6 @@ __webpack_require__.r(__webpack_exports__);
2760
2764
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "DEFAULT_LIGHT_BGCOLOR", function() { return DEFAULT_LIGHT_BGCOLOR; });
2761
2765
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "DEFAULT_DARK_TEXTCOLOR", function() { return DEFAULT_DARK_TEXTCOLOR; });
2762
2766
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "DEFAULT_DARK_BGCOLOR", function() { return DEFAULT_DARK_BGCOLOR; });
2763
- /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "GRAY_MASK_COLOR", function() { return GRAY_MASK_COLOR; });
2764
2767
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "WHITE_LIKE_COLOR_BRIGHTNESS", function() { return WHITE_LIKE_COLOR_BRIGHTNESS; });
2765
2768
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "MAX_LIMIT_BGCOLOR_BRIGHTNESS", function() { return MAX_LIMIT_BGCOLOR_BRIGHTNESS; });
2766
2769
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "MIN_LIMIT_OFFSET_BRIGHTNESS", function() { return MIN_LIMIT_OFFSET_BRIGHTNESS; });
@@ -2796,8 +2799,6 @@ var DEFAULT_DARK_TEXTCOLOR = '#a3a3a3'; // 前景色:rgba(255,255,255,0.6) 背
2796
2799
 
2797
2800
  var DEFAULT_DARK_BGCOLOR = '#191919'; // Dark Mode下背景颜色
2798
2801
 
2799
- var GRAY_MASK_COLOR = 'rgba(0,0,0,0.2)'; // 灰色蒙层色值
2800
-
2801
2802
  var WHITE_LIKE_COLOR_BRIGHTNESS = 250; // 接近白色的感知亮度阈值
2802
2803
 
2803
2804
  var MAX_LIMIT_BGCOLOR_BRIGHTNESS = 190;
@@ -3530,6 +3531,24 @@ var adjustBrightnessByLimit = function adjustBrightnessByLimit(limitBright, rgb)
3530
3531
  }
3531
3532
 
3532
3533
  return color__WEBPACK_IMPORTED_MODULE_0___default.a.rgb(newTextR, newTextG, newTextB);
3534
+ }; // 计算亮度,用作对比度计算
3535
+
3536
+
3537
+ var getLuminanace = function getLuminanace(rgb) {
3538
+ var srgb = rgb.map(function (val) {
3539
+ val /= 255;
3540
+ return val <= 0.03928 ? val / 12.92 : Math.pow((val + 0.055) / 1.055, 2.4);
3541
+ });
3542
+ return srgb[0] * 0.2126 + srgb[1] * 0.7152 + srgb[2] * 0.0722;
3543
+ }; // 计算对比度 https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-procedure
3544
+
3545
+
3546
+ var _getContrast = function getContrast(rgb1, rgb2) {
3547
+ var lum1 = getLuminanace(rgb1);
3548
+ var lum2 = getLuminanace(rgb2); // 亮色 / 暗色
3549
+
3550
+ if (lum1 < lum2) return (lum2 + 0.05) / (lum1 + 0.05);
3551
+ return (lum1 + 0.05) / (lum2 + 0.05);
3533
3552
  };
3534
3553
 
3535
3554
  var SDK = /*#__PURE__*/function () {
@@ -4004,7 +4023,6 @@ var SDK = /*#__PURE__*/function () {
4004
4023
 
4005
4024
 
4006
4025
  if (isBackgroundAttr) {
4007
- newValue = "linear-gradient(".concat(_constant__WEBPACK_IMPORTED_MODULE_2__["GRAY_MASK_COLOR"], ", ").concat(_constant__WEBPACK_IMPORTED_MODULE_2__["GRAY_MASK_COLOR"], "),").concat(matches);
4008
4026
  tmpCssKvStr = _global__WEBPACK_IMPORTED_MODULE_4__["cssUtils"].genCssKV(key, "".concat(newValue, ",linear-gradient(").concat(imgBgColor, ", ").concat(imgBgColor, ")"));
4009
4027
 
4010
4028
  if (elBackgroundPositionAttr) {
@@ -4029,7 +4047,7 @@ var SDK = /*#__PURE__*/function () {
4029
4047
  } else {
4030
4048
  // border-image元素,如果当前元素没有背景颜色,补背景颜色
4031
4049
  if (!hasInlineBackground) {
4032
- tmpCssKvStr = _global__WEBPACK_IMPORTED_MODULE_4__["cssUtils"].genCssKV('background-image', "linear-gradient(".concat(_constant__WEBPACK_IMPORTED_MODULE_2__["GRAY_MASK_COLOR"], ", ").concat(_constant__WEBPACK_IMPORTED_MODULE_2__["GRAY_MASK_COLOR"], "),linear-gradient(").concat(imgBgColor, ", ").concat(imgBgColor, ")"));
4050
+ tmpCssKvStr = _global__WEBPACK_IMPORTED_MODULE_4__["cssUtils"].genCssKV('background-image', "linear-gradient(".concat(imgBgColor, ", ").concat(imgBgColor, ")"));
4033
4051
 
4034
4052
  if (dmBgClassName) {
4035
4053
  // 如果是文字底图,则直接加样式
@@ -4103,6 +4121,11 @@ var SDK = /*#__PURE__*/function () {
4103
4121
  _global__WEBPACK_IMPORTED_MODULE_4__["plugins"].emit("afterConvertNode".concat(isUpdate ? 'ByUpdateStyle' : ''), el);
4104
4122
  return css;
4105
4123
  }
4124
+ }, {
4125
+ key: "getContrast",
4126
+ value: function getContrast(color1, color2) {
4127
+ return _getContrast(color__WEBPACK_IMPORTED_MODULE_0___default()(color1).rgb().array(), color__WEBPACK_IMPORTED_MODULE_0___default()(color2).rgb().array());
4128
+ }
4106
4129
  }]);
4107
4130
 
4108
4131
  return SDK;