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