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.
5 #include "chrome/common/icon_with_badge_image_source.h"
9 #include "chrome/common/badge_util.h"
10 #include "ui/gfx/canvas.h"
11 #include "ui/gfx/geometry/rect.h"
12 #include "ui/gfx/image/image_skia_operations.h"
14 IconWithBadgeImageSource::Badge::Badge(std::string text
,
16 SkColor background_color
)
17 : text(text
), text_color(text_color
), background_color(background_color
) {
20 IconWithBadgeImageSource::Badge::~Badge() {}
22 IconWithBadgeImageSource::IconWithBadgeImageSource(const gfx::Size
& size
)
23 : gfx::CanvasImageSource(size
, false),
25 paint_decoration_(false) {
28 IconWithBadgeImageSource::~IconWithBadgeImageSource() {
31 void IconWithBadgeImageSource::SetIcon(const gfx::Image
& icon
) {
35 void IconWithBadgeImageSource::SetBadge(scoped_ptr
<Badge
> badge
) {
36 badge_
= badge
.Pass();
39 void IconWithBadgeImageSource::Draw(gfx::Canvas
* canvas
) {
43 int x_offset
= std::floor((size().width() - icon_
.Width()) / 2.0);
44 int y_offset
= std::floor((size().height() - icon_
.Height()) / 2.0);
45 gfx::ImageSkia skia
= icon_
.AsImageSkia();
47 skia
= gfx::ImageSkiaOperations::CreateHSLShiftedImage(skia
, {-1, 0, 0.6});
48 canvas
->DrawImageInt(skia
, x_offset
, y_offset
);
50 // Draw a badge on the provided browser action icon's canvas.
51 // TODO(devlin): Pull PaintBadge() into here.
53 badge_util::PaintBadge(canvas
, gfx::Rect(size()), badge_
->text
,
54 badge_
->text_color
, badge_
->background_color
,
58 if (paint_decoration_
)
59 PaintDecoration(canvas
);
62 void IconWithBadgeImageSource::PaintDecoration(gfx::Canvas
* canvas
) {
63 static const SkColor decoration_color
= SkColorSetARGB(255, 70, 142, 226);
65 int major_radius
= std::ceil(size().width() / 5.0);
66 int minor_radius
= std::ceil(major_radius
/ 2.0);
67 gfx::Point
center_point(major_radius
+ 1,
68 size().height() - (major_radius
) - 1);
70 paint
.setAntiAlias(true);
71 paint
.setStyle(SkPaint::kFill_Style
);
72 paint
.setColor(SK_ColorTRANSPARENT
);
73 paint
.setXfermodeMode(SkXfermode::kSrc_Mode
);
74 canvas
->DrawCircle(center_point
,
77 paint
.setColor(decoration_color
);
78 canvas
->DrawCircle(center_point
,