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