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
--- 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;
}