blob: d9276c9ad942c718826638d20173618e8efdbb0e [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
11#include "SkShader.h"
12
commit-bot@chromium.orge2b193c2014-05-16 13:49:08 +000013class SkUnitMapper;
14
reed@android.com8a1c16f2008-12-17 15:59:43 +000015/** \class SkGradientShader
16
17 SkGradientShader hosts factories for creating subclasses of SkShader that
18 render linear and radial gradients.
19*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000020class SK_API SkGradientShader {
reed@android.com8a1c16f2008-12-17 15:59:43 +000021public:
reed@google.com3d3a8602013-05-24 14:58:44 +000022 enum Flags {
23 /** By default gradients will interpolate their colors in unpremul space
24 * and then premultiply each of the results. By setting this flag, the
25 * gradients will premultiply their colors first, and then interpolate
26 * between them.
27 */
28 kInterpolateColorsInPremul_Flag = 1 << 0,
29 };
30
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 /** Returns a shader that generates a linear gradient between the two
32 specified points.
33 <p />
34 CreateLinear returns a shader with a reference count of 1.
35 The caller should decrement the shader's reference count when done with the shader.
36 It is an error for count to be < 2.
37 @param pts The start and end points for the gradient.
38 @param colors The array[count] of colors, to be distributed between the two points
39 @param pos May be NULL. array[count] of SkScalars, or NULL, of the relative position of
40 each corresponding color in the colors array. If this is NULL,
41 the the colors are distributed evenly between the start and end point.
42 If this is not null, the values must begin with 0, end with 1.0, and
43 intermediate values must be strictly increasing.
rmistry@google.comfbfcd562012-08-23 18:09:54 +000044 @param count Must be >=2. The number of colors (and pos if not NULL) entries.
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 @param mode The tiling mode
commit-bot@chromium.orge2b193c2014-05-16 13:49:08 +000046 @param mapper May be NULL. Callback to modify the spread of the colors.
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 */
reed@google.com3d3a8602013-05-24 14:58:44 +000048 static SkShader* CreateLinear(const SkPoint pts[2],
49 const SkColor colors[], const SkScalar pos[], int count,
50 SkShader::TileMode mode,
commit-bot@chromium.orge2b193c2014-05-16 13:49:08 +000051 SkUnitMapper* mapper = NULL,
52 uint32_t flags = 0,
53 const SkMatrix* localMatrix = NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +000054
commit-bot@chromium.org7b171522014-05-19 15:46:09 +000055 static SkShader* CreateLinear(const SkPoint pts[2],
56 const SkColor colors[], const SkScalar pos[], int count,
57 SkShader::TileMode mode,
58 uint32_t flags,
59 const SkMatrix* localMatrix) {
60 return CreateLinear(pts, colors, pos, count, mode, NULL, flags, localMatrix);
61 }
62
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 /** Returns a shader that generates a radial gradient given the center and radius.
64 <p />
65 CreateRadial returns a shader with a reference count of 1.
66 The caller should decrement the shader's reference count when done with the shader.
67 It is an error for colorCount to be < 2, or for radius to be <= 0.
68 @param center The center of the circle for this gradient
69 @param radius Must be positive. The radius of the circle for this gradient
70 @param colors The array[count] of colors, to be distributed between the center and edge of the circle
71 @param pos May be NULL. The array[count] of SkScalars, or NULL, of the relative position of
72 each corresponding color in the colors array. If this is NULL,
73 the the colors are distributed evenly between the center and edge of the circle.
74 If this is not null, the values must begin with 0, end with 1.0, and
75 intermediate values must be strictly increasing.
76 @param count Must be >= 2. The number of colors (and pos if not NULL) entries
77 @param mode The tiling mode
commit-bot@chromium.orge2b193c2014-05-16 13:49:08 +000078 @param mapper May be NULL. Callback to modify the spread of the colors.
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 */
reed@google.com3d3a8602013-05-24 14:58:44 +000080 static SkShader* CreateRadial(const SkPoint& center, SkScalar radius,
81 const SkColor colors[], const SkScalar pos[], int count,
82 SkShader::TileMode mode,
commit-bot@chromium.orge2b193c2014-05-16 13:49:08 +000083 SkUnitMapper* mapper = NULL,
84 uint32_t flags = 0,
85 const SkMatrix* localMatrix = NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +000086
commit-bot@chromium.org7b171522014-05-19 15:46:09 +000087 static SkShader* CreateRadial(const SkPoint& center, SkScalar radius,
88 const SkColor colors[], const SkScalar pos[], int count,
89 SkShader::TileMode mode,
90 uint32_t flags,
91 const SkMatrix* localMatrix) {
92 return CreateRadial(center, radius, colors, pos, count, mode, NULL, flags, localMatrix);
93 }
94
senorblanco@chromium.org7ef071f2009-09-22 17:25:29 +000095 /** Returns a shader that generates a radial gradient given the start position, start radius, end position and end radius.
96 <p />
97 CreateTwoPointRadial returns a shader with a reference count of 1.
98 The caller should decrement the shader's reference count when done with the shader.
99 It is an error for colorCount to be < 2, for startRadius or endRadius to be < 0, or for
100 startRadius to be equal to endRadius.
101 @param start The center of the start circle for this gradient
102 @param startRadius Must be positive. The radius of the start circle for this gradient.
103 @param end The center of the end circle for this gradient
104 @param endRadius Must be positive. The radius of the end circle for this gradient.
105 @param colors The array[count] of colors, to be distributed between the center and edge of the circle
106 @param pos May be NULL. The array[count] of SkScalars, or NULL, of the relative position of
107 each corresponding color in the colors array. If this is NULL,
108 the the colors are distributed evenly between the center and edge of the circle.
109 If this is not null, the values must begin with 0, end with 1.0, and
110 intermediate values must be strictly increasing.
111 @param count Must be >= 2. The number of colors (and pos if not NULL) entries
112 @param mode The tiling mode
commit-bot@chromium.orge2b193c2014-05-16 13:49:08 +0000113 @param mapper May be NULL. Callback to modify the spread of the colors.
senorblanco@chromium.org7ef071f2009-09-22 17:25:29 +0000114 */
commit-bot@chromium.orge2b193c2014-05-16 13:49:08 +0000115 static SkShader* CreateTwoPointRadial(const SkPoint& start,
116 SkScalar startRadius,
117 const SkPoint& end,
118 SkScalar endRadius,
119 const SkColor colors[],
120 const SkScalar pos[], int count,
senorblanco@chromium.org7ef071f2009-09-22 17:25:29 +0000121 SkShader::TileMode mode,
commit-bot@chromium.orge2b193c2014-05-16 13:49:08 +0000122 SkUnitMapper* mapper = NULL,
123 uint32_t flags = 0,
124 const SkMatrix* localMatrix = NULL);
reed@google.comcb7be692012-06-06 20:31:56 +0000125
commit-bot@chromium.org7b171522014-05-19 15:46:09 +0000126 static SkShader* CreateTwoPointRadial(const SkPoint& start,
127 SkScalar startRadius,
128 const SkPoint& end,
129 SkScalar endRadius,
130 const SkColor colors[],
131 const SkScalar pos[], int count,
132 SkShader::TileMode mode,
133 uint32_t flags,
134 const SkMatrix* localMatrix) {
135 return CreateTwoPointRadial(start, startRadius, end, endRadius, colors, pos, count, mode,
136 NULL, flags, localMatrix);
137 }
138
reed@google.comcb7be692012-06-06 20:31:56 +0000139 /**
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 */
commit-bot@chromium.orge2b193c2014-05-16 13:49:08 +0000145 static SkShader* CreateTwoPointConical(const SkPoint& start,
146 SkScalar startRadius,
147 const SkPoint& end,
148 SkScalar endRadius,
149 const SkColor colors[],
150 const SkScalar pos[], int count,
reed@google.com3d3a8602013-05-24 14:58:44 +0000151 SkShader::TileMode mode,
commit-bot@chromium.orge2b193c2014-05-16 13:49:08 +0000152 SkUnitMapper* mapper = NULL,
153 uint32_t flags = 0,
154 const SkMatrix* localMatrix = NULL);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000155
commit-bot@chromium.org7b171522014-05-19 15:46:09 +0000156 static SkShader* CreateTwoPointConical(const SkPoint& start,
157 SkScalar startRadius,
158 const SkPoint& end,
159 SkScalar endRadius,
160 const SkColor colors[],
161 const SkScalar pos[], int count,
162 SkShader::TileMode mode,
163 uint32_t flags,
164 const SkMatrix* localMatrix) {
165 return CreateTwoPointConical(start, startRadius, end, endRadius, colors, pos, count, mode,
166 NULL, flags, localMatrix);
167 }
168
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169 /** Returns a shader that generates a sweep gradient given a center.
170 <p />
senorblanco@chromium.org7ef071f2009-09-22 17:25:29 +0000171 CreateSweep returns a shader with a reference count of 1.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 The caller should decrement the shader's reference count when done with the shader.
173 It is an error for colorCount to be < 2.
174 @param cx The X coordinate of the center of the sweep
175 @param cx The Y coordinate of the center of the sweep
176 @param colors The array[count] of colors, to be distributed around the center.
177 @param pos May be NULL. The array[count] of SkScalars, or NULL, of the relative position of
178 each corresponding color in the colors array. If this is NULL,
179 the the colors are distributed evenly between the center and edge of the circle.
180 If this is not null, the values must begin with 0, end with 1.0, and
181 intermediate values must be strictly increasing.
182 @param count Must be >= 2. The number of colors (and pos if not NULL) entries
commit-bot@chromium.orge2b193c2014-05-16 13:49:08 +0000183 @param mapper May be NULL. Callback to modify the spread of the colors.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000184 */
185 static SkShader* CreateSweep(SkScalar cx, SkScalar cy,
commit-bot@chromium.orge2b193c2014-05-16 13:49:08 +0000186 const SkColor colors[], const SkScalar pos[],
187 int count, SkUnitMapper* mapper = NULL,
188 uint32_t flags = 0,
189 const SkMatrix* localMatrix = NULL);
caryclark@google.comd26147a2011-12-15 14:16:43 +0000190
commit-bot@chromium.org7b171522014-05-19 15:46:09 +0000191 static SkShader* CreateSweep(SkScalar cx, SkScalar cy,
192 const SkColor colors[], const SkScalar pos[], int count,
193 uint32_t flags, const SkMatrix* localMatrix) {
194 return CreateSweep(cx, cy, colors, pos, count, NULL, flags, localMatrix);
195 }
196
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000197 SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198};
199
200#endif