Kawiesh

[BM] Night Mode

Jun 9th, 2020 (edited)
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. javascript: (function() {
  2.  function RGBtoHSL(RGBColor) {
  3.   with(Math) {
  4.    var R, G, B;
  5.    var cMax, cMin;
  6.    var sum, diff;
  7.    var Rdelta, Gdelta, Bdelta;
  8.    var H, L, S;
  9.    R = RGBColor[0];
  10.    G = RGBColor[1];
  11.    B = RGBColor[2];
  12.    cMax = max(max(R, G), B);
  13.    cMin = min(min(R, G), B);
  14.    sum = cMax + cMin;
  15.    diff = cMax - cMin;
  16.    L = sum / 2;
  17.    if (cMax == cMin) {
  18.     S = 0;
  19.     H = 0;
  20.    } else {
  21.     if (L <= (1 / 2)) S = diff / sum;
  22.     else S = diff / (2 - sum);
  23.     Rdelta = R / 6 / diff;
  24.     Gdelta = G / 6 / diff;
  25.     Bdelta = B / 6 / diff;
  26.     if (R == cMax) H = Gdelta - Bdelta;
  27.     else if (G == cMax) H = (1 / 3) + Bdelta - Rdelta;
  28.     else H = (2 / 3) + Rdelta - Gdelta;
  29.     if (H < 0) H += 1;
  30.     if (H > 1) H -= 1;
  31.    }
  32.    return [H, S, L];
  33.   }
  34.  }
  35.  
  36.  function getRGBColor(node, prop) {
  37.   var rgb = getComputedStyle(node, null).getPropertyValue(prop);
  38.   var r, g, b;
  39.   if (/rgb\((\d+),\s(\d+),\s(\d+)\)/.exec(rgb)) {
  40.    r = parseInt(RegExp.$1, 10);
  41.    g = parseInt(RegExp.$2, 10);
  42.    b = parseInt(RegExp.$3, 10);
  43.    return [r / 255, g / 255, b / 255];
  44.   }
  45.   return rgb;
  46.  }
  47.  
  48.  function hslToCSS(hsl) {
  49.   return "hsl(" + Math.round(hsl[0] * 360) + ", " + Math.round(hsl[1] * 100) + "%, " + Math.round(hsl[2] * 100) + "%)";
  50.  }
  51.  var props = ["color", "background-color", "border-left-color", "border-right-color", "border-top-color", "border-bottom-color"];
  52.  var props2 = ["color", "backgroundColor", "borderLeftColor", "borderRightColor", "borderTopColor", "borderBottomColor"];
  53.  if (typeof getRGBColor(document.documentElement, "background-color") == "string") document.documentElement.style.backgroundColor = "white";
  54.  revl(document.documentElement);
  55.  
  56.  function revl(n) {
  57.   var i, x, color, hsl;
  58.   if (n.nodeType == Node.ELEMENT_NODE) {
  59.    for (i = 0; x = n.childNodes[i]; ++i) revl(x);
  60.    for (i = 0; x = props[i]; ++i) {
  61.     color = getRGBColor(n, x);
  62.     if (typeof(color) != "string") {
  63.      hsl = RGBtoHSL(color);
  64.      hsl[2] = 1 - hsl[2];
  65.      n.style[props2[i]] = hslToCSS(hsl);
  66.     }
  67.    }
  68.   }
  69.  }
  70. })()
Advertisement
Add Comment
Please, Sign In to add comment