OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/metro_pin_tab_helper_win.h" | 5 #include "chrome/browser/ui/metro_pin_tab_helper_win.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/base_paths.h" | 9 #include "base/base_paths.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
25 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
26 #include "crypto/sha2.h" | 26 #include "crypto/sha2.h" |
27 #include "third_party/skia/include/core/SkCanvas.h" | 27 #include "third_party/skia/include/core/SkCanvas.h" |
28 #include "third_party/skia/include/core/SkColor.h" | 28 #include "third_party/skia/include/core/SkColor.h" |
29 #include "ui/gfx/canvas.h" | 29 #include "ui/gfx/canvas.h" |
30 #include "ui/gfx/codec/png_codec.h" | 30 #include "ui/gfx/codec/png_codec.h" |
31 #include "ui/gfx/color_analysis.h" | 31 #include "ui/gfx/color_analysis.h" |
32 #include "ui/gfx/color_utils.h" | 32 #include "ui/gfx/color_utils.h" |
33 #include "ui/gfx/image/image.h" | 33 #include "ui/gfx/image/image.h" |
| 34 #include "ui/gfx/image/image_skia.h" |
34 #include "ui/gfx/rect.h" | 35 #include "ui/gfx/rect.h" |
35 #include "ui/gfx/size.h" | 36 #include "ui/gfx/size.h" |
36 | 37 |
37 DEFINE_WEB_CONTENTS_USER_DATA_KEY(MetroPinTabHelper) | 38 DEFINE_WEB_CONTENTS_USER_DATA_KEY(MetroPinTabHelper) |
38 | 39 |
39 namespace { | 40 namespace { |
40 | 41 |
41 // Histogram name for site-specific tile pinning metrics. | 42 // Histogram name for site-specific tile pinning metrics. |
42 const char kMetroPinMetric[] = "Metro.SecondaryTilePin"; | 43 const char kMetroPinMetric[] = "Metro.SecondaryTilePin"; |
43 | 44 |
(...skipping 26 matching lines...) Expand all Loading... |
70 bool CreateSiteSpecificLogo(const gfx::ImageSkia& image, | 71 bool CreateSiteSpecificLogo(const gfx::ImageSkia& image, |
71 const string16& tile_id, | 72 const string16& tile_id, |
72 const FilePath& logo_dir, | 73 const FilePath& logo_dir, |
73 FilePath* logo_path) { | 74 FilePath* logo_path) { |
74 const int kLogoWidth = 120; | 75 const int kLogoWidth = 120; |
75 const int kLogoHeight = 120; | 76 const int kLogoHeight = 120; |
76 const int kBoxWidth = 40; | 77 const int kBoxWidth = 40; |
77 const int kBoxHeight = 40; | 78 const int kBoxHeight = 40; |
78 const int kCaptionHeight = 20; | 79 const int kCaptionHeight = 20; |
79 const double kBoxFade = 0.75; | 80 const double kBoxFade = 0.75; |
80 const int kColorMeanDarknessLimit = 100; | |
81 const int kColorMeanLightnessLimit = 650; | |
82 | 81 |
83 if (image.isNull()) | 82 if (image.isNull()) |
84 return false; | 83 return false; |
85 | 84 |
86 // First paint the image onto an opaque background to get rid of transparency. | 85 // Fill the tile logo with the dominant color of the favicon bitmap. |
87 // White is used as it will be disregarded in the mean calculation because of | 86 SkColor dominant_color = color_utils::CalculateKMeanColorOfBitmap( |
88 // lightness limit. | 87 image.GetRepresentation(ui::SCALE_FACTOR_100P).sk_bitmap()); |
89 SkPaint paint; | 88 SkPaint paint; |
90 paint.setColor(SK_ColorWHITE); | 89 paint.setColor(dominant_color); |
91 gfx::Canvas favicon_canvas(gfx::Size(image.width(), image.height()), | |
92 ui::SCALE_FACTOR_100P, true); | |
93 favicon_canvas.DrawRect(gfx::Rect(0, 0, image.width(), image.height()), | |
94 paint); | |
95 favicon_canvas.DrawImageInt(image, 0, 0); | |
96 | |
97 // Fill the tile logo with the average color from bitmap. To do this we need | |
98 // to work out the 'average color' which is calculated using PNG encoded data | |
99 // of the bitmap. | |
100 std::vector<unsigned char> icon_png; | |
101 if (!gfx::PNGCodec::EncodeBGRASkBitmap( | |
102 favicon_canvas.ExtractImageRep().sk_bitmap(), false, &icon_png)) { | |
103 return false; | |
104 } | |
105 | |
106 scoped_refptr<base::RefCountedStaticMemory> icon_mem( | |
107 new base::RefCountedStaticMemory(&icon_png.front(), icon_png.size())); | |
108 color_utils::GridSampler sampler; | |
109 SkColor mean_color = color_utils::CalculateKMeanColorOfPNG( | |
110 icon_mem, kColorMeanDarknessLimit, kColorMeanLightnessLimit, &sampler); | |
111 paint.setColor(mean_color); | |
112 gfx::Canvas canvas(gfx::Size(kLogoWidth, kLogoHeight), ui::SCALE_FACTOR_100P, | 90 gfx::Canvas canvas(gfx::Size(kLogoWidth, kLogoHeight), ui::SCALE_FACTOR_100P, |
113 true); | 91 true); |
114 canvas.DrawRect(gfx::Rect(0, 0, kLogoWidth, kLogoHeight), paint); | 92 canvas.DrawRect(gfx::Rect(0, 0, kLogoWidth, kLogoHeight), paint); |
115 | 93 |
116 // Now paint a faded square for the favicon to go in. | 94 // Now paint a faded square for the favicon to go in. |
117 color_utils::HSL shift = {-1, -1, kBoxFade}; | 95 color_utils::HSL shift = {-1, -1, kBoxFade}; |
118 paint.setColor(color_utils::HSLShift(mean_color, shift)); | 96 paint.setColor(color_utils::HSLShift(dominant_color, shift)); |
119 int box_left = (kLogoWidth - kBoxWidth) / 2; | 97 int box_left = (kLogoWidth - kBoxWidth) / 2; |
120 int box_top = (kLogoHeight - kCaptionHeight - kBoxHeight) / 2; | 98 int box_top = (kLogoHeight - kCaptionHeight - kBoxHeight) / 2; |
121 canvas.DrawRect(gfx::Rect(box_left, box_top, kBoxWidth, kBoxHeight), paint); | 99 canvas.DrawRect(gfx::Rect(box_left, box_top, kBoxWidth, kBoxHeight), paint); |
122 | 100 |
123 // Now paint the favicon into the tile, leaving some room at the bottom for | 101 // Now paint the favicon into the tile, leaving some room at the bottom for |
124 // the caption. | 102 // the caption. |
125 int left = (kLogoWidth - image.width()) / 2; | 103 int left = (kLogoWidth - image.width()) / 2; |
126 int top = (kLogoHeight - kCaptionHeight - image.height()) / 2; | 104 int top = (kLogoHeight - kCaptionHeight - image.height()) / 2; |
127 canvas.DrawImageInt(image, left, top); | 105 canvas.DrawImageInt(image, left, top); |
128 | 106 |
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
476 | 454 |
477 GURL url = web_contents()->GetURL(); | 455 GURL url = web_contents()->GetURL(); |
478 string16 tile_id = GenerateTileId(UTF8ToUTF16(url.spec())); | 456 string16 tile_id = GenerateTileId(UTF8ToUTF16(url.spec())); |
479 metro_un_pin_from_start_screen(tile_id, | 457 metro_un_pin_from_start_screen(tile_id, |
480 base::Bind(&PinPageReportUmaCallback)); | 458 base::Bind(&PinPageReportUmaCallback)); |
481 } | 459 } |
482 | 460 |
483 void MetroPinTabHelper::FaviconDownloaderFinished() { | 461 void MetroPinTabHelper::FaviconDownloaderFinished() { |
484 favicon_chooser_.reset(); | 462 favicon_chooser_.reset(); |
485 } | 463 } |
OLD | NEW |