Bug 1365869 - Bug 1365869 - Better contrast comparison for Selection#setColors(). r=masayuki draft
authorTimothy Guan-tin Chien <timdream@gmail.com>
Thu, 18 May 2017 16:49:51 +0800
changeset 580322 baca19289313e46c70cae4f38d85bea1e285a3ef
parent 580040 baf05f61bc14fdf45511bc1165ce76daa08c5c0f
child 580974 637c13a3391f84aded6c5507ba82cab279314cbf
child 580977 842cf22af80ce7fc4c53b47e466a56fa50e3cdd9
push id59512
push userbmo:timdream@gmail.com
push dateThu, 18 May 2017 10:39:56 +0000
reviewersmasayuki
bugs1365869
milestone55.0a1
Bug 1365869 - Bug 1365869 - Better contrast comparison for Selection#setColors(). r=masayuki Instead of hardcoding a color-to-compare, compare the contrast between the text and the background color v.s. text and the alternative background color, and use the color that has better contrast. MozReview-Commit-ID: D90047Y0Xst
layout/generic/nsTextFrame.cpp
--- a/layout/generic/nsTextFrame.cpp
+++ b/layout/generic/nsTextFrame.cpp
@@ -3859,54 +3859,60 @@ nsTextPaintStyle::GetHighlightColors(nsc
       backColor = *customColors->mAltBackgroundColor;
     }
 
     *aForeColor = foreColor;
     *aBackColor = backColor;
     return;
   }
 
+  InitCommonColors();
+
   if (customColors->mBackgroundColor) {
     // !mForegroundColor means "currentColor"; the current color of the text.
     nscolor foreColor = GetTextColor();
     nscolor backColor = *customColors->mBackgroundColor;
 
-    if (customColors->mAltBackgroundColor) {
-      int32_t foreLuminosityDifference =
-                NS_LUMINOSITY_DIFFERENCE(foreColor, backColor);
-
-      // The sufficient luminosity difference is based on the link color of
-      // about:preferences, so we don't invert the background color on these text.
-      // XXX: Make this more generic.
-      int32_t sufficientLuminosityDifference =
-                NS_LUMINOSITY_DIFFERENCE(NS_RGBA(23, 140, 229, 255), backColor);
-
-      if (foreLuminosityDifference < sufficientLuminosityDifference) {
+    int32_t luminosityDifference =
+              NS_LUMINOSITY_DIFFERENCE(foreColor, backColor);
+
+    if (mSufficientContrast > luminosityDifference &&
+        customColors->mAltBackgroundColor) {
+      int32_t altLuminosityDifference =
+                NS_LUMINOSITY_DIFFERENCE(foreColor, *customColors->mAltBackgroundColor);
+
+      if (luminosityDifference < altLuminosityDifference) {
         backColor = *customColors->mAltBackgroundColor;
       }
     }
 
     *aForeColor = foreColor;
     *aBackColor = backColor;
     return;
   }
 
   if (customColors->mForegroundColor) {
     nscolor foreColor = *customColors->mForegroundColor;
     // !mBackgroundColor means "transparent"; the current color of the background.
-    nscolor backColor = mFrameBackgroundColor;
-
-    if (customColors->mAltForegroundColor &&
-        EnsureSufficientContrast(&foreColor, &backColor)) {
-      foreColor = *customColors->mAltForegroundColor;
-      backColor = mFrameBackgroundColor;
+
+    int32_t luminosityDifference =
+              NS_LUMINOSITY_DIFFERENCE(foreColor, mFrameBackgroundColor);
+
+    if (mSufficientContrast > luminosityDifference &&
+        customColors->mAltForegroundColor) {
+      int32_t altLuminosityDifference =
+                NS_LUMINOSITY_DIFFERENCE(*customColors->mForegroundColor, mFrameBackgroundColor);
+
+      if (luminosityDifference < altLuminosityDifference) {
+        foreColor = *customColors->mAltForegroundColor;
+      }
     }
 
     *aForeColor = foreColor;
-    *aBackColor = backColor;
+    *aBackColor = NS_TRANSPARENT;
     return;
   }
 
   // There are neither mForegroundColor nor mBackgroundColor.
   *aForeColor = GetTextColor();
   *aBackColor = NS_TRANSPARENT;
 }