Skia
2D Graphics Library
SkSGGradient.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkSGGradient_DEFINED
9 #define SkSGGradient_DEFINED
10 
12 
13 #include "include/core/SkColor.h"
14 #include "include/core/SkPoint.h"
15 #include "include/core/SkScalar.h"
16 #include "include/core/SkShader.h"
17 
18 #include <vector>
19 
20 namespace sksg {
21 
25 class Gradient : public Shader {
26 public:
27  struct ColorStop {
30 
31  bool operator==(const ColorStop& other) const {
32  return fPosition == other.fPosition && fColor == other.fColor;
33  }
34  };
35 
36  SG_ATTRIBUTE(ColorStops, std::vector<ColorStop>, fColorStops)
37  SG_ATTRIBUTE(TileMode , SkTileMode , fTileMode )
38 
39 protected:
41 
42  virtual sk_sp<SkShader> onMakeShader(const std::vector<SkColor4f>& colors,
43  const std::vector<SkScalar >& positions) const = 0;
44 
45 protected:
46  Gradient() = default;
47 
48 private:
49  std::vector<ColorStop> fColorStops;
50  SkTileMode fTileMode = SkTileMode::kClamp;
51 
52  using INHERITED = Shader;
53 };
54 
55 class LinearGradient final : public Gradient {
56 public:
59  }
60 
61  SG_ATTRIBUTE(StartPoint, SkPoint, fStartPoint)
62  SG_ATTRIBUTE(EndPoint , SkPoint, fEndPoint )
63 
64 protected:
65  sk_sp<SkShader> onMakeShader(const std::vector<SkColor4f>&,
66  const std::vector<SkScalar >&) const override;
67 
68 private:
69  LinearGradient() = default;
70 
71  SkPoint fStartPoint = SkPoint::Make(0, 0),
72  fEndPoint = SkPoint::Make(0, 0);
73 
74  using INHERITED = Gradient;
75 };
76 
77 class RadialGradient final : public Gradient {
78 public:
81  }
82 
83  SG_ATTRIBUTE(StartCenter, SkPoint , fStartCenter)
84  SG_ATTRIBUTE(EndCenter , SkPoint , fEndCenter )
85  SG_ATTRIBUTE(StartRadius, SkScalar, fStartRadius)
86  SG_ATTRIBUTE(EndRadius , SkScalar, fEndRadius )
87 
88 protected:
89  sk_sp<SkShader> onMakeShader(const std::vector<SkColor4f>&,
90  const std::vector<SkScalar >&) const override;
91 
92 private:
93  RadialGradient() = default;
94 
95  SkPoint fStartCenter = SkPoint::Make(0, 0),
96  fEndCenter = SkPoint::Make(0, 0);
97  SkScalar fStartRadius = 0,
98  fEndRadius = 0;
99 
100  using INHERITED = Gradient;
101 };
102 
103 } // namespace sksg
104 
105 #endif // SkSGGradient_DEFINED
Types, consts, functions, and macros for colors.
#define SG_ATTRIBUTE(attr_name, attr_type, attr_container)
Definition: SkSGNode.h:99
float SkScalar
Definition: SkScalar.h:14
SkTileMode
Definition: SkTileMode.h:13
Shaders specify the source color(s) for what is being drawn.
Definition: SkShader.h:35
Gradient base class.
Definition: SkSGGradient.h:25
sk_sp< SkShader > onRevalidateShader() final
Gradient()=default
virtual sk_sp< SkShader > onMakeShader(const std::vector< SkColor4f > &colors, const std::vector< SkScalar > &positions) const =0
Definition: SkSGGradient.h:55
sk_sp< SkShader > onMakeShader(const std::vector< SkColor4f > &, const std::vector< SkScalar > &) const override
static sk_sp< LinearGradient > Make()
Definition: SkSGGradient.h:57
Definition: SkSGGradient.h:77
sk_sp< SkShader > onMakeShader(const std::vector< SkColor4f > &, const std::vector< SkScalar > &) const override
static sk_sp< RadialGradient > Make()
Definition: SkSGGradient.h:79
Shader base class.
Definition: SkSGRenderEffect.h:33
sk_sp< SkDrawLooper > SK_API Make(SkColor4f color, SkColorSpace *cs, SkScalar sigma, SkScalar dx, SkScalar dy)
Definition: Skottie.h:30
RGBA color value, holding four floating point components.
Definition: SkSGGradient.h:27
SkColor4f fColor
Definition: SkSGGradient.h:29
bool operator==(const ColorStop &other) const
Definition: SkSGGradient.h:31
SkScalar fPosition
Definition: SkSGGradient.h:28