blob: daa6ed080874f386f5022878d426caac3899383c [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
8#ifndef SkGradientShader_DEFINED
9#define SkGradientShader_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkShader.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012
reed@android.com8a1c16f2008-12-17 15:59:43 +000013/** \class SkGradientShader
14
15 SkGradientShader hosts factories for creating subclasses of SkShader that
Mike Klein024072a2018-11-11 00:26:30 +000016 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.com8a1c16f2008-12-17 15:59:43 +000046*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000047class SK_API SkGradientShader {
reed@android.com8a1c16f2008-12-17 15:59:43 +000048public:
reed@google.com3d3a8602013-05-24 14:58:44 +000049 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
brianosmana99b66d2016-09-27 03:33:12 -070058 /** Returns a shader that generates a linear gradient between the two specified points.
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 <p />
brianosmana99b66d2016-09-27 03:33:12 -070060 @param pts The start and end points for the gradient.
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 @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.comfbfcd562012-08-23 18:09:54 +000067 @param count Must be >=2. The number of colors (and pos if not NULL) entries.
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 @param mode The tiling mode
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 */
reed8a21c9f2016-03-08 18:50:00 -080070 static sk_sp<SkShader> MakeLinear(const SkPoint pts[2],
71 const SkColor colors[], const SkScalar pos[], int count,
Mike Reedfae8fce2019-04-03 10:27:45 -040072 SkTileMode mode,
reed8a21c9f2016-03-08 18:50:00 -080073 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 Reedfae8fce2019-04-03 10:27:45 -040076 SkTileMode mode) {
77 return MakeLinear(pts, colors, pos, count, mode, 0, nullptr);
78 }
commit-bot@chromium.org7b171522014-05-19 15:46:09 +000079
brianosmane25d71c2016-09-28 11:27:28 -070080 /** 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 Reedfae8fce2019-04-03 10:27:45 -040094 const SkScalar pos[], int count, SkTileMode mode,
brianosmane25d71c2016-09-28 11:27:28 -070095 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 Reedfae8fce2019-04-03 10:27:45 -040098 const SkScalar pos[], int count, SkTileMode mode) {
99 return MakeLinear(pts, colors, std::move(colorSpace), pos, count, mode, 0, nullptr);
100 }
brianosmane25d71c2016-09-28 11:27:28 -0700101
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 /** Returns a shader that generates a radial gradient given the center and radius.
103 <p />
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 @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.com8a1c16f2008-12-17 15:59:43 +0000114 */
reed8a21c9f2016-03-08 18:50:00 -0800115 static sk_sp<SkShader> MakeRadial(const SkPoint& center, SkScalar radius,
116 const SkColor colors[], const SkScalar pos[], int count,
Mike Reedfae8fce2019-04-03 10:27:45 -0400117 SkTileMode mode,
reed8a21c9f2016-03-08 18:50:00 -0800118 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 Reedfae8fce2019-04-03 10:27:45 -0400121 SkTileMode mode) {
122 return MakeRadial(center, radius, colors, pos, count, mode, 0, nullptr);
123 }
commit-bot@chromium.org7b171522014-05-19 15:46:09 +0000124
brianosmane25d71c2016-09-28 11:27:28 -0700125 /** 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 Reedfae8fce2019-04-03 10:27:45 -0400140 const SkScalar pos[], int count, SkTileMode mode,
brianosmane25d71c2016-09-28 11:27:28 -0700141 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 Reedfae8fce2019-04-03 10:27:45 -0400144 const SkScalar pos[], int count, SkTileMode mode) {
145 return MakeRadial(center, radius, colors, std::move(colorSpace), pos, count, mode,
146 0, nullptr);
147 }
brianosmane25d71c2016-09-28 11:27:28 -0700148
reed@google.comcb7be692012-06-06 20:31:56 +0000149 /**
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 */
reed8a21c9f2016-03-08 18:50:00 -0800155 static sk_sp<SkShader> MakeTwoPointConical(const SkPoint& start, SkScalar startRadius,
156 const SkPoint& end, SkScalar endRadius,
157 const SkColor colors[], const SkScalar pos[],
Mike Reedfae8fce2019-04-03 10:27:45 -0400158 int count, SkTileMode mode,
reed8a21c9f2016-03-08 18:50:00 -0800159 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 Reedfae8fce2019-04-03 10:27:45 -0400163 int count, SkTileMode mode) {
164 return MakeTwoPointConical(start, startRadius, end, endRadius, colors, pos, count, mode,
165 0, nullptr);
166 }
commit-bot@chromium.org7b171522014-05-19 15:46:09 +0000167
brianosmane25d71c2016-09-28 11:27:28 -0700168 /**
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 Reedfae8fce2019-04-03 10:27:45 -0400178 int count, SkTileMode mode,
brianosmane25d71c2016-09-28 11:27:28 -0700179 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 Reedfae8fce2019-04-03 10:27:45 -0400184 int count, SkTileMode mode) {
185 return MakeTwoPointConical(start, startRadius, end, endRadius, colors,
186 std::move(colorSpace), pos, count, mode, 0, nullptr);
187 }
brianosmane25d71c2016-09-28 11:27:28 -0700188
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189 /** Returns a shader that generates a sweep gradient given a center.
190 <p />
Florin Malita5a9a9812017-08-01 16:38:08 -0400191 @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.com8a1c16f2008-12-17 15:59:43 +0000204 */
reed8a21c9f2016-03-08 18:50:00 -0800205 static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy,
206 const SkColor colors[], const SkScalar pos[], int count,
Mike Reedfae8fce2019-04-03 10:27:45 -0400207 SkTileMode mode,
Florin Malita5a9a9812017-08-01 16:38:08 -0400208 SkScalar startAngle, SkScalar endAngle,
reed8a21c9f2016-03-08 18:50:00 -0800209 uint32_t flags, const SkMatrix* localMatrix);
210 static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy,
Florin Malita5a9a9812017-08-01 16:38:08 -0400211 const SkColor colors[], const SkScalar pos[], int count,
212 uint32_t flags, const SkMatrix* localMatrix) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400213 return MakeSweep(cx, cy, colors, pos, count, SkTileMode::kClamp, 0, 360, flags,
Florin Malita5a9a9812017-08-01 16:38:08 -0400214 localMatrix);
215 }
216 static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy,
reed8a21c9f2016-03-08 18:50:00 -0800217 const SkColor colors[], const SkScalar pos[], int count) {
Florin Malita5a9a9812017-08-01 16:38:08 -0400218 return MakeSweep(cx, cy, colors, pos, count, 0, nullptr);
reed8a21c9f2016-03-08 18:50:00 -0800219 }
220
brianosmane25d71c2016-09-28 11:27:28 -0700221 /** Returns a shader that generates a sweep gradient given a center.
222 <p />
Florin Malita5a9a9812017-08-01 16:38:08 -0400223 @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.
brianosmane25d71c2016-09-28 11:27:28 -0700236 */
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 Reedfae8fce2019-04-03 10:27:45 -0400240 SkTileMode mode,
Florin Malita5a9a9812017-08-01 16:38:08 -0400241 SkScalar startAngle, SkScalar endAngle,
brianosmane25d71c2016-09-28 11:27:28 -0700242 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 Malita5a9a9812017-08-01 16:38:08 -0400245 const SkScalar pos[], int count,
246 uint32_t flags, const SkMatrix* localMatrix) {
247 return MakeSweep(cx, cy, colors, std::move(colorSpace), pos, count,
Mike Reedfae8fce2019-04-03 10:27:45 -0400248 SkTileMode::kClamp, 0, 360, flags, localMatrix);
Florin Malita5a9a9812017-08-01 16:38:08 -0400249 }
250 static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy,
251 const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace,
brianosmane25d71c2016-09-28 11:27:28 -0700252 const SkScalar pos[], int count) {
Florin Malita5a9a9812017-08-01 16:38:08 -0400253 return MakeSweep(cx, cy, colors, std::move(colorSpace), pos, count, 0, nullptr);
brianosmane25d71c2016-09-28 11:27:28 -0700254 }
255
Mike Kleinfa5f6ce2018-10-20 08:21:31 -0400256 static void RegisterFlattenables();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000257};
258
259#endif