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) { |
| 48 | return MakeLinear(pts, colors, pos, count, mode, 0, NULL); |
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) { |
| 70 | return MakeLinear(pts, colors, std::move(colorSpace), pos, count, mode, 0, NULL); |
| 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) { |
| 93 | return MakeRadial(center, radius, colors, pos, count, mode, 0, NULL); |
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) { |
| 116 | return MakeRadial(center, radius, colors, std::move(colorSpace), pos, count, mode, 0, NULL); |
| 117 | } |
| 118 | |
reed@google.com | cb7be69 | 2012-06-06 20:31:56 +0000 | [diff] [blame] | 119 | /** |
| 120 | * Returns a shader that generates a conical gradient given two circles, or |
| 121 | * returns NULL if the inputs are invalid. The gradient interprets the |
| 122 | * two circles according to the following HTML spec. |
| 123 | * http://dev.w3.org/html5/2dcontext/#dom-context-2d-createradialgradient |
| 124 | */ |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 125 | static sk_sp<SkShader> MakeTwoPointConical(const SkPoint& start, SkScalar startRadius, |
| 126 | const SkPoint& end, SkScalar endRadius, |
| 127 | const SkColor colors[], const SkScalar pos[], |
| 128 | int count, SkShader::TileMode mode, |
| 129 | uint32_t flags, const SkMatrix* localMatrix); |
| 130 | static sk_sp<SkShader> MakeTwoPointConical(const SkPoint& start, SkScalar startRadius, |
| 131 | const SkPoint& end, SkScalar endRadius, |
| 132 | const SkColor colors[], const SkScalar pos[], |
| 133 | int count, SkShader::TileMode mode) { |
| 134 | return MakeTwoPointConical(start, startRadius, end, endRadius, colors, pos, count, mode, |
| 135 | 0, NULL); |
commit-bot@chromium.org | 7b17152 | 2014-05-19 15:46:09 +0000 | [diff] [blame] | 136 | } |
| 137 | |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 138 | /** |
| 139 | * Returns a shader that generates a conical gradient given two circles, or |
| 140 | * returns NULL if the inputs are invalid. The gradient interprets the |
| 141 | * two circles according to the following HTML spec. |
| 142 | * http://dev.w3.org/html5/2dcontext/#dom-context-2d-createradialgradient |
| 143 | */ |
| 144 | static sk_sp<SkShader> MakeTwoPointConical(const SkPoint& start, SkScalar startRadius, |
| 145 | const SkPoint& end, SkScalar endRadius, |
| 146 | const SkColor4f colors[], |
| 147 | sk_sp<SkColorSpace> colorSpace, const SkScalar pos[], |
| 148 | int count, SkShader::TileMode mode, |
| 149 | uint32_t flags, const SkMatrix* localMatrix); |
| 150 | static sk_sp<SkShader> MakeTwoPointConical(const SkPoint& start, SkScalar startRadius, |
| 151 | const SkPoint& end, SkScalar endRadius, |
| 152 | const SkColor4f colors[], |
| 153 | sk_sp<SkColorSpace> colorSpace, const SkScalar pos[], |
| 154 | int count, SkShader::TileMode mode) { |
| 155 | return MakeTwoPointConical(start, startRadius, end, endRadius, colors, |
| 156 | std::move(colorSpace), pos, count, mode, 0, NULL); |
| 157 | } |
| 158 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 159 | /** Returns a shader that generates a sweep gradient given a center. |
| 160 | <p /> |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 161 | @param cx The X coordinate of the center of the sweep |
| 162 | @param cx The Y coordinate of the center of the sweep |
| 163 | @param colors The array[count] of colors, to be distributed around the center. |
| 164 | @param pos May be NULL. The array[count] of SkScalars, or NULL, of the relative position of |
| 165 | each corresponding color in the colors array. If this is NULL, |
| 166 | the the colors are distributed evenly between the center and edge of the circle. |
| 167 | If this is not null, the values must begin with 0, end with 1.0, and |
| 168 | intermediate values must be strictly increasing. |
| 169 | @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] | 170 | */ |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 171 | static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy, |
| 172 | const SkColor colors[], const SkScalar pos[], int count, |
| 173 | uint32_t flags, const SkMatrix* localMatrix); |
| 174 | static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy, |
| 175 | const SkColor colors[], const SkScalar pos[], int count) { |
| 176 | return MakeSweep(cx, cy, colors, pos, count, 0, NULL); |
| 177 | } |
| 178 | |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 179 | /** Returns a shader that generates a sweep gradient given a center. |
| 180 | <p /> |
| 181 | @param cx The X coordinate of the center of the sweep |
| 182 | @param cx The Y coordinate of the center of the sweep |
| 183 | @param colors The array[count] of colors, to be distributed around the center. |
| 184 | @param pos May be NULL. The array[count] of SkScalars, or NULL, of the relative position of |
| 185 | each corresponding color in the colors array. If this is NULL, |
| 186 | the the colors are distributed evenly between the center and edge of the circle. |
| 187 | If this is not null, the values must begin with 0, end with 1.0, and |
| 188 | intermediate values must be strictly increasing. |
| 189 | @param count Must be >= 2. The number of colors (and pos if not NULL) entries |
| 190 | */ |
| 191 | static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy, |
| 192 | const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace, |
| 193 | const SkScalar pos[], int count, |
| 194 | uint32_t flags, const SkMatrix* localMatrix); |
| 195 | static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy, |
| 196 | const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace, |
| 197 | const SkScalar pos[], int count) { |
| 198 | return MakeSweep(cx, cy, colors, std::move(colorSpace), pos, count, 0, NULL); |
| 199 | } |
| 200 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 201 | #ifdef SK_SUPPORT_LEGACY_CREATESHADER_PTR |
| 202 | static SkShader* CreateLinear(const SkPoint pts[2], |
| 203 | const SkColor colors[], const SkScalar pos[], int count, |
| 204 | SkShader::TileMode mode, |
| 205 | uint32_t flags, const SkMatrix* localMatrix) { |
| 206 | return MakeLinear(pts, colors, pos, count, mode, flags, localMatrix).release(); |
| 207 | } |
| 208 | static SkShader* CreateLinear(const SkPoint pts[2], |
| 209 | const SkColor colors[], const SkScalar pos[], int count, |
| 210 | SkShader::TileMode mode) { |
| 211 | return CreateLinear(pts, colors, pos, count, mode, 0, NULL); |
| 212 | } |
| 213 | |
| 214 | static SkShader* CreateRadial(const SkPoint& center, SkScalar radius, |
| 215 | const SkColor colors[], const SkScalar pos[], int count, |
| 216 | SkShader::TileMode mode, |
| 217 | uint32_t flags, const SkMatrix* localMatrix) { |
| 218 | return MakeRadial(center, radius, colors, pos, count, mode, flags, localMatrix).release(); |
| 219 | } |
| 220 | |
| 221 | static SkShader* CreateRadial(const SkPoint& center, SkScalar radius, |
| 222 | const SkColor colors[], const SkScalar pos[], int count, |
| 223 | SkShader::TileMode mode) { |
| 224 | return CreateRadial(center, radius, colors, pos, count, mode, 0, NULL); |
| 225 | } |
| 226 | |
| 227 | static SkShader* CreateTwoPointConical(const SkPoint& start, SkScalar startRadius, |
| 228 | const SkPoint& end, SkScalar endRadius, |
| 229 | const SkColor colors[], const SkScalar pos[], int count, |
| 230 | SkShader::TileMode mode, |
| 231 | uint32_t flags, const SkMatrix* localMatrix) { |
| 232 | return MakeTwoPointConical(start, startRadius, end, endRadius, colors, pos, count, mode, |
| 233 | flags, localMatrix).release(); |
| 234 | } |
| 235 | static SkShader* CreateTwoPointConical(const SkPoint& start, SkScalar startRadius, |
| 236 | const SkPoint& end, SkScalar endRadius, |
| 237 | const SkColor colors[], const SkScalar pos[], int count, |
| 238 | SkShader::TileMode mode) { |
| 239 | return CreateTwoPointConical(start, startRadius, end, endRadius, colors, pos, count, mode, |
| 240 | 0, NULL); |
| 241 | } |
| 242 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 243 | static SkShader* CreateSweep(SkScalar cx, SkScalar cy, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 244 | const SkColor colors[], const SkScalar pos[], int count, |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 245 | uint32_t flags, const SkMatrix* localMatrix) { |
| 246 | return MakeSweep(cx, cy, colors, pos, count, flags, localMatrix).release(); |
| 247 | } |
commit-bot@chromium.org | 7b17152 | 2014-05-19 15:46:09 +0000 | [diff] [blame] | 248 | static SkShader* CreateSweep(SkScalar cx, SkScalar cy, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 249 | const SkColor colors[], const SkScalar pos[], int count) { |
| 250 | return CreateSweep(cx, cy, colors, pos, count, 0, NULL); |
commit-bot@chromium.org | 7b17152 | 2014-05-19 15:46:09 +0000 | [diff] [blame] | 251 | } |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 252 | #endif |
| 253 | |
commit-bot@chromium.org | 7b17152 | 2014-05-19 15:46:09 +0000 | [diff] [blame] | 254 | |
djsollen@google.com | a2ca41e | 2012-03-23 19:00:34 +0000 | [diff] [blame] | 255 | SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 256 | }; |
| 257 | |
| 258 | #endif |