summaryrefslogtreecommitdiffstats
path: root/chrome/browser/profiles/profile_info_util.cc
blob: 83d38f5f7921860fdfc19a6adefd7d1630cb1926 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/profiles/profile_info_util.h"

#include "skia/ext/image_operations.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/rect.h"

namespace profiles {

const int kAvatarIconWidth = 38;
const int kAvatarIconHeight = 31;

gfx::Image GetAvatarIconForMenu(const gfx::Image& image,
                                bool is_gaia_picture) {
  if (!is_gaia_picture)
    return image;

  int length = std::min(kAvatarIconWidth, kAvatarIconHeight) - 2;
  SkBitmap bmp = skia::ImageOperations::Resize(
      *image.ToSkBitmap(), skia::ImageOperations::RESIZE_BEST, length, length);
  gfx::Canvas canvas(gfx::Size(kAvatarIconWidth, kAvatarIconHeight), false);

  // Draw the icon centered on the canvas.
  int x = (kAvatarIconWidth - length) / 2;
  int y = (kAvatarIconHeight - length) / 2;
  canvas.DrawImageInt(bmp, x, y);

  // Draw a gray border on the inside of the icon.
  SkColor color = SkColorSetARGB(83, 0, 0, 0);
  canvas.DrawRect(gfx::Rect(x, y, length - 1, length - 1), color);

  return gfx::Image(canvas.ExtractBitmap());
}

gfx::Image GetAvatarIconForWebUI(const gfx::Image& image,
                                 bool is_gaia_picture) {
  if (!is_gaia_picture)
    return image;

  int length = std::min(kAvatarIconWidth, kAvatarIconHeight) - 2;
  SkBitmap bmp = skia::ImageOperations::Resize(
      *image.ToSkBitmap(), skia::ImageOperations::RESIZE_BEST, length, length);
  gfx::Canvas canvas(gfx::Size(kAvatarIconWidth, kAvatarIconHeight), false);

  // Draw the icon centered on the canvas.
  int x = (kAvatarIconWidth - length) / 2;
  int y = (kAvatarIconHeight - length) / 2;
  canvas.DrawImageInt(bmp, x, y);

  return gfx::Image(canvas.ExtractBitmap());
}

gfx::Image GetAvatarIconForTitleBar(const gfx::Image& image,
                                    bool is_gaia_picture,
                                    int dst_width,
                                    int dst_height) {
  if (!is_gaia_picture)
    return image;

  int length = std::min(std::min(kAvatarIconWidth, kAvatarIconHeight),
      std::min(dst_width, dst_height)) - 2;
  SkBitmap bmp = skia::ImageOperations::Resize(
      *image.ToSkBitmap(), skia::ImageOperations::RESIZE_BEST, length, length);
  gfx::Canvas canvas(gfx::Size(dst_width, dst_height), false);

  // Draw the icon on the bottom center of the canvas.
  int x1 = (dst_width - length) / 2;
  int x2 = x1 + length;
  int y1 = dst_height - length - 1;
  int y2 = y1 + length;
  canvas.DrawImageInt(bmp, x1, y1);

  // Give the icon an etched look by drawing a highlight on the bottom edge
  // and a shadow on the remaining edges.
  SkColor highlight_color = SkColorSetARGB(128, 255, 255, 255);
  SkColor shadow_color = SkColorSetARGB(83, 0, 0, 0);
  // Bottom highlight.
  canvas.DrawLine(gfx::Point(x1, y2 - 1), gfx::Point(x2, y2 - 1),
                  highlight_color);
  // Top shadow.
  canvas.DrawLine(gfx::Point(x1, y1), gfx::Point(x2, y1), shadow_color);
  // Left shadow.
  canvas.DrawLine(gfx::Point(x1, y1 + 1), gfx::Point(x1, y2 - 1), shadow_color);
  // Right shadow.
  canvas.DrawLine(gfx::Point(x2 - 1, y1 + 1), gfx::Point(x2 - 1, y2 - 1),
                  shadow_color);

  return gfx::Image(canvas.ExtractBitmap());
}

} // namespace