Flutter Engine
The Flutter Engine
SkSGGradient.cpp
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
9
16
17namespace sksg {
18
20 if (fColorStops.empty()) {
21 return nullptr;
22 }
23
24 std::vector<SkColor4f> colors;
25 std::vector<SkScalar> positions;
26 colors.reserve(fColorStops.size());
27 positions.reserve(fColorStops.size());
28
29 SkScalar position = 0;
30 for (const auto& stop : fColorStops) {
31 colors.push_back(stop.fColor);
32 position = SkTPin(stop.fPosition, position, 1.0f);
33 positions.push_back(position);
34 }
35
36 // TODO: detect even stop distributions, pass null for positions.
37 return this->onMakeShader(colors, positions);
38}
39
41 const std::vector<SkScalar >& positions) const {
42 SkASSERT(colors.size() == positions.size());
43
44 const SkPoint pts[] = { fStartPoint, fEndPoint };
45 return SkGradientShader::MakeLinear(pts, colors.data(), nullptr, positions.data(),
46 SkToInt(colors.size()), this->getTileMode());
47}
48
50 const std::vector<SkScalar >& positions) const {
51 SkASSERT(colors.size() == positions.size());
52
53 return (fStartRadius <= 0 && fStartCenter == fEndCenter)
54 ? SkGradientShader::MakeRadial(fEndCenter, fEndRadius,
55 colors.data(), nullptr, positions.data(),
56 SkToInt(colors.size()), this->getTileMode())
57 : SkGradientShader::MakeTwoPointConical(fStartCenter, fStartRadius,
58 fEndCenter, fEndRadius,
59 colors.data(), nullptr, positions.data(),
60 SkToInt(colors.size()), this->getTileMode());
61}
62
63} //namespace sksg
#define SkASSERT(cond)
Definition: SkAssert.h:116
static constexpr const T & SkTPin(const T &x, const T &lo, const T &hi)
Definition: SkTPin.h:19
constexpr int SkToInt(S x)
Definition: SkTo.h:29
static sk_sp< SkShader > MakeTwoPointConical(const SkPoint &start, SkScalar startRadius, const SkPoint &end, SkScalar endRadius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
static sk_sp< SkShader > MakeRadial(const SkPoint &center, SkScalar radius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
virtual sk_sp< SkShader > onMakeShader(const std::vector< SkColor4f > &colors, const std::vector< SkScalar > &positions) const =0
sk_sp< SkShader > onRevalidateShader() final
sk_sp< SkShader > onMakeShader(const std::vector< SkColor4f > &, const std::vector< SkScalar > &) const override
sk_sp< SkShader > onMakeShader(const std::vector< SkColor4f > &, const std::vector< SkScalar > &) const override
float SkScalar
Definition: extension.cpp:12
PODArray< SkColor > colors
Definition: SkRecords.h:276
Definition: Skottie.h:32