Don't use InvertColor() to try and guarantee contrast.

In particular, inverting a light color may give you an even lighter color, e.g.
#8070ff (contrast ratio against white 3.69) inverts to #7f8f00 (contrast ratio
against white 3.6).  There's a function in color_utils called GetReadableColor()
that tries to lightness-invert the color, but that turns out not to work well
either, e.g. HSL 60,100%,36% (contrast ratio against white 2.12)
lightness-inverts to HSL 60,100%,64% (contrast ratio against white 1.06).

Instead use GetColorWithMinimumContrast().  This will reduce the ontrast of some
cases compared to current behavior, but it will also guarantee no cases are
below the minimum.

Bug: 659451
Change-Id: I09b5850e0ba3b76ca6e004c9320131ac175cbe43
Reviewed-on: https://chromium-review.googlesource.com/c/1406241
Reviewed-by: Kristi Park <kristipark@chromium.org>
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#622194}
diff --git a/chrome/browser/search/ntp_icon_source.cc b/chrome/browser/search/ntp_icon_source.cc
index a971360..1b5e246 100644
--- a/chrome/browser/search/ntp_icon_source.cc
+++ b/chrome/browser/search/ntp_icon_source.cc
@@ -52,6 +52,10 @@
 
 namespace {
 
+// The color of the letter drawn for a fallback icon.  Changing this may require
+// changing the algorithm in RenderIconBitmap() that guarantees contrast.
+constexpr SkColor kFallbackIconLetterColor = SK_ColorWHITE;
+
 // Delimiter in the url that looks for the size specification.
 const char kSizeParameter[] = "size/";
 
@@ -168,7 +172,7 @@
       icon_text,
       gfx::FontList({l10n_util::GetStringUTF8(IDS_NTP_FONT_FAMILY)},
                     gfx::Font::NORMAL, font_size, font_weight),
-      SK_ColorWHITE, gfx::Rect(0, 0, size, size),
+      kFallbackIconLetterColor, gfx::Rect(0, 0, size, size),
       gfx::Canvas::TEXT_ALIGN_CENTER);
 }
 
@@ -214,12 +218,8 @@
 
   // If necessary, draw the colored fallback monogram.
   if (favicon.empty()) {
-    SkColor fallback_color = GetBackgroundColorForUrl(icon_url);
-    // If luminance is too high, the white text will become unreadable. Invert
-    // the background color to achieve better contrast. The constant comes from
-    // W3C Accessibility standards.
-    if (color_utils::GetRelativeLuminance(fallback_color) > 0.179f)
-      fallback_color = color_utils::InvertColor(fallback_color);
+    SkColor fallback_color = color_utils::GetColorWithMinimumContrast(
+        GetBackgroundColorForUrl(icon_url), kFallbackIconLetterColor);
 
     int offset = (icon_size - fallback_size) / 2;
     DrawCircleInCanvas(&canvas, fallback_size, offset, fallback_color);