From b7fda546dca1759cb1fe07de1a3d81b6d712314c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Tue, 13 Apr 2021 16:29:14 +0000 Subject: [PATCH] Bug 1476524 - Don't require so much contrast for selection background-against-background checks. r=jfkthame Differential Revision: https://phabricator.services.mozilla.com/D111794 --- layout/base/nsCSSColorUtils.h | 6 ++++++ layout/generic/nsTextFrame.cpp | 18 ++++++++++++++---- widget/gtk/nsLookAndFeel.cpp | 5 +---- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/layout/base/nsCSSColorUtils.h b/layout/base/nsCSSColorUtils.h index 23424beb94c9..e64241328e6b 100644 --- a/layout/base/nsCSSColorUtils.h +++ b/layout/base/nsCSSColorUtils.h @@ -15,6 +15,12 @@ // "Techniques For Accessibility Evalution And Repair Tools". // See http://www.w3.org/TR/AERT#color-contrast #define NS_SUFFICIENT_LUMINOSITY_DIFFERENCE 125000 +// NS_SUFFICIENT_LUMINOSITY_DIFFERENCE is the a11y standard for text +// on a background. Use 20% of that standard since we have a background +// on top of another background +#define NS_SUFFICIENT_LUMINOSITY_DIFFERENCE_BG \ + (NS_SUFFICIENT_LUMINOSITY_DIFFERENCE / 5) + #define NS_LUMINOSITY_DIFFERENCE(a, b) \ int32_t(mozilla::Abs(NS_GetLuminosity(a | 0xff000000) - \ NS_GetLuminosity(b | 0xff000000))) diff --git a/layout/generic/nsTextFrame.cpp b/layout/generic/nsTextFrame.cpp index 09263925f29e..d4bbab0c51cd 100644 --- a/layout/generic/nsTextFrame.cpp +++ b/layout/generic/nsTextFrame.cpp @@ -3837,10 +3837,18 @@ bool nsTextPaintStyle::EnsureSufficientContrast(nscolor* aForeColor, InitCommonColors(); // If the combination of selection background color and frame background color - // is sufficient contrast, don't exchange the selection colors. - int32_t backLuminosityDifference = + // has sufficient contrast, don't exchange the selection colors. + // + // Note we use a different threshold here: mSufficientContrast is for contrast + // between text and background colors, but since we're diffing two + // backgrounds, we don't need that much contrast. We match the heuristic from + // NS_SUFFICIENT_LUMINOSITY_DIFFERENCE_BG and use 20% of mSufficientContrast. + const int32_t minLuminosityDifferenceForBackground = mSufficientContrast / 5; + const int32_t backLuminosityDifference = NS_LUMINOSITY_DIFFERENCE(*aBackColor, mFrameBackgroundColor); - if (backLuminosityDifference >= mSufficientContrast) return false; + if (backLuminosityDifference >= minLuminosityDifferenceForBackground) { + return false; + } // Otherwise, we should use the higher-contrast color for the selection // background color. @@ -4023,7 +4031,9 @@ bool nsTextPaintStyle::GetSelectionUnderlineForPaint(int32_t aIndex, } void nsTextPaintStyle::InitCommonColors() { - if (mInitCommonColors) return; + if (mInitCommonColors) { + return; + } nsIFrame* bgFrame = nsCSSRendering::FindNonTransparentBackgroundFrame(mFrame); NS_ASSERTION(bgFrame, "Cannot find NonTransparentBackgroundFrame."); diff --git a/widget/gtk/nsLookAndFeel.cpp b/widget/gtk/nsLookAndFeel.cpp index e89ca29ea4b6..997b15ea8435 100644 --- a/widget/gtk/nsLookAndFeel.cpp +++ b/widget/gtk/nsLookAndFeel.cpp @@ -213,10 +213,7 @@ static bool GetBorderColors(GtkStyleContext* aContext, nscolor* aLightColor, // from both Highlight, used as focused+selected background, and the listbox // background which is assumed to be similar to -moz-field nsresult nsLookAndFeel::InitCellHighlightColors() { - // NS_SUFFICIENT_LUMINOSITY_DIFFERENCE is the a11y standard for text - // on a background. Use 20% of that standard since we have a background - // on top of another background - int32_t minLuminosityDifference = NS_SUFFICIENT_LUMINOSITY_DIFFERENCE / 5; + int32_t minLuminosityDifference = NS_SUFFICIENT_LUMINOSITY_DIFFERENCE_BG; int32_t backLuminosityDifference = NS_LUMINOSITY_DIFFERENCE(mMozWindowBackground, mFieldBackground); if (backLuminosityDifference >= minLuminosityDifference) {