| 38 | } |
| 39 | |
| 40 | void RadialGradient::updateShader(const Rect& bounds) { |
| 41 | if (isEmpty()) { |
| 42 | _shader = nullptr; |
| 43 | } else { |
| 44 | auto width = bounds.width(); |
| 45 | auto height = bounds.height(); |
| 46 | auto radius = std::min(width / 2, height / 2); |
| 47 | |
| 48 | const auto* colorsData = reinterpret_cast<const SkColor*>(_colors.data()); |
| 49 | |
| 50 | SkMatrix localMatrix; |
| 51 | if (width != height) { |
| 52 | localMatrix.postScale(width / height, 1); |
| 53 | } |
| 54 | localMatrix.postTranslate(bounds.x() + (width / 2), bounds.y() + (height / 2)); |
| 55 | |
| 56 | sk_sp<SkShader> shader; |
| 57 | if (_colors.size() == _locations.size()) { |
| 58 | // User provided color distribution |
| 59 | |
| 60 | _shader = SkGradientShader::MakeRadial(Point::make(0, 0).getSkValue(), |
| 61 | radius, |
| 62 | colorsData, |
| 63 | _locations.data(), |
| 64 | static_cast<int>(_colors.size()), |
| 65 | SkTileMode::kClamp, |
| 66 | 0, |
| 67 | &localMatrix); |
| 68 | } else { |
| 69 | // Distribute colors evenly |
| 70 | _shader = SkGradientShader::MakeRadial(Point::make(0, 0).getSkValue(), |
| 71 | radius, |
| 72 | colorsData, |
| 73 | nullptr, |
| 74 | static_cast<int>(_colors.size()), |
| 75 | SkTileMode::kClamp, |
| 76 | 0, |
| 77 | &localMatrix); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void RadialGradient::applyToPaint(Paint& paint) const { |
| 83 | paint.getSkValue().setShader(_shader); |