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#include "include/private/SkTPin.h" 13 14namespace sksg { 15 16sk_sp<SkShader> Gradient::onRevalidateShader() { 17 if (fColorStops.empty()) { 18 return nullptr; 19 } 20 21 std::vector<SkColor4f> 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 return this->onMakeShader(colors, positions); 35} 36 37sk_sp<SkShader> LinearGradient::onMakeShader(const std::vector<SkColor4f>& 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(), nullptr, positions.data(), 43 SkToInt(colors.size()), this->getTileMode()); 44} 45 46sk_sp<SkShader> RadialGradient::onMakeShader(const std::vector<SkColor4f>& 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(), nullptr, positions.data(), 53 SkToInt(colors.size()), this->getTileMode()) 54 : SkGradientShader::MakeTwoPointConical(fStartCenter, fStartRadius, 55 fEndCenter, fEndRadius, 56 colors.data(), nullptr, positions.data(), 57 SkToInt(colors.size()), this->getTileMode()); 58} 59 60} //namespace sksg 61