blob: ed0f1bf75b5454e5809fbfb1f09449d881b71110 [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
13class SkUnitMapper;
14
15/** \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
46 @param mapper May be NULL. Callback to modify the spread of the colors.
47 */
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,
51 SkUnitMapper* mapper = NULL,
52 uint32_t flags = 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +000053
54 /** Returns a shader that generates a radial gradient given the center and radius.
55 <p />
56 CreateRadial returns a shader with a reference count of 1.
57 The caller should decrement the shader's reference count when done with the shader.
58 It is an error for colorCount to be < 2, or for radius to be <= 0.
59 @param center The center of the circle for this gradient
60 @param radius Must be positive. The radius of the circle for this gradient
61 @param colors The array[count] of colors, to be distributed between the center and edge of the circle
62 @param pos May be NULL. The 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 center and edge of the circle.
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 @param mapper May be NULL. Callback to modify the spread of the colors.
70 */
reed@google.com3d3a8602013-05-24 14:58:44 +000071 static SkShader* CreateRadial(const SkPoint& center, SkScalar radius,
72 const SkColor colors[], const SkScalar pos[], int count,
73 SkShader::TileMode mode,
74 SkUnitMapper* mapper = NULL,
75 uint32_t flags = 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +000076
senorblanco@chromium.org7ef071f2009-09-22 17:25:29 +000077 /** Returns a shader that generates a radial gradient given the start position, start radius, end position and end radius.
78 <p />
79 CreateTwoPointRadial returns a shader with a reference count of 1.
80 The caller should decrement the shader's reference count when done with the shader.
81 It is an error for colorCount to be < 2, for startRadius or endRadius to be < 0, or for
82 startRadius to be equal to endRadius.
83 @param start The center of the start circle for this gradient
84 @param startRadius Must be positive. The radius of the start circle for this gradient.
85 @param end The center of the end circle for this gradient
86 @param endRadius Must be positive. The radius of the end circle for this gradient.
87 @param colors The array[count] of colors, to be distributed between the center and edge of the circle
88 @param pos May be NULL. The array[count] of SkScalars, or NULL, of the relative position of
89 each corresponding color in the colors array. If this is NULL,
90 the the colors are distributed evenly between the center and edge of the circle.
91 If this is not null, the values must begin with 0, end with 1.0, and
92 intermediate values must be strictly increasing.
93 @param count Must be >= 2. The number of colors (and pos if not NULL) entries
94 @param mode The tiling mode
95 @param mapper May be NULL. Callback to modify the spread of the colors.
96 */
97 static SkShader* CreateTwoPointRadial(const SkPoint& start,
98 SkScalar startRadius,
99 const SkPoint& end,
100 SkScalar endRadius,
101 const SkColor colors[],
102 const SkScalar pos[], int count,
103 SkShader::TileMode mode,
reed@google.com3d3a8602013-05-24 14:58:44 +0000104 SkUnitMapper* mapper = NULL,
105 uint32_t flags = 0);
reed@google.comcb7be692012-06-06 20:31:56 +0000106
107 /**
108 * Returns a shader that generates a conical gradient given two circles, or
109 * returns NULL if the inputs are invalid. The gradient interprets the
110 * two circles according to the following HTML spec.
111 * http://dev.w3.org/html5/2dcontext/#dom-context-2d-createradialgradient
112 */
113 static SkShader* CreateTwoPointConical(const SkPoint& start,
reed@google.com3d3a8602013-05-24 14:58:44 +0000114 SkScalar startRadius,
115 const SkPoint& end,
116 SkScalar endRadius,
117 const SkColor colors[],
118 const SkScalar pos[], int count,
119 SkShader::TileMode mode,
120 SkUnitMapper* mapper = NULL,
121 uint32_t flags = 0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000122
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 /** Returns a shader that generates a sweep gradient given a center.
124 <p />
senorblanco@chromium.org7ef071f2009-09-22 17:25:29 +0000125 CreateSweep returns a shader with a reference count of 1.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 The caller should decrement the shader's reference count when done with the shader.
127 It is an error for colorCount to be < 2.
128 @param cx The X coordinate of the center of the sweep
129 @param cx The Y coordinate of the center of the sweep
130 @param colors The array[count] of colors, to be distributed around the center.
131 @param pos May be NULL. The array[count] of SkScalars, or NULL, of the relative position of
132 each corresponding color in the colors array. If this is NULL,
133 the the colors are distributed evenly between the center and edge of the circle.
134 If this is not null, the values must begin with 0, end with 1.0, and
135 intermediate values must be strictly increasing.
136 @param count Must be >= 2. The number of colors (and pos if not NULL) entries
137 @param mapper May be NULL. Callback to modify the spread of the colors.
138 */
139 static SkShader* CreateSweep(SkScalar cx, SkScalar cy,
140 const SkColor colors[], const SkScalar pos[],
reed@google.com3d3a8602013-05-24 14:58:44 +0000141 int count, SkUnitMapper* mapper = NULL,
142 uint32_t flags = 0);
caryclark@google.comd26147a2011-12-15 14:16:43 +0000143
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000144 SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145};
146
147#endif