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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/profiles/profile_info_util.h" 6 7 #include "skia/ext/image_operations.h" 8 #include "ui/gfx/canvas.h" 9 #include "ui/gfx/rect.h" 10 11 namespace profiles { 12 13 const int kAvatarIconWidth = 38; 14 const int kAvatarIconHeight = 31; 15 const int kAvatarIconBorder = 2; 16 17 gfx::Image GetSizedAvatarIconWithBorder(const gfx::Image& image, 18 bool is_gaia_picture, 19 int width, int height) { 20 if (!is_gaia_picture) 21 return image; 22 23 int length = std::min(width, height) - kAvatarIconBorder; 24 SkBitmap bmp = skia::ImageOperations::Resize( 25 *image.ToSkBitmap(), skia::ImageOperations::RESIZE_BEST, length, length); 26 gfx::Canvas canvas(gfx::Size(width, height), ui::SCALE_FACTOR_100P, false); 27 28 // Draw the icon centered on the canvas. 29 int x = (width - length) / 2; 30 int y = (height - length) / 2; 31 canvas.DrawImageInt(gfx::ImageSkia::CreateFrom1xBitmap(bmp), x, y); 32 33 // Draw a gray border on the inside of the icon. 34 SkColor color = SkColorSetARGB(83, 0, 0, 0); 35 canvas.DrawRect(gfx::Rect(x, y, length - 1, length - 1), color); 36 37 return gfx::Image(gfx::ImageSkia(canvas.ExtractImageRep())); 38 } 39 40 gfx::Image GetAvatarIconForMenu(const gfx::Image& image, 41 bool is_gaia_picture) { 42 return GetSizedAvatarIconWithBorder( 43 image, is_gaia_picture, kAvatarIconWidth, kAvatarIconHeight); 44 } 45 46 gfx::Image GetAvatarIconForWebUI(const gfx::Image& image, 47 bool is_gaia_picture) { 48 if (!is_gaia_picture) 49 return image; 50 51 int length = 52 std::min(kAvatarIconWidth, kAvatarIconHeight) - kAvatarIconBorder; 53 SkBitmap bmp = skia::ImageOperations::Resize( 54 *image.ToSkBitmap(), skia::ImageOperations::RESIZE_BEST, length, length); 55 gfx::Canvas canvas(gfx::Size(kAvatarIconWidth, kAvatarIconHeight), 56 ui::SCALE_FACTOR_100P, false); 57 58 // Draw the icon centered on the canvas. 59 int x = (kAvatarIconWidth - length) / 2; 60 int y = (kAvatarIconHeight - length) / 2; 61 canvas.DrawImageInt(gfx::ImageSkia::CreateFrom1xBitmap(bmp), x, y); 62 63 return gfx::Image(gfx::ImageSkia(canvas.ExtractImageRep())); 64 } 65 66 gfx::Image GetAvatarIconForTitleBar(const gfx::Image& image, 67 bool is_gaia_picture, 68 int dst_width, 69 int dst_height) { 70 if (!is_gaia_picture) 71 return image; 72 73 int length = std::min(std::min(kAvatarIconWidth, kAvatarIconHeight), 74 std::min(dst_width, dst_height)) - kAvatarIconBorder; 75 SkBitmap bmp = skia::ImageOperations::Resize( 76 *image.ToSkBitmap(), skia::ImageOperations::RESIZE_BEST, length, length); 77 gfx::Canvas canvas(gfx::Size(dst_width, dst_height), ui::SCALE_FACTOR_100P, 78 false); 79 80 // Draw the icon on the bottom center of the canvas. 81 int x1 = (dst_width - length) / 2; 82 int x2 = x1 + length; 83 int y1 = dst_height - length - 1; 84 int y2 = y1 + length; 85 canvas.DrawImageInt(gfx::ImageSkia::CreateFrom1xBitmap(bmp), x1, y1); 86 87 // Give the icon an etched look by drawing a highlight on the bottom edge 88 // and a shadow on the remaining edges. 89 SkColor highlight_color = SkColorSetARGB(128, 255, 255, 255); 90 SkColor shadow_color = SkColorSetARGB(83, 0, 0, 0); 91 // Bottom highlight. 92 canvas.DrawLine(gfx::Point(x1, y2 - 1), gfx::Point(x2, y2 - 1), 93 highlight_color); 94 // Top shadow. 95 canvas.DrawLine(gfx::Point(x1, y1), gfx::Point(x2, y1), shadow_color); 96 // Left shadow. 97 canvas.DrawLine(gfx::Point(x1, y1 + 1), gfx::Point(x1, y2 - 1), shadow_color); 98 // Right shadow. 99 canvas.DrawLine(gfx::Point(x2 - 1, y1 + 1), gfx::Point(x2 - 1, y2 - 1), 100 shadow_color); 101 102 return gfx::Image(gfx::ImageSkia(canvas.ExtractImageRep())); 103 } 104 105 } // namespace 106