1 /* 2 * Copyright 2006 The Android Open Source Project 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef SkGradientShader_DEFINED 9 #define SkGradientShader_DEFINED 10 11 #include "include/core/SkShader.h" 12 13 /** \class SkGradientShader 14 15 SkGradientShader hosts factories for creating subclasses of SkShader that 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. 46 */ 47 class SK_API SkGradientShader { 48 public: 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 58 /** Returns a shader that generates a linear gradient between the two specified points. 59 <p /> 60 @param pts The start and end points for the gradient. 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. 67 @param count Must be >=2. The number of colors (and pos if not NULL) entries. 68 @param mode The tiling mode 69 */ 70 static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], 71 const SkColor colors[], const SkScalar pos[], int count, 72 SkTileMode mode, 73 uint32_t flags, const SkMatrix* localMatrix); MakeLinear(const SkPoint pts[2],const SkColor colors[],const SkScalar pos[],int count,SkTileMode mode)74 static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], 75 const SkColor colors[], const SkScalar pos[], int count, 76 SkTileMode mode) { 77 return MakeLinear(pts, colors, pos, count, mode, 0, nullptr); 78 } 79 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, 94 const SkScalar pos[], int count, SkTileMode mode, 95 uint32_t flags, const SkMatrix* localMatrix); MakeLinear(const SkPoint pts[2],const SkColor4f colors[],sk_sp<SkColorSpace> colorSpace,const SkScalar pos[],int count,SkTileMode mode)96 static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], 97 const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace, 98 const SkScalar pos[], int count, SkTileMode mode) { 99 return MakeLinear(pts, colors, std::move(colorSpace), pos, count, mode, 0, nullptr); 100 } 101 102 /** Returns a shader that generates a radial gradient given the center and radius. 103 <p /> 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 114 */ 115 static sk_sp<SkShader> MakeRadial(const SkPoint& center, SkScalar radius, 116 const SkColor colors[], const SkScalar pos[], int count, 117 SkTileMode mode, 118 uint32_t flags, const SkMatrix* localMatrix); MakeRadial(const SkPoint & center,SkScalar radius,const SkColor colors[],const SkScalar pos[],int count,SkTileMode mode)119 static sk_sp<SkShader> MakeRadial(const SkPoint& center, SkScalar radius, 120 const SkColor colors[], const SkScalar pos[], int count, 121 SkTileMode mode) { 122 return MakeRadial(center, radius, colors, pos, count, mode, 0, nullptr); 123 } 124 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, 140 const SkScalar pos[], int count, SkTileMode mode, 141 uint32_t flags, const SkMatrix* localMatrix); MakeRadial(const SkPoint & center,SkScalar radius,const SkColor4f colors[],sk_sp<SkColorSpace> colorSpace,const SkScalar pos[],int count,SkTileMode mode)142 static sk_sp<SkShader> MakeRadial(const SkPoint& center, SkScalar radius, 143 const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace, 144 const SkScalar pos[], int count, SkTileMode mode) { 145 return MakeRadial(center, radius, colors, std::move(colorSpace), pos, count, mode, 146 0, nullptr); 147 } 148 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 */ 155 static sk_sp<SkShader> MakeTwoPointConical(const SkPoint& start, SkScalar startRadius, 156 const SkPoint& end, SkScalar endRadius, 157 const SkColor colors[], const SkScalar pos[], 158 int count, SkTileMode mode, 159 uint32_t flags, const SkMatrix* localMatrix); MakeTwoPointConical(const SkPoint & start,SkScalar startRadius,const SkPoint & end,SkScalar endRadius,const SkColor colors[],const SkScalar pos[],int count,SkTileMode mode)160 static sk_sp<SkShader> MakeTwoPointConical(const SkPoint& start, SkScalar startRadius, 161 const SkPoint& end, SkScalar endRadius, 162 const SkColor colors[], const SkScalar pos[], 163 int count, SkTileMode mode) { 164 return MakeTwoPointConical(start, startRadius, end, endRadius, colors, pos, count, mode, 165 0, nullptr); 166 } 167 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[], 178 int count, SkTileMode mode, 179 uint32_t flags, const SkMatrix* localMatrix); MakeTwoPointConical(const SkPoint & start,SkScalar startRadius,const SkPoint & end,SkScalar endRadius,const SkColor4f colors[],sk_sp<SkColorSpace> colorSpace,const SkScalar pos[],int count,SkTileMode mode)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[], 184 int count, SkTileMode mode) { 185 return MakeTwoPointConical(start, startRadius, end, endRadius, colors, 186 std::move(colorSpace), pos, count, mode, 0, nullptr); 187 } 188 189 /** Returns a shader that generates a sweep gradient given a center. 190 <p /> 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. 204 */ 205 static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy, 206 const SkColor colors[], const SkScalar pos[], int count, 207 SkTileMode mode, 208 SkScalar startAngle, SkScalar endAngle, 209 uint32_t flags, const SkMatrix* localMatrix); MakeSweep(SkScalar cx,SkScalar cy,const SkColor colors[],const SkScalar pos[],int count,uint32_t flags,const SkMatrix * localMatrix)210 static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy, 211 const SkColor colors[], const SkScalar pos[], int count, 212 uint32_t flags, const SkMatrix* localMatrix) { 213 return MakeSweep(cx, cy, colors, pos, count, SkTileMode::kClamp, 0, 360, flags, 214 localMatrix); 215 } MakeSweep(SkScalar cx,SkScalar cy,const SkColor colors[],const SkScalar pos[],int count)216 static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy, 217 const SkColor colors[], const SkScalar pos[], int count) { 218 return MakeSweep(cx, cy, colors, pos, count, 0, nullptr); 219 } 220 221 /** Returns a shader that generates a sweep gradient given a center. 222 <p /> 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. 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, 240 SkTileMode mode, 241 SkScalar startAngle, SkScalar endAngle, 242 uint32_t flags, const SkMatrix* localMatrix); MakeSweep(SkScalar cx,SkScalar cy,const SkColor4f colors[],sk_sp<SkColorSpace> colorSpace,const SkScalar pos[],int count,uint32_t flags,const SkMatrix * localMatrix)243 static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy, 244 const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace, 245 const SkScalar pos[], int count, 246 uint32_t flags, const SkMatrix* localMatrix) { 247 return MakeSweep(cx, cy, colors, std::move(colorSpace), pos, count, 248 SkTileMode::kClamp, 0, 360, flags, localMatrix); 249 } MakeSweep(SkScalar cx,SkScalar cy,const SkColor4f colors[],sk_sp<SkColorSpace> colorSpace,const SkScalar pos[],int count)250 static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy, 251 const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace, 252 const SkScalar pos[], int count) { 253 return MakeSweep(cx, cy, colors, std::move(colorSpace), pos, count, 0, nullptr); 254 } 255 256 static void RegisterFlattenables(); 257 }; 258 259 #endif 260