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 #include "modules/sksg/include/SkSGGradient.h"
9
10 #include "include/core/SkPaint.h"
11 #include "include/effects/SkGradientShader.h"
12
13 namespace sksg {
14
onRevalidateShader()15 sk_sp<SkShader> Gradient::onRevalidateShader() {
16 if (fColorStops.empty()) {
17 return nullptr;
18 }
19
20 std::vector<SkColor> colors;
21 std::vector<SkScalar> positions;
22 colors.reserve(fColorStops.size());
23 positions.reserve(fColorStops.size());
24
25 SkScalar position = 0;
26 for (const auto& stop : fColorStops) {
27 colors.push_back(stop.fColor);
28 position = SkTPin(stop.fPosition, position, 1.0f);
29 positions.push_back(position);
30 }
31
32 // TODO: detect even stop distributions, pass null for positions.
33 return this->onMakeShader(colors, positions);
34 }
35
onMakeShader(const std::vector<SkColor> & colors,const std::vector<SkScalar> & positions) const36 sk_sp<SkShader> LinearGradient::onMakeShader(const std::vector<SkColor>& colors,
37 const std::vector<SkScalar>& positions) const {
38 SkASSERT(colors.size() == positions.size());
39
40 const SkPoint pts[] = { fStartPoint, fEndPoint };
41 return SkGradientShader::MakeLinear(pts, colors.data(), positions.data(), colors.size(),
42 this->getTileMode());
43 }
44
onMakeShader(const std::vector<SkColor> & colors,const std::vector<SkScalar> & positions) const45 sk_sp<SkShader> RadialGradient::onMakeShader(const std::vector<SkColor>& colors,
46 const std::vector<SkScalar>& positions) const {
47 SkASSERT(colors.size() == positions.size());
48
49 return (fStartRadius <= 0 && fStartCenter == fEndCenter)
50 ? SkGradientShader::MakeRadial(fEndCenter, fEndRadius,
51 colors.data(), positions.data(), colors.size(),
52 this->getTileMode())
53 : SkGradientShader::MakeTwoPointConical(fStartCenter, fStartRadius,
54 fEndCenter, fEndRadius,
55 colors.data(), positions.data(), colors.size(),
56 this->getTileMode());
57 }
58
59 } //namespace sksg
60