Debian Codegraph @

modules/sksg/src/SkSGGradient.cpp

raw
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/SkShader.h"
12 #include "include/effects/SkGradient.h"
13 #include "include/private/base/SkAssert.h"
14 #include "include/private/base/SkTPin.h"
15 #include "include/private/base/SkTo.h"
16
17 namespace sksg {
18
19 sk_sp<SkShader> Gradient::onRevalidateShader() {
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
40 sk_sp<SkShader> LinearGradient::onMakeShader(const std::vector<SkColor4f>& colors,
41 const std::vector<SkScalar >& positions) const {
42 SkASSERT(colors.size() == positions.size());
43
44 const SkPoint pts[] = { fStartPoint, fEndPoint };
45 return SkShaders::LinearGradient(pts, {{colors, positions, this->getTileMode()}, {}});
46 }
47
48 sk_sp<SkShader> RadialGradient::onMakeShader(const std::vector<SkColor4f>& colors,
49 const std::vector<SkScalar >& positions) const {
50 SkASSERT(colors.size() == positions.size());
51 SkGradient grad = {{colors, positions, this->getTileMode()}, {}};
52
53 return (fStartRadius <= 0 && fStartCenter == fEndCenter)
54 ? SkShaders::RadialGradient(fEndCenter, fEndRadius, grad)
55 : SkShaders::TwoPointConicalGradient(fStartCenter, fStartRadius, fEndCenter, fEndRadius,
56 grad);
57 }
58
59 } //namespace sksg