SkGradientShader.h 16 KB

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