1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
/** ***** BEGIN LICENSE BLOCK *****
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var ToolbarIconColor = {
init: function () {
this._initialized = true;
window.addEventListener("activate", this);
window.addEventListener("deactivate", this);
Services.obs.addObserver(this, "lightweight-theme-styling-update", false);
// If the window isn't active now, we assume that it has never been active
// before and will soon become active such that inferFromText will be
// called from the initial activate event.
if (Services.focus.activeWindow == window)
this.inferFromText();
},
uninit: function () {
this._initialized = false;
window.removeEventListener("activate", this);
window.removeEventListener("deactivate", this);
Services.obs.removeObserver(this, "lightweight-theme-styling-update");
},
handleEvent: function (event) {
switch (event.type) {
case "activate":
case "deactivate":
this.inferFromText();
break;
}
},
observe: function (aSubject, aTopic, aData) {
switch (aTopic) {
case "lightweight-theme-styling-update":
// inferFromText needs to run after LightweightThemeConsumer.jsm's
// lightweight-theme-styling-update observer.
setTimeout(() => { this.inferFromText(); }, 0);
break;
}
},
inferFromText: function () {
if (!this._initialized)
return;
function parseRGB(aColorString) {
let rgb = aColorString.match(/^rgba?\((\d+), (\d+), (\d+)/);
rgb.shift();
return rgb.map(x => parseInt(x));
}
let toolbarSelector = "toolbox > toolbar:not([collapsed=true])";
#ifdef XP_MACOSX
toolbarSelector += ":not([type=menubar])";
#endif
for (let toolbar of document.querySelectorAll(toolbarSelector)) {
let [r, g, b] = parseRGB(getComputedStyle(toolbar).color);
let luminance = 0.2125 * r + 0.7154 * g + 0.0721 * b;
if (luminance <= 110)
toolbar.removeAttribute("brighttext");
else
toolbar.setAttribute("brighttext", "true");
}
}
}
|