1 // Copyright (c) 2011 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/ui/views/infobars/infobar_background.h"
6
7 #include "chrome/browser/ui/views/infobars/infobar_view.h"
8 #include "ui/gfx/canvas.h"
9 #include "ui/gfx/canvas_skia_paint.h"
10 #include "third_party/skia/include/effects/SkGradientShader.h"
11 #include "views/view.h"
12
InfoBarBackground(InfoBarDelegate::Type infobar_type)13 InfoBarBackground::InfoBarBackground(InfoBarDelegate::Type infobar_type)
14 : separator_color_(SK_ColorBLACK),
15 top_color_(GetTopColor(infobar_type)),
16 bottom_color_(GetBottomColor(infobar_type)) {
17 }
18
~InfoBarBackground()19 InfoBarBackground::~InfoBarBackground() {
20 }
21
GetTopColor(InfoBarDelegate::Type infobar_type)22 SkColor InfoBarBackground::GetTopColor(InfoBarDelegate::Type infobar_type) {
23 static const SkColor kWarningBackgroundColorTop =
24 SkColorSetRGB(255, 242, 183);
25 static const SkColor kPageActionBackgroundColorTop =
26 SkColorSetRGB(218, 231, 249);
27
28 return (infobar_type == InfoBarDelegate::WARNING_TYPE) ?
29 kWarningBackgroundColorTop : kPageActionBackgroundColorTop;
30 }
31
GetBottomColor(InfoBarDelegate::Type infobar_type)32 SkColor InfoBarBackground::GetBottomColor(InfoBarDelegate::Type infobar_type) {
33 static const SkColor kWarningBackgroundColorBottom =
34 SkColorSetRGB(250, 230, 145);
35 static const SkColor kPageActionBackgroundColorBottom =
36 SkColorSetRGB(179, 202, 231);
37
38 return (infobar_type == InfoBarDelegate::WARNING_TYPE) ?
39 kWarningBackgroundColorBottom : kPageActionBackgroundColorBottom;
40 }
41
Paint(gfx::Canvas * canvas,views::View * view) const42 void InfoBarBackground::Paint(gfx::Canvas* canvas, views::View* view) const {
43 SkPoint gradient_points[2] = {
44 {SkIntToScalar(0), SkIntToScalar(0)},
45 {SkIntToScalar(0), SkIntToScalar(view->height())}
46 };
47 SkColor gradient_colors[2] = {
48 top_color_,
49 bottom_color_
50 };
51 SkShader* gradient_shader = SkGradientShader::CreateLinear(
52 gradient_points, gradient_colors, NULL, 2, SkShader::kClamp_TileMode);
53 SkPaint paint;
54 paint.setStrokeWidth(SkIntToScalar(InfoBar::kSeparatorLineHeight));
55 paint.setStyle(SkPaint::kFill_Style);
56 paint.setStrokeCap(SkPaint::kRound_Cap);
57 paint.setShader(gradient_shader);
58 gradient_shader->unref();
59
60 InfoBarView* infobar = static_cast<InfoBarView*>(view);
61 gfx::CanvasSkia* canvas_skia = canvas->AsCanvasSkia();
62 canvas_skia->drawPath(*infobar->fill_path(), paint);
63
64 paint.setShader(NULL);
65 paint.setColor(SkColorSetA(separator_color_,
66 SkColorGetA(gradient_colors[0])));
67 paint.setStyle(SkPaint::kStroke_Style);
68 // Anti-alias the path so it doesn't look goofy when the edges are not at 45
69 // degree angles, but don't anti-alias anything else, especially not the fill,
70 // lest we get weird color bleeding problems.
71 paint.setAntiAlias(true);
72 canvas_skia->drawPath(*infobar->stroke_path(), paint);
73 paint.setAntiAlias(false);
74
75 // Now draw the separator at the bottom.
76 canvas->FillRectInt(separator_color_, 0,
77 view->height() - InfoBar::kSeparatorLineHeight,
78 view->width(), InfoBar::kSeparatorLineHeight);
79 }
80