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 #ifndef UI_GFX_SHADOW_VALUE_H_
6 #define UI_GFX_SHADOW_VALUE_H_
7 
8 #include <string>
9 #include <tuple>
10 #include <vector>
11 
12 #include "third_party/skia/include/core/SkColor.h"
13 #include "ui/gfx/geometry/vector2d.h"
14 #include "ui/gfx/gfx_export.h"
15 
16 namespace gfx {
17 
18 class Insets;
19 
20 class ShadowValue;
21 typedef std::vector<ShadowValue> ShadowValues;
22 
23 // ShadowValue encapsulates parameters needed to define a shadow, including the
24 // shadow's offset, blur amount and color.
25 class GFX_EXPORT ShadowValue {
26  public:
27   constexpr ShadowValue() = default;
ShadowValue(const gfx::Vector2d & offset,double blur,SkColor color)28   constexpr ShadowValue(const gfx::Vector2d& offset, double blur, SkColor color)
29       : offset_(offset), blur_(blur), color_(color) {}
30 
x()31   constexpr int x() const { return offset_.x(); }
y()32   constexpr int y() const { return offset_.y(); }
offset()33   constexpr const gfx::Vector2d& offset() const { return offset_; }
blur()34   constexpr double blur() const { return blur_; }
color()35   constexpr SkColor color() const { return color_; }
36 
37   constexpr bool operator==(const ShadowValue& other) const {
38     return offset_ == other.offset_ && blur_ == other.blur_ &&
39            color_ == other.color_;
40   }
41 
42   ShadowValue Scale(float scale) const;
43 
44   std::string ToString() const;
45 
46   // Gets margin space needed for shadows. Note that values in returned Insets
47   // are negative because shadow margins are outside a boundary.
48   static Insets GetMargin(const ShadowValues& shadows);
49 
50   // Gets the area inside a rectangle that would be affected by shadow blur.
51   // This is similar to the margin except it's positive (the blur region is
52   // inside a hypothetical rectangle) and it accounts for the blur both inside
53   // and outside the bounding box. The region inside the "blur region" would be
54   // a uniform color.
55   static Insets GetBlurRegion(const ShadowValues& shadows);
56 
57   // Makes ShadowValues matching MD or Refresh shadows for the given elevation
58   // and color.
59   static ShadowValues MakeRefreshShadowValues(int elevation, SkColor color);
60   static ShadowValues MakeMdShadowValues(int elevation,
61                                          SkColor color = SK_ColorBLACK);
62 
63  private:
64   gfx::Vector2d offset_;
65 
66   // Blur amount of the shadow in pixels. If underlying implementation supports
67   // (e.g. Skia), it can have fraction part such as 0.5 pixel. The value
68   // defines a range from full shadow color at the start point inside the
69   // shadow to fully transparent at the end point outside it. The range is
70   // perpendicular to and centered on the shadow edge. For example, a blur
71   // amount of 4.0 means to have a blurry shadow edge of 4 pixels that
72   // transitions from full shadow color to fully transparent and with 2 pixels
73   // inside the shadow and 2 pixels goes beyond the edge.
74   double blur_ = 0.;
75 
76   SkColor color_ = SK_ColorTRANSPARENT;
77 };
78 
79 }  // namespace gfx
80 
81 #endif  // UI_GFX_SHADOW_VALUE_H_
82