Bug 988191 - change to WCAG algorithm for titlebar font, r=jaws, a=sledru.
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Tue, 08 Apr 2014 10:39:05 -0400
changeset 183660 5e0b16fe8951
parent 183659 85d2c5b844bc
child 183661 274d760590d5
push id3439
push usermconley@mozilla.com
push date2014-04-08 14:42 +0000
treeherdermozilla-beta@3f2d6f68c415 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjaws, sledru
bugs988191
milestone29.0
Bug 988191 - change to WCAG algorithm for titlebar font, r=jaws, a=sledru.
browser/base/content/browser.js
--- a/browser/base/content/browser.js
+++ b/browser/base/content/browser.js
@@ -1184,32 +1184,30 @@ var gBrowserInit = {
     }
 
 #ifdef XP_WIN
     if (window.matchMedia("(-moz-os-version: windows-win8)").matches &&
         window.matchMedia("(-moz-windows-default-theme)").matches) {
       let windows8WindowFrameColor = Cu.import("resource:///modules/Windows8WindowFrameColor.jsm", {}).Windows8WindowFrameColor;
       let windowFrameColor = windows8WindowFrameColor.get();
 
-      // Formula from W3C Techniques For Accessibility Evaluation And
-      // Repair Tools, Section 2.2 http://www.w3.org/TR/AERT#color
-      let brightnessThreshold = 125;
-      let colorThreshold = 500;
-      let bY = windowFrameColor[0] * .299 +
-               windowFrameColor[1] * .587 +
-               windowFrameColor[2] * .114;
-      let fY = 0; // Default to black for foreground text.
-      let brightnessDifference = Math.abs(bY - fY);
-      // Color difference calculation is simplified because black is 0 for R,G,B.
-      let colorDifference = windowFrameColor[0] + windowFrameColor[1] + windowFrameColor[2];
-
-      // Brightness is defined within {0, 255}. Set an attribute
-      // if the window frame color doesn't reach these thresholds
-      // so the theme can be adjusted for readability.
-      if (brightnessDifference < brightnessThreshold && colorDifference < colorThreshold) {
+      // Formula from W3C's WCAG 2.0 spec's color ratio and relative luminance,
+      // section 1.3.4, http://www.w3.org/TR/WCAG20/ .
+      windowFrameColor = windowFrameColor.map((color) => {
+        if (color <= 10) {
+          return color / 255 / 12.92;
+        }
+        return Math.pow(((color / 255) + 0.055) / 1.055, 2.4);
+      });
+      let backgroundLuminance = windowFrameColor[0] * 0.2126 +
+                                windowFrameColor[1] * 0.7152 +
+                                windowFrameColor[2] * 0.0722;
+      let foregroundLuminance = 0; // Default to black for foreground text.
+      let contrastRatio = (backgroundLuminance + 0.05) / (foregroundLuminance + 0.05);
+      if (contrastRatio < 3) {
         document.documentElement.setAttribute("darkwindowframe", "true");
       }
     }
 #endif
 
     if (gMultiProcessBrowser) {
       // Bug 862519 - Backspace doesn't work in electrolysis builds.
       // We bypass the problem by disabling the backspace-to-go-back command.