| 45 | } |
| 46 | |
| 47 | void LinearGradient::updateShader(const Rect& bounds) { |
| 48 | if (isEmpty()) { |
| 49 | _shader = nullptr; |
| 50 | } else { |
| 51 | auto halfWidth = bounds.x() + (bounds.width() / 2); |
| 52 | auto halfHeight = bounds.y() + (bounds.height() / 2); |
| 53 | |
| 54 | Point points[2]; |
| 55 | |
| 56 | switch (_orientation) { |
| 57 | case LinearGradientOrientationTopBottom: |
| 58 | points[0] = Point::make(halfWidth, bounds.top); |
| 59 | points[1] = Point::make(halfWidth, bounds.bottom); |
| 60 | break; |
| 61 | case LinearGradientOrientationTopRightBottomLeft: |
| 62 | points[0] = Point::make(bounds.right, bounds.top); |
| 63 | points[1] = Point::make(bounds.left, bounds.bottom); |
| 64 | break; |
| 65 | case LinearGradientOrientationRightLeft: |
| 66 | points[0] = Point::make(bounds.right, halfHeight); |
| 67 | points[1] = Point::make(bounds.left, halfHeight); |
| 68 | break; |
| 69 | case LinearGradientOrientationBottomRightTopLeft: |
| 70 | points[0] = Point::make(bounds.right, bounds.bottom); |
| 71 | points[1] = Point::make(bounds.left, bounds.top); |
| 72 | break; |
| 73 | case LinearGradientOrientationBottomTop: |
| 74 | points[0] = Point::make(halfWidth, bounds.bottom); |
| 75 | points[1] = Point::make(halfWidth, bounds.top); |
| 76 | break; |
| 77 | case LinearGradientOrientationBottomLeftTopRight: |
| 78 | points[0] = Point::make(bounds.left, bounds.bottom); |
| 79 | points[1] = Point::make(bounds.right, bounds.top); |
| 80 | break; |
| 81 | case LinearGradientOrientationLeftRight: |
| 82 | points[0] = Point::make(bounds.left, halfHeight); |
| 83 | points[1] = Point::make(bounds.right, halfHeight); |
| 84 | break; |
| 85 | case LinearGradientOrientationTopLeftBottomRight: |
| 86 | points[0] = Point::make(bounds.left, bounds.top); |
| 87 | points[1] = Point::make(bounds.right, bounds.bottom); |
| 88 | break; |
| 89 | default: |
| 90 | points[0] = Point::make(halfWidth, bounds.top); |
| 91 | points[1] = Point::make(halfWidth, bounds.bottom); |
| 92 | } |
| 93 | |
| 94 | const auto* colorsData = reinterpret_cast<const SkColor*>(_colors.data()); |
| 95 | |
| 96 | sk_sp<SkShader> shader; |
| 97 | if (_colors.size() == _locations.size()) { |
| 98 | // User provided color distribution |
| 99 | |
| 100 | _shader = SkGradientShader::MakeLinear(&points[0].getSkValue(), |
| 101 | colorsData, |
| 102 | _locations.data(), |
| 103 | static_cast<int>(_colors.size()), |
| 104 | SkTileMode::kClamp); |