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 | |
| 11 | #include "SkShader.h" |
| 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 |
| 16 | render linear and radial gradients. |
| 17 | */ |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 18 | class SK_API SkGradientShader { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | public: |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 20 | enum Flags { |
| 21 | /** By default gradients will interpolate their colors in unpremul space |
| 22 | * and then premultiply each of the results. By setting this flag, the |
| 23 | * gradients will premultiply their colors first, and then interpolate |
| 24 | * between them. |
| 25 | */ |
| 26 | kInterpolateColorsInPremul_Flag = 1 << 0, |
| 27 | }; |
| 28 | |
brianosman | a99b66d | 2016-09-27 03:33:12 -0700 | [diff] [blame] | 29 | /** 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] | 30 | <p /> |
brianosman | a99b66d | 2016-09-27 03:33:12 -0700 | [diff] [blame] | 31 | @param pts The start and end points for the gradient. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | @param colors The array[count] of colors, to be distributed between the two points |
| 33 | @param pos May be NULL. array[count] of SkScalars, or NULL, of the relative position of |
| 34 | each corresponding color in the colors array. If this is NULL, |
| 35 | the the colors are distributed evenly between the start and end point. |
| 36 | If this is not null, the values must begin with 0, end with 1.0, and |
| 37 | intermediate values must be strictly increasing. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 38 | @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] | 39 | @param mode The tiling mode |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | */ |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 41 | static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], |
| 42 | const SkColor colors[], const SkScalar pos[], int count, |
| 43 | SkShader::TileMode mode, |
| 44 | uint32_t flags, const SkMatrix* localMatrix); |
| 45 | static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], |
| 46 | const SkColor colors[], const SkScalar pos[], int count, |
| 47 | SkShader::TileMode mode) { |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 48 | return MakeLinear(pts, colors, pos, count, mode, 0, nullptr); |
commit-bot@chromium.org | 7b17152 | 2014-05-19 15:46:09 +0000 | [diff] [blame] | 49 | } |
| 50 | |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 51 | /** Returns a shader that generates a linear gradient between the two specified points. |
| 52 | <p /> |
| 53 | @param pts The start and end points for the gradient. |
| 54 | @param colors The array[count] of colors, to be distributed between the two points |
| 55 | @param pos May be NULL. array[count] of SkScalars, or NULL, of the relative position of |
| 56 | each corresponding color in the colors array. If this is NULL, |
| 57 | the the colors are distributed evenly between the start and end point. |
| 58 | If this is not null, the values must begin with 0, end with 1.0, and |
| 59 | intermediate values must be strictly increasing. |
| 60 | @param count Must be >=2. The number of colors (and pos if not NULL) entries. |
| 61 | @param mode The tiling mode |
| 62 | */ |
| 63 | static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], |
| 64 | const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace, |
| 65 | const SkScalar pos[], int count, SkShader::TileMode mode, |
| 66 | uint32_t flags, const SkMatrix* localMatrix); |
| 67 | static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], |
| 68 | const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace, |
| 69 | const SkScalar pos[], int count, SkShader::TileMode mode) { |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 70 | return MakeLinear(pts, colors, std::move(colorSpace), pos, count, mode, 0, nullptr); |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 71 | } |
| 72 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | /** Returns a shader that generates a radial gradient given the center and radius. |
| 74 | <p /> |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | @param center The center of the circle for this gradient |
| 76 | @param radius Must be positive. The radius of the circle for this gradient |
| 77 | @param colors The array[count] of colors, to be distributed between the center and edge of the circle |
| 78 | @param pos May be NULL. The array[count] of SkScalars, or NULL, of the relative position of |
| 79 | each corresponding color in the colors array. If this is NULL, |
| 80 | the the colors are distributed evenly between the center and edge of the circle. |
| 81 | If this is not null, the values must begin with 0, end with 1.0, and |
| 82 | intermediate values must be strictly increasing. |
| 83 | @param count Must be >= 2. The number of colors (and pos if not NULL) entries |
| 84 | @param mode The tiling mode |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | */ |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 86 | static sk_sp<SkShader> MakeRadial(const SkPoint& center, SkScalar radius, |
| 87 | const SkColor colors[], const SkScalar pos[], int count, |
| 88 | SkShader::TileMode mode, |
| 89 | uint32_t flags, const SkMatrix* localMatrix); |
| 90 | static sk_sp<SkShader> MakeRadial(const SkPoint& center, SkScalar radius, |
| 91 | const SkColor colors[], const SkScalar pos[], int count, |
| 92 | SkShader::TileMode mode) { |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 93 | return MakeRadial(center, radius, colors, pos, count, mode, 0, nullptr); |
commit-bot@chromium.org | 7b17152 | 2014-05-19 15:46:09 +0000 | [diff] [blame] | 94 | } |
| 95 | |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 96 | /** Returns a shader that generates a radial gradient given the center and radius. |
| 97 | <p /> |
| 98 | @param center The center of the circle for this gradient |
| 99 | @param radius Must be positive. The radius of the circle for this gradient |
| 100 | @param colors The array[count] of colors, to be distributed between the center and edge of the circle |
| 101 | @param pos May be NULL. The array[count] of SkScalars, or NULL, of the relative position of |
| 102 | each corresponding color in the colors array. If this is NULL, |
| 103 | the the colors are distributed evenly between the center and edge of the circle. |
| 104 | If this is not null, the values must begin with 0, end with 1.0, and |
| 105 | intermediate values must be strictly increasing. |
| 106 | @param count Must be >= 2. The number of colors (and pos if not NULL) entries |
| 107 | @param mode The tiling mode |
| 108 | */ |
| 109 | static sk_sp<SkShader> MakeRadial(const SkPoint& center, SkScalar radius, |
| 110 | const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace, |
| 111 | const SkScalar pos[], int count, SkShader::TileMode mode, |
| 112 | uint32_t flags, const SkMatrix* localMatrix); |
| 113 | static sk_sp<SkShader> MakeRadial(const SkPoint& center, SkScalar radius, |
| 114 | const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace, |
| 115 | const SkScalar pos[], int count, SkShader::TileMode mode) { |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 116 | return MakeRadial(center, radius, colors, std::move(colorSpace), pos, count, mode, |
| 117 | 0, nullptr); |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 118 | } |
| 119 | |
reed@google.com | cb7be69 | 2012-06-06 20:31:56 +0000 | [diff] [blame] | 120 | /** |
| 121 | * Returns a shader that generates a conical gradient given two circles, or |
| 122 | * returns NULL if the inputs are invalid. The gradient interprets the |
| 123 | * two circles according to the following HTML spec. |
| 124 | * http://dev.w3.org/html5/2dcontext/#dom-context-2d-createradialgradient |
| 125 | */ |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 126 | static sk_sp<SkShader> MakeTwoPointConical(const SkPoint& start, SkScalar startRadius, |
| 127 | const SkPoint& end, SkScalar endRadius, |
| 128 | const SkColor colors[], const SkScalar pos[], |
| 129 | int count, SkShader::TileMode mode, |
| 130 | uint32_t flags, const SkMatrix* localMatrix); |
| 131 | static sk_sp<SkShader> MakeTwoPointConical(const SkPoint& start, SkScalar startRadius, |
| 132 | const SkPoint& end, SkScalar endRadius, |
| 133 | const SkColor colors[], const SkScalar pos[], |
| 134 | int count, SkShader::TileMode mode) { |
| 135 | return MakeTwoPointConical(start, startRadius, end, endRadius, colors, pos, count, mode, |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 136 | 0, nullptr); |
commit-bot@chromium.org | 7b17152 | 2014-05-19 15:46:09 +0000 | [diff] [blame] | 137 | } |
| 138 | |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 139 | /** |
| 140 | * Returns a shader that generates a conical gradient given two circles, or |
| 141 | * returns NULL if the inputs are invalid. The gradient interprets the |
| 142 | * two circles according to the following HTML spec. |
| 143 | * http://dev.w3.org/html5/2dcontext/#dom-context-2d-createradialgradient |
| 144 | */ |
| 145 | static sk_sp<SkShader> MakeTwoPointConical(const SkPoint& start, SkScalar startRadius, |
| 146 | const SkPoint& end, SkScalar endRadius, |
| 147 | const SkColor4f colors[], |
| 148 | sk_sp<SkColorSpace> colorSpace, const SkScalar pos[], |
| 149 | int count, SkShader::TileMode mode, |
| 150 | uint32_t flags, const SkMatrix* localMatrix); |
| 151 | static sk_sp<SkShader> MakeTwoPointConical(const SkPoint& start, SkScalar startRadius, |
| 152 | const SkPoint& end, SkScalar endRadius, |
| 153 | const SkColor4f colors[], |
| 154 | sk_sp<SkColorSpace> colorSpace, const SkScalar pos[], |
| 155 | int count, SkShader::TileMode mode) { |
| 156 | return MakeTwoPointConical(start, startRadius, end, endRadius, colors, |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 157 | std::move(colorSpace), pos, count, mode, 0, nullptr); |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 158 | } |
| 159 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 160 | /** Returns a shader that generates a sweep gradient given a center. |
| 161 | <p /> |
Florin Malita | 5a9a981 | 2017-08-01 16:38:08 -0400 | [diff] [blame] | 162 | @param cx The X coordinate of the center of the sweep |
| 163 | @param cx The Y coordinate of the center of the sweep |
| 164 | @param colors The array[count] of colors, to be distributed around the center, within |
| 165 | the gradient angle range. |
| 166 | @param pos May be NULL. The array[count] of SkScalars, or NULL, of the relative |
| 167 | position of each corresponding color in the colors array. If this is |
| 168 | NULL, then the colors are distributed evenly within the angular range. |
| 169 | If this is not null, the values must begin with 0, end with 1.0, and |
| 170 | intermediate values must be strictly increasing. |
| 171 | @param count Must be >= 2. The number of colors (and pos if not NULL) entries |
| 172 | @param mode Tiling mode: controls drawing outside of the gradient angular range. |
| 173 | @param startAngle Start of the angular range, corresponding to pos == 0. |
| 174 | @param endAngle End of the angular range, corresponding to pos == 1. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 175 | */ |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 176 | static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy, |
| 177 | const SkColor colors[], const SkScalar pos[], int count, |
Florin Malita | 5a9a981 | 2017-08-01 16:38:08 -0400 | [diff] [blame] | 178 | SkShader::TileMode mode, |
| 179 | SkScalar startAngle, SkScalar endAngle, |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 180 | uint32_t flags, const SkMatrix* localMatrix); |
| 181 | static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy, |
Florin Malita | 5a9a981 | 2017-08-01 16:38:08 -0400 | [diff] [blame] | 182 | const SkColor colors[], const SkScalar pos[], int count, |
| 183 | uint32_t flags, const SkMatrix* localMatrix) { |
| 184 | return MakeSweep(cx, cy, colors, pos, count, SkShader::kClamp_TileMode, 0, 360, flags, |
| 185 | localMatrix); |
| 186 | } |
| 187 | static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy, |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 188 | const SkColor colors[], const SkScalar pos[], int count) { |
Florin Malita | 5a9a981 | 2017-08-01 16:38:08 -0400 | [diff] [blame] | 189 | return MakeSweep(cx, cy, colors, pos, count, 0, nullptr); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 190 | } |
| 191 | |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 192 | /** Returns a shader that generates a sweep gradient given a center. |
| 193 | <p /> |
Florin Malita | 5a9a981 | 2017-08-01 16:38:08 -0400 | [diff] [blame] | 194 | @param cx The X coordinate of the center of the sweep |
| 195 | @param cx The Y coordinate of the center of the sweep |
| 196 | @param colors The array[count] of colors, to be distributed around the center, within |
| 197 | the gradient angle range. |
| 198 | @param pos May be NULL. The array[count] of SkScalars, or NULL, of the relative |
| 199 | position of each corresponding color in the colors array. If this is |
| 200 | NULL, then the colors are distributed evenly within the angular range. |
| 201 | If this is not null, the values must begin with 0, end with 1.0, and |
| 202 | intermediate values must be strictly increasing. |
| 203 | @param count Must be >= 2. The number of colors (and pos if not NULL) entries |
| 204 | @param mode Tiling mode: controls drawing outside of the gradient angular range. |
| 205 | @param startAngle Start of the angular range, corresponding to pos == 0. |
| 206 | @param endAngle End of the angular range, corresponding to pos == 1. |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 207 | */ |
| 208 | static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy, |
| 209 | const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace, |
| 210 | const SkScalar pos[], int count, |
Florin Malita | 5a9a981 | 2017-08-01 16:38:08 -0400 | [diff] [blame] | 211 | SkShader::TileMode mode, |
| 212 | SkScalar startAngle, SkScalar endAngle, |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 213 | uint32_t flags, const SkMatrix* localMatrix); |
| 214 | static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy, |
| 215 | const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace, |
Florin Malita | 5a9a981 | 2017-08-01 16:38:08 -0400 | [diff] [blame] | 216 | const SkScalar pos[], int count, |
| 217 | uint32_t flags, const SkMatrix* localMatrix) { |
| 218 | return MakeSweep(cx, cy, colors, std::move(colorSpace), pos, count, |
| 219 | SkShader::kClamp_TileMode, 0, 360, flags, localMatrix); |
| 220 | } |
| 221 | static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy, |
| 222 | const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace, |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 223 | const SkScalar pos[], int count) { |
Florin Malita | 5a9a981 | 2017-08-01 16:38:08 -0400 | [diff] [blame] | 224 | return MakeSweep(cx, cy, colors, std::move(colorSpace), pos, count, 0, nullptr); |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Cary Clark | 4dc5a45 | 2018-05-21 11:56:57 -0400 | [diff] [blame] | 227 | static void InitializeFlattenables(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 228 | }; |
| 229 | |
| 230 | #endif |