gecko/browser/metro/modules/colorUtils.jsm
Jim Blandy 4d6a633bba Bug 914753: Make Emacs file variable header lines correct, or at least consistent. DONTBUILD r=ehsan
The -*- file variable lines -*- establish per-file settings that Emacs will
pick up. This patch makes the following changes to those lines (and touches
nothing else):

 - Never set the buffer's mode.

   Years ago, Emacs did not have a good JavaScript mode, so it made sense
   to use Java or C++ mode in .js files. However, Emacs has had js-mode for
   years now; it's perfectly serviceable, and is available and enabled by
   default in all major Emacs packagings.

   Selecting a mode in the -*- file variable line -*- is almost always the
   wrong thing to do anyway. It overrides Emacs's default choice, which is
   (now) reasonable; and even worse, it overrides settings the user might
   have made in their '.emacs' file for that file extension. It's only
   useful when there's something specific about that particular file that
   makes a particular mode appropriate.

 - Correctly propagate settings that establish the correct indentation
   level for this file: c-basic-offset and js2-basic-offset should be
   js-indent-level. Whatever value they're given should be preserved;
   different parts of our tree use different indentation styles.

 - We don't use tabs in Mozilla JS code. Always set indent-tabs-mode: nil.
   Remove tab-width: settings, at least in files that don't contain tab
   characters.

 - Remove js2-mode settings that belong in the user's .emacs file, like
   js2-skip-preprocessor-directives.
2014-06-24 22:12:07 -07:00

174 lines
5.3 KiB
JavaScript

// -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
/* 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/. */
'use strict';
Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/Promise.jsm");
const ColorAnalyzer = Components.classes["@mozilla.org/places/colorAnalyzer;1"]
.getService(Components.interfaces.mozIColorAnalyzer);
this.EXPORTED_SYMBOLS = ["ColorUtils"];
let ColorUtils = {
_initialized: false,
init: function() {
if (this._initialized)
return;
Services.obs.addObserver(this, "idle-daily", false);
Services.obs.addObserver(this, "quit-application", false);
this._initialized = true;
},
uninit: function() {
Services.obs.removeObserver(this, "idle-daily");
Services.obs.removeObserver(this, "quit-application");
},
// default to keeping icon colorInfo for max 1 day
iconColorCacheMaxAge: 86400000,
// in-memory store for favicon color data
_uriColorsMap: (function() {
let cache = new Map();
// remove stale entries
cache.purge = function(maxAgeMs = 0) {
let cuttoff = Date.now() - (maxAgeMs || ColorUtils.iconColorCacheMaxAge);
for (let [key, value] of this) {
if (value.timestamp && value.timestamp >= cuttoff) {
continue;
}
this.delete(key);
}
}
return cache;
})(),
get iconColorCache() {
return ColorUtils._uriColorsMap;
},
observe: function (aSubject, aTopic, aData) {
switch (aTopic) {
case "idle-daily":
this.iconColorCache.purge();
break;
case "quit-application":
this.uninit();
break;
}
},
/** Takes an icon and returns either an object with foreground and background color properties
* or a promise for the same.
* The foreground is the contrast color, the background is the primary/dominant one
*/
getForegroundAndBackgroundIconColors: function getForegroundAndBackgroundIconColors(aIconURI) {
let colorKey = aIconURI.spec;
let colorInfo = this._uriColorsMap.get(colorKey);
if (colorInfo) {
return colorInfo;
}
let deferred = Promise.defer();
let wrappedIcon = aIconURI;
this._uriColorsMap.set(colorKey, deferred.promise);
ColorAnalyzer.findRepresentativeColor(wrappedIcon, (success, color) => {
if (!success) {
this._uriColorsMap.delete(colorKey);
deferred.reject();
} else {
colorInfo = {
foreground: this.bestTextColorForContrast(color),
background: this.convertDecimalToRgbColor(color),
timestamp: Date.now()
};
deferred.resolve(colorInfo);
}
});
return deferred.promise;
},
/** returns the best color for text readability on top of aColor
* return color is in rgb(r,g,b) format, suitable to csss
* The color bightness algorithm is currently: http://www.w3.org/TR/AERT#color-contrast
*/
bestTextColorForContrast: function bestTextColorForContrast(aColor) {
let r = (aColor & 0xff0000) >> 16;
let g = (aColor & 0x00ff00) >> 8;
let b = (aColor & 0x0000ff);
let w3cContrastValue = ((r*299)+(g*587)+(b*114))/1000;
w3cContrastValue = Math.round(w3cContrastValue);
let textColor = "rgb(255,255,255)";
if (w3cContrastValue > 125) {
// bright/light, use black text
textColor = "rgb(0,0,0)";
}
return textColor;
},
toCSSRgbColor: function toCSSRgbColor(r, g, b, a) {
var values = [Math.round(r), Math.round(g), Math.round(b)];
if (undefined !== a && a < 1) {
values.push(a);
return 'rgba('+values.join(',')+')';
}
return 'rgb('+values.join(',')+')';
},
/**
* converts a decimal(base10) number into CSS rgb color value string
*/
convertDecimalToRgbColor: function convertDecimalToRgbColor(aColor) {
let [r,g,b,a] = this.unpackDecimalColorWord(aColor);
return this.toCSSRgbColor(r,g,b,a);
},
/**
* unpack a decimal(base10) word for r,g,b,a values
*/
unpackDecimalColorWord: function unpackDecimalColorWord(aColor) {
let a = (aColor & 0xff000000) >> 24;
let r = (aColor & 0x00ff0000) >> 16;
let g = (aColor & 0x0000ff00) >> 8;
let b = (aColor & 0x000000ff);
// NB: falsy alpha treated as undefined, fully opaque
return a ? [r,g,b,a/255] : [r,g,b];
},
/**
* create a decimal(base10) word for r,g,b values
*/
createDecimalColorWord: function createDecimalColorWord(r, g, b, a) {
let rgb = 0;
rgb |= b;
rgb |= (g << 8);
rgb |= (r << 16);
// pack alpha value if one is given
if (undefined !== a && a < 1)
rgb |= (Math.round(a*255) << 24);
return rgb;
},
/**
* Add 2 rgb(a) colors to get a flat color
*/
addRgbColors: function addRgbColors(color1, color2) {
let [r1, g1, b1] = this.unpackDecimalColorWord(color1);
let [r2, g2, b2, alpha] = this.unpackDecimalColorWord(color2);
let color = {};
// early return if 2nd color is opaque
if (!alpha || alpha >= 1)
return color2;
return this.createDecimalColorWord(
Math.min(255, alpha * r2 + (1 - alpha) * r1),
Math.min(255, alpha * g2 + (1 - alpha) * g1),
Math.min(255, alpha * b2 + (1 - alpha) * b1)
);
}
};