Bug 1344910, part 2 - Add a '-moz-win-accentcolortext' color keyword to color text that will be drawn over an accent color background. r=jimm
☠☠ backed out by 3878dd93fa71 ☠ ☠
authorJonathan Watt <jwatt@jwatt.org>
Mon, 05 Jun 2017 15:56:29 +0100
changeset 598343 94f08c38200bafa9c76234494f6a0b858f3e82eb
parent 598342 31a3362f641c45953dc820e2c8cadd42ca3cc51e
child 598344 af94bc2592d10b5a130426a3b676758d81a63daa
push id65172
push usergpascutto@mozilla.com
push dateWed, 21 Jun 2017 16:21:54 +0000
reviewersjimm
bugs1344910
milestone56.0a1
Bug 1344910, part 2 - Add a '-moz-win-accentcolortext' color keyword to color text that will be drawn over an accent color background. r=jimm MozReview-Commit-ID: 2HgWbx6eF48
layout/style/nsCSSKeywordList.h
layout/style/nsCSSProps.cpp
widget/LookAndFeel.h
widget/nsXPLookAndFeel.cpp
widget/windows/nsLookAndFeel.cpp
widget/windows/nsLookAndFeel.h
--- a/layout/style/nsCSSKeywordList.h
+++ b/layout/style/nsCSSKeywordList.h
@@ -743,16 +743,17 @@ CSS_KEY(checkbox-container, checkbox_con
 CSS_KEY(radio-container, radio_container)
 CSS_KEY(checkbox-label, checkbox_label)
 CSS_KEY(radio-label, radio_label)
 CSS_KEY(button-focus, button_focus)
 CSS_KEY(-moz-win-media-toolbox, _moz_win_media_toolbox)
 CSS_KEY(-moz-win-communications-toolbox, _moz_win_communications_toolbox)
 CSS_KEY(-moz-win-browsertabbar-toolbox, _moz_win_browsertabbar_toolbox)
 CSS_KEY(-moz-win-accentcolor, _moz_win_accentcolor)
+CSS_KEY(-moz-win-accentcolortext, _moz_win_accentcolortext)
 CSS_KEY(-moz-win-mediatext, _moz_win_mediatext)
 CSS_KEY(-moz-win-communicationstext, _moz_win_communicationstext)
 CSS_KEY(-moz-win-glass, _moz_win_glass)
 CSS_KEY(-moz-win-borderless-glass, _moz_win_borderless_glass)
 CSS_KEY(-moz-window-titlebar, _moz_window_titlebar)
 CSS_KEY(-moz-window-titlebar-maximized, _moz_window_titlebar_maximized)
 CSS_KEY(-moz-window-frame-left, _moz_window_frame_left)
 CSS_KEY(-moz-window-frame-right, _moz_window_frame_right)
--- a/layout/style/nsCSSProps.cpp
+++ b/layout/style/nsCSSProps.cpp
@@ -1133,16 +1133,17 @@ const KTableEntry nsCSSProps::kColorKTab
   { eCSSKeyword__moz_menuhover, LookAndFeel::eColorID__moz_menuhover },
   { eCSSKeyword__moz_menuhovertext, LookAndFeel::eColorID__moz_menuhovertext },
   { eCSSKeyword__moz_menubartext, LookAndFeel::eColorID__moz_menubartext },
   { eCSSKeyword__moz_menubarhovertext, LookAndFeel::eColorID__moz_menubarhovertext },
   { eCSSKeyword__moz_oddtreerow, LookAndFeel::eColorID__moz_oddtreerow },
   { eCSSKeyword__moz_visitedhyperlinktext, NS_COLOR_MOZ_VISITEDHYPERLINKTEXT },
   { eCSSKeyword_currentcolor, NS_COLOR_CURRENTCOLOR },
   { eCSSKeyword__moz_win_accentcolor, LookAndFeel::eColorID__moz_win_accentcolor },
+  { eCSSKeyword__moz_win_accentcolortext, LookAndFeel::eColorID__moz_win_accentcolortext },
   { eCSSKeyword__moz_win_mediatext, LookAndFeel::eColorID__moz_win_mediatext },
   { eCSSKeyword__moz_win_communicationstext, LookAndFeel::eColorID__moz_win_communicationstext },
   { eCSSKeyword__moz_nativehyperlinktext, LookAndFeel::eColorID__moz_nativehyperlinktext },
   { eCSSKeyword__moz_comboboxtext, LookAndFeel::eColorID__moz_comboboxtext },
   { eCSSKeyword__moz_combobox, LookAndFeel::eColorID__moz_combobox },
   { eCSSKeyword_UNKNOWN, -1 }
 };
 
--- a/widget/LookAndFeel.h
+++ b/widget/LookAndFeel.h
@@ -155,16 +155,18 @@ public:
     eColorID__moz_mac_disabledtoolbartext,
     //inactive light hightlight
     eColorID__moz_mac_secondaryhighlight,
 
     // vista rebars
 
     // accent color for title bar
     eColorID__moz_win_accentcolor,
+    // color from drawing text over the accent color
+    eColorID__moz_win_accentcolortext,
     // media rebar text
     eColorID__moz_win_mediatext,
     // communications rebar text
     eColorID__moz_win_communicationstext,
 
     // Hyperlink color extracted from the system, not affected by the
     // browser.anchor_color user pref.
     // There is no OS-specified safe background color for this text,
--- a/widget/nsXPLookAndFeel.cpp
+++ b/widget/nsXPLookAndFeel.cpp
@@ -656,16 +656,18 @@ nsXPLookAndFeel::GetStandinForNativeColo
       result = NS_RGB(0xFF, 0xFF, 0xFF); break;
     case eColorID__moz_mac_disabledtoolbartext:
       result = NS_RGB(0x3F, 0x3F, 0x3F); break;
     case eColorID__moz_mac_secondaryhighlight:
       result = NS_RGB(0xD4, 0xD4, 0xD4); break;
     case eColorID__moz_win_accentcolor:
       // Seems to be the default color (hardcoded because of bug 1065998)
       result = NS_RGB(0x9E, 0x9E, 0x9E); break;
+    case eColorID__moz_win_accentcolortext:
+      result = NS_RGB(0x00, 0x00, 0x00); break;
     case eColorID__moz_win_mediatext:
       result = NS_RGB(0xFF, 0xFF, 0xFF); break;
     case eColorID__moz_win_communicationstext:
       result = NS_RGB(0xFF, 0xFF, 0xFF); break;
     case eColorID__moz_nativehyperlinktext:
       result = NS_RGB(0x00, 0x66, 0xCC); break;
     case eColorID__moz_comboboxtext:
       result = NS_RGB(0x00, 0x00, 0x00); break;
--- a/widget/windows/nsLookAndFeel.cpp
+++ b/widget/windows/nsLookAndFeel.cpp
@@ -267,16 +267,23 @@ nsLookAndFeel::NativeGetColor(ColorID aI
       if (NS_SUCCEEDED(res)) {
         return res;
       }
       NS_WARNING("Using fallback for accent color - UI code failed to use the "
                  "-moz-windows-accent-color-applies media query properly");
       // Seems to be the default color (hardcoded because of bug 1065998)
       aColor = NS_RGB(158, 158, 158);
       return NS_OK;
+    case eColorID__moz_win_accentcolortext:
+      res = GetAccentColorText(aColor);
+      if (NS_SUCCEEDED(res)) {
+        return res;
+      }
+      aColor = NS_RGB(0, 0, 0);
+      return NS_OK;
     case eColorID__moz_win_mediatext:
       if (IsAppThemed()) {
         res = ::GetColorFromTheme(eUXMediaToolbar,
                                   TP_BUTTON, TS_NORMAL, TMT_TEXTCOLOR, aColor);
         if (NS_SUCCEEDED(res))
           return res;
       }
       // if we've gotten here just return -moz-dialogtext instead
@@ -791,8 +798,33 @@ nsLookAndFeel::GetAccentColor(nscolor& a
   } else {
     rv = NS_ERROR_NOT_AVAILABLE;
   }
 
   mDwmKey->Close();
 
   return rv;
 }
+
+/* static */ nsresult
+nsLookAndFeel::GetAccentColorText(nscolor& aColor)
+{
+  nscolor accentColor;
+  nsresult rv = GetAccentColor(accentColor);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+
+  // We want the color that we return for text that will be drawn over
+  // a background that has the accent color to have good contrast with
+  // the accent color.  Windows itself uses either white or black text
+  // depending on how light or dark the accent color is.  We do the same
+  // here based on the luminance of the accent color with a threshhold
+  // value that seem consistent with what Windows does.
+
+  float luminance = 0.2125f * NS_GET_R(accentColor) +
+                    0.7154f * NS_GET_G(accentColor) +
+                    0.0721f * NS_GET_B(accentColor);
+
+  aColor = (luminance <= 110) ? NS_RGB(255, 255, 255) : NS_RGB(0, 0, 0);
+
+  return NS_OK;
+}
--- a/widget/windows/nsLookAndFeel.h
+++ b/widget/windows/nsLookAndFeel.h
@@ -54,16 +54,25 @@ public:
 private:
   /**
    * Fetches the Windows accent color from the Windows settings if
    * the accent color is set to apply to the title bar, otherwise
    * returns an error code.
    */
   nsresult GetAccentColor(nscolor& aColor);
 
+  /**
+   * If the Windows accent color from the Windows settings is set
+   * to apply to the title bar, this computes the color that should
+   * be used for text that is to be written over a background that has
+   * the accent color.  Otherwise, (if the accent color should not
+   * apply to the title bar) this returns an error code.
+   */
+  nsresult GetAccentColorText(nscolor& aColor);
+
   // Content process cached values that get shipped over from the browser
   // process.
   int32_t mUseAccessibilityTheme;
   int32_t mUseDefaultTheme; // is the current theme a known default?
   int32_t mNativeThemeId; // see LookAndFeel enum 'WindowsTheme'
 
   struct CachedSystemFont {
     CachedSystemFont()