reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef SkGradientShader_DEFINED |
| 9 | #define SkGradientShader_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkShader.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | /** \class SkGradientShader |
| 14 | |
| 15 | SkGradientShader hosts factories for creating subclasses of SkShader that |
Mike Klein | 024072a | 2018-11-11 00:26:30 +0000 | [diff] [blame] | 16 | render linear and radial gradients. In general, degenerate cases should not |
| 17 | produce surprising results, but there are several types of degeneracies: |
| 18 | |
| 19 | * A linear gradient made from the same two points. |
| 20 | * A radial gradient with a radius of zero. |
| 21 | * A sweep gradient where the start and end angle are the same. |
| 22 | * A two point conical gradient where the two centers and the two radii are |
| 23 | the same. |
| 24 | |
| 25 | For any degenerate gradient with a decal tile mode, it will draw empty since the interpolating |
| 26 | region is zero area and the outer region is discarded by the decal mode. |
| 27 | |
| 28 | For any degenerate gradient with a repeat or mirror tile mode, it will draw a solid color that |
| 29 | is the average gradient color, since infinitely many repetitions of the gradients will fill the |
| 30 | shape. |
| 31 | |
| 32 | For a clamped gradient, every type is well-defined at the limit except for linear gradients. The |
| 33 | radial gradient with zero radius becomes the last color. The sweep gradient draws the sector |
| 34 | from 0 to the provided angle with the first color, with a hardstop switching to the last color. |
| 35 | When the provided angle is 0, this is just the solid last color again. Similarly, the two point |
| 36 | conical gradient becomes a circle filled with the first color, sized to the provided radius, |
| 37 | with a hardstop switching to the last color. When the two radii are both zero, this is just the |
| 38 | solid last color. |
| 39 | |
| 40 | As a linear gradient approaches the degenerate case, its shader will approach the appearance of |
| 41 | two half planes, each filled by the first and last colors of the gradient. The planes will be |
| 42 | oriented perpendicular to the vector between the two defining points of the gradient. However, |
| 43 | once they become the same point, Skia cannot reconstruct what that expected orientation is. To |
| 44 | provide a stable and predictable color in this case, Skia just uses the last color as a solid |
| 45 | fill to be similar to many of the other degenerate gradients' behaviors in clamp mode. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | */ |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 47 | class SK_API SkGradientShader { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | public: |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 49 | enum Flags { |
| 50 | /** By default gradients will interpolate their colors in unpremul space |
| 51 | * and then premultiply each of the results. By setting this flag, the |
| 52 | * gradients will premultiply their colors first, and then interpolate |
| 53 | * between them. |
| 54 | */ |
| 55 | kInterpolateColorsInPremul_Flag = 1 << 0, |
| 56 | }; |
| 57 | |
brianosman | a99b66d | 2016-09-27 03:33:12 -0700 | [diff] [blame] | 58 | /** Returns a shader that generates a linear gradient between the two specified points. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | <p /> |
brianosman | a99b66d | 2016-09-27 03:33:12 -0700 | [diff] [blame] | 60 | @param pts The start and end points for the gradient. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 61 | @param colors The array[count] of colors, to be distributed between the two points |
| 62 | @param pos May be NULL. array[count] of SkScalars, or NULL, of the relative position of |
| 63 | each corresponding color in the colors array. If this is NULL, |
| 64 | the the colors are distributed evenly between the start and end point. |
| 65 | If this is not null, the values must begin with 0, end with 1.0, and |
| 66 | intermediate values must be strictly increasing. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 67 | @param count Must be >=2. The number of colors (and pos if not NULL) entries. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | @param mode The tiling mode |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | */ |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 70 | static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], |
| 71 | const SkColor colors[], const SkScalar pos[], int count, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 72 | SkTileMode mode, |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 73 | uint32_t flags, const SkMatrix* localMatrix); |
| 74 | static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], |
| 75 | const SkColor colors[], const SkScalar pos[], int count, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 76 | SkTileMode mode) { |
| 77 | return MakeLinear(pts, colors, pos, count, mode, 0, nullptr); |
| 78 | } |
commit-bot@chromium.org | 7b17152 | 2014-05-19 15:46:09 +0000 | [diff] [blame] | 79 | |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 80 | /** Returns a shader that generates a linear gradient between the two specified points. |
| 81 | <p /> |
| 82 | @param pts The start and end points for the gradient. |
| 83 | @param colors The array[count] of colors, to be distributed between the two points |
| 84 | @param pos May be NULL. array[count] of SkScalars, or NULL, of the relative position of |
| 85 | each corresponding color in the colors array. If this is NULL, |
| 86 | the the colors are distributed evenly between the start and end point. |
| 87 | If this is not null, the values must begin with 0, end with 1.0, and |
| 88 | intermediate values must be strictly increasing. |
| 89 | @param count Must be >=2. The number of colors (and pos if not NULL) entries. |
| 90 | @param mode The tiling mode |
| 91 | */ |
| 92 | static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], |
| 93 | const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 94 | const SkScalar pos[], int count, SkTileMode mode, |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 95 | uint32_t flags, const SkMatrix* localMatrix); |
| 96 | static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], |
| 97 | const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 98 | const SkScalar pos[], int count, SkTileMode mode) { |
| 99 | return MakeLinear(pts, colors, std::move(colorSpace), pos, count, mode, 0, nullptr); |
| 100 | } |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 101 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | /** Returns a shader that generates a radial gradient given the center and radius. |
| 103 | <p /> |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | @param center The center of the circle for this gradient |
| 105 | @param radius Must be positive. The radius of the circle for this gradient |
| 106 | @param colors The array[count] of colors, to be distributed between the center and edge of the circle |
| 107 | @param pos May be NULL. The array[count] of SkScalars, or NULL, of the relative position of |
| 108 | each corresponding color in the colors array. If this is NULL, |
| 109 | the the colors are distributed evenly between the center and edge of the circle. |
| 110 | If this is not null, the values must begin with 0, end with 1.0, and |
| 111 | intermediate values must be strictly increasing. |
| 112 | @param count Must be >= 2. The number of colors (and pos if not NULL) entries |
| 113 | @param mode The tiling mode |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 114 | */ |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 115 | static sk_sp<SkShader> MakeRadial(const SkPoint& center, SkScalar radius, |
| 116 | const SkColor colors[], const SkScalar pos[], int count, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 117 | SkTileMode mode, |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 118 | uint32_t flags, const SkMatrix* localMatrix); |
| 119 | static sk_sp<SkShader> MakeRadial(const SkPoint& center, SkScalar radius, |
| 120 | const SkColor colors[], const SkScalar pos[], int count, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 121 | SkTileMode mode) { |
| 122 | return MakeRadial(center, radius, colors, pos, count, mode, 0, nullptr); |
| 123 | } |
commit-bot@chromium.org | 7b17152 | 2014-05-19 15:46:09 +0000 | [diff] [blame] | 124 | |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 125 | /** Returns a shader that generates a radial gradient given the center and radius. |
| 126 | <p /> |
| 127 | @param center The center of the circle for this gradient |
| 128 | @param radius Must be positive. The radius of the circle for this gradient |
| 129 | @param colors The array[count] of colors, to be distributed between the center and edge of the circle |
| 130 | @param pos May be NULL. The array[count] of SkScalars, or NULL, of the relative position of |
| 131 | each corresponding color in the colors array. If this is NULL, |
| 132 | the the colors are distributed evenly between the center and edge of the circle. |
| 133 | If this is not null, the values must begin with 0, end with 1.0, and |
| 134 | intermediate values must be strictly increasing. |
| 135 | @param count Must be >= 2. The number of colors (and pos if not NULL) entries |
| 136 | @param mode The tiling mode |
| 137 | */ |
| 138 | static sk_sp<SkShader> MakeRadial(const SkPoint& center, SkScalar radius, |
| 139 | const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 140 | const SkScalar pos[], int count, SkTileMode mode, |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 141 | uint32_t flags, const SkMatrix* localMatrix); |
| 142 | static sk_sp<SkShader> MakeRadial(const SkPoint& center, SkScalar radius, |
| 143 | const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 144 | const SkScalar pos[], int count, SkTileMode mode) { |
| 145 | return MakeRadial(center, radius, colors, std::move(colorSpace), pos, count, mode, |
| 146 | 0, nullptr); |
| 147 | } |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 148 | |
reed@google.com | cb7be69 | 2012-06-06 20:31:56 +0000 | [diff] [blame] | 149 | /** |
| 150 | * Returns a shader that generates a conical gradient given two circles, or |
| 151 | * returns NULL if the inputs are invalid. The gradient interprets the |
| 152 | * two circles according to the following HTML spec. |
| 153 | * http://dev.w3.org/html5/2dcontext/#dom-context-2d-createradialgradient |
| 154 | */ |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 155 | static sk_sp<SkShader> MakeTwoPointConical(const SkPoint& start, SkScalar startRadius, |
| 156 | const SkPoint& end, SkScalar endRadius, |
| 157 | const SkColor colors[], const SkScalar pos[], |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 158 | int count, SkTileMode mode, |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 159 | uint32_t flags, const SkMatrix* localMatrix); |
| 160 | static sk_sp<SkShader> MakeTwoPointConical(const SkPoint& start, SkScalar startRadius, |
| 161 | const SkPoint& end, SkScalar endRadius, |
| 162 | const SkColor colors[], const SkScalar pos[], |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 163 | int count, SkTileMode mode) { |
| 164 | return MakeTwoPointConical(start, startRadius, end, endRadius, colors, pos, count, mode, |
| 165 | 0, nullptr); |
| 166 | } |
commit-bot@chromium.org | 7b17152 | 2014-05-19 15:46:09 +0000 | [diff] [blame] | 167 | |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 168 | /** |
| 169 | * Returns a shader that generates a conical gradient given two circles, or |
| 170 | * returns NULL if the inputs are invalid. The gradient interprets the |
| 171 | * two circles according to the following HTML spec. |
| 172 | * http://dev.w3.org/html5/2dcontext/#dom-context-2d-createradialgradient |
| 173 | */ |
| 174 | static sk_sp<SkShader> MakeTwoPointConical(const SkPoint& start, SkScalar startRadius, |
| 175 | const SkPoint& end, SkScalar endRadius, |
| 176 | const SkColor4f colors[], |
| 177 | sk_sp<SkColorSpace> colorSpace, const SkScalar pos[], |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 178 | int count, SkTileMode mode, |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 179 | uint32_t flags, const SkMatrix* localMatrix); |
| 180 | static sk_sp<SkShader> MakeTwoPointConical(const SkPoint& start, SkScalar startRadius, |
| 181 | const SkPoint& end, SkScalar endRadius, |
| 182 | const SkColor4f colors[], |
| 183 | sk_sp<SkColorSpace> colorSpace, const SkScalar pos[], |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 184 | int count, SkTileMode mode) { |
| 185 | return MakeTwoPointConical(start, startRadius, end, endRadius, colors, |
| 186 | std::move(colorSpace), pos, count, mode, 0, nullptr); |
| 187 | } |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 188 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 189 | /** Returns a shader that generates a sweep gradient given a center. |
| 190 | <p /> |
Florin Malita | 5a9a981 | 2017-08-01 16:38:08 -0400 | [diff] [blame] | 191 | @param cx The X coordinate of the center of the sweep |
| 192 | @param cx The Y coordinate of the center of the sweep |
| 193 | @param colors The array[count] of colors, to be distributed around the center, within |
| 194 | the gradient angle range. |
| 195 | @param pos May be NULL. The array[count] of SkScalars, or NULL, of the relative |
| 196 | position of each corresponding color in the colors array. If this is |
| 197 | NULL, then the colors are distributed evenly within the angular range. |
| 198 | If this is not null, the values must begin with 0, end with 1.0, and |
| 199 | intermediate values must be strictly increasing. |
| 200 | @param count Must be >= 2. The number of colors (and pos if not NULL) entries |
| 201 | @param mode Tiling mode: controls drawing outside of the gradient angular range. |
| 202 | @param startAngle Start of the angular range, corresponding to pos == 0. |
| 203 | @param endAngle End of the angular range, corresponding to pos == 1. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 204 | */ |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 205 | static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy, |
| 206 | const SkColor colors[], const SkScalar pos[], int count, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 207 | SkTileMode mode, |
Florin Malita | 5a9a981 | 2017-08-01 16:38:08 -0400 | [diff] [blame] | 208 | SkScalar startAngle, SkScalar endAngle, |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 209 | uint32_t flags, const SkMatrix* localMatrix); |
| 210 | static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy, |
Florin Malita | 5a9a981 | 2017-08-01 16:38:08 -0400 | [diff] [blame] | 211 | const SkColor colors[], const SkScalar pos[], int count, |
| 212 | uint32_t flags, const SkMatrix* localMatrix) { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 213 | return MakeSweep(cx, cy, colors, pos, count, SkTileMode::kClamp, 0, 360, flags, |
Florin Malita | 5a9a981 | 2017-08-01 16:38:08 -0400 | [diff] [blame] | 214 | localMatrix); |
| 215 | } |
| 216 | static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy, |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 217 | const SkColor colors[], const SkScalar pos[], int count) { |
Florin Malita | 5a9a981 | 2017-08-01 16:38:08 -0400 | [diff] [blame] | 218 | return MakeSweep(cx, cy, colors, pos, count, 0, nullptr); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 219 | } |
| 220 | |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 221 | /** Returns a shader that generates a sweep gradient given a center. |
| 222 | <p /> |
Florin Malita | 5a9a981 | 2017-08-01 16:38:08 -0400 | [diff] [blame] | 223 | @param cx The X coordinate of the center of the sweep |
| 224 | @param cx The Y coordinate of the center of the sweep |
| 225 | @param colors The array[count] of colors, to be distributed around the center, within |
| 226 | the gradient angle range. |
| 227 | @param pos May be NULL. The array[count] of SkScalars, or NULL, of the relative |
| 228 | position of each corresponding color in the colors array. If this is |
| 229 | NULL, then the colors are distributed evenly within the angular range. |
| 230 | If this is not null, the values must begin with 0, end with 1.0, and |
| 231 | intermediate values must be strictly increasing. |
| 232 | @param count Must be >= 2. The number of colors (and pos if not NULL) entries |
| 233 | @param mode Tiling mode: controls drawing outside of the gradient angular range. |
| 234 | @param startAngle Start of the angular range, corresponding to pos == 0. |
| 235 | @param endAngle End of the angular range, corresponding to pos == 1. |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 236 | */ |
| 237 | static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy, |
| 238 | const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace, |
| 239 | const SkScalar pos[], int count, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 240 | SkTileMode mode, |
Florin Malita | 5a9a981 | 2017-08-01 16:38:08 -0400 | [diff] [blame] | 241 | SkScalar startAngle, SkScalar endAngle, |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 242 | uint32_t flags, const SkMatrix* localMatrix); |
| 243 | static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy, |
| 244 | const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace, |
Florin Malita | 5a9a981 | 2017-08-01 16:38:08 -0400 | [diff] [blame] | 245 | const SkScalar pos[], int count, |
| 246 | uint32_t flags, const SkMatrix* localMatrix) { |
| 247 | return MakeSweep(cx, cy, colors, std::move(colorSpace), pos, count, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 248 | SkTileMode::kClamp, 0, 360, flags, localMatrix); |
Florin Malita | 5a9a981 | 2017-08-01 16:38:08 -0400 | [diff] [blame] | 249 | } |
| 250 | static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy, |
| 251 | const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace, |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 252 | const SkScalar pos[], int count) { |
Florin Malita | 5a9a981 | 2017-08-01 16:38:08 -0400 | [diff] [blame] | 253 | return MakeSweep(cx, cy, colors, std::move(colorSpace), pos, count, 0, nullptr); |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Mike Klein | fa5f6ce | 2018-10-20 08:21:31 -0400 | [diff] [blame] | 256 | static void RegisterFlattenables(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 257 | }; |
| 258 | |
| 259 | #endif |