Skip to content

Commit dba741d

Browse files
author
Mathieu Perreault
committed
[New Tab] Have default background colors for tiles with no favicon.
TBR=mathp@chromium.org (cherry picked from commit 6b5891c) Bug: 853780 Test: ntp_render_browsertests Change-Id: I57d4321f6a72d1171066b1578128117c91521bfd Reviewed-on: https://chromium-review.googlesource.com/1145693 Commit-Queue: Mathieu Perreault <mathp@chromium.org> Reviewed-by: Fernando Serboncini <fserb@chromium.org> Cr-Original-Commit-Position: refs/heads/master@{#577298} Reviewed-on: https://chromium-review.googlesource.com/1149116 Reviewed-by: Mathieu Perreault <mathp@chromium.org> Cr-Commit-Position: refs/branch-heads/3497@{#60} Cr-Branched-From: 271eaf5-refs/heads/master@{#576753}
1 parent b638de5 commit dba741d

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

chrome/browser/search/ntp_icon_source.cc

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "base/callback.h"
1212
#include "base/memory/ref_counted_memory.h"
13+
#include "base/sha1.h"
1314
#include "base/strings/string_number_conversions.h"
1415
#include "cc/paint/skia_paint_canvas.h"
1516
#include "chrome/browser/favicon/favicon_service_factory.h"
@@ -36,6 +37,7 @@
3637
#include "ui/base/webui/web_ui_util.h"
3738
#include "ui/gfx/canvas.h"
3839
#include "ui/gfx/codec/png_codec.h"
40+
#include "ui/gfx/color_utils.h"
3941
#include "ui/gfx/favicon_size.h"
4042
#include "ui/gfx/font_list.h"
4143
#include "ui/gfx/geometry/rect.h"
@@ -162,6 +164,18 @@ void DrawFavicon(const SkBitmap& bitmap, gfx::Canvas* canvas, int size) {
162164
x_origin, y_origin);
163165
}
164166

167+
// Returns a color that based on the hash of |icon_url|'s origin.
168+
SkColor GetBackgroundColorForUrl(const GURL& icon_url) {
169+
if (!icon_url.is_valid())
170+
return SK_ColorGRAY;
171+
172+
unsigned char hash[20];
173+
const std::string origin = icon_url.GetOrigin().spec();
174+
base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(origin.c_str()),
175+
origin.size(), hash);
176+
return SkColorSetRGB(hash[0], hash[1], hash[2]);
177+
}
178+
165179
// For the given |icon_url|, will render a fallback icon with an appropriate
166180
// letter in a circle.
167181
std::vector<unsigned char> RenderIconBitmap(const GURL& icon_url,
@@ -177,8 +191,14 @@ std::vector<unsigned char> RenderIconBitmap(const GURL& icon_url,
177191
DrawCircleInCanvas(&canvas, size, /*background_color=*/kFaviconBackground);
178192
DrawFavicon(favicon, &canvas, size);
179193
} else {
180-
// TODO(crbug.com/853780): Set the appropriate fallback background color.
181-
DrawCircleInCanvas(&canvas, size, /*background_color=*/SK_ColorGRAY);
194+
SkColor background_color = GetBackgroundColorForUrl(icon_url);
195+
// If luminance is too high, the white text will become unreadable. Invert
196+
// the background color to achieve better constrast. The constant comes
197+
// W3C Accessibility standards.
198+
if (color_utils::GetRelativeLuminance(background_color) > 0.179)
199+
background_color = color_utils::InvertColor(background_color);
200+
201+
DrawCircleInCanvas(&canvas, size, background_color);
182202
DrawFallbackIconLetter(icon_url, size, &canvas);
183203
}
184204
std::vector<unsigned char> bitmap_data;

0 commit comments

Comments
 (0)