SkGradientShader.h revision 9c9005a347e9996f357bd79591bd34f74f8bbc66
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 "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*/
20class SK_API SkGradientShader {
21public:
22    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
31    /** 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.
44        @param  count   Must be >=2. The number of colors (and pos if not NULL) entries.
45        @param  mode    The tiling mode
46        @param  mapper  May be NULL. Callback to modify the spread of the colors.
47    */
48    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,
53                                  const SkMatrix* localMatrix = NULL);
54
55    /** Returns a shader that generates a radial gradient given the center and radius.
56        <p />
57        CreateRadial returns a shader with a reference count of 1.
58        The caller should decrement the shader's reference count when done with the shader.
59        It is an error for colorCount to be < 2, or for radius to be <= 0.
60        @param  center  The center of the circle for this gradient
61        @param  radius  Must be positive. The radius of the circle for this gradient
62        @param  colors  The array[count] of colors, to be distributed between the center and edge of the circle
63        @param  pos     May be NULL. The array[count] of SkScalars, or NULL, of the relative position of
64                        each corresponding color in the colors array. If this is NULL,
65                        the the colors are distributed evenly between the center and edge of the circle.
66                        If this is not null, the values must begin with 0, end with 1.0, and
67                        intermediate values must be strictly increasing.
68        @param  count   Must be >= 2. The number of colors (and pos if not NULL) entries
69        @param  mode    The tiling mode
70        @param  mapper  May be NULL. Callback to modify the spread of the colors.
71    */
72    static SkShader* CreateRadial(const SkPoint& center, SkScalar radius,
73                                  const SkColor colors[], const SkScalar pos[], int count,
74                                  SkShader::TileMode mode,
75                                  SkUnitMapper* mapper = NULL,
76                                  uint32_t flags = 0,
77                                  const SkMatrix* localMatrix = NULL);
78
79    /** Returns a shader that generates a radial gradient given the start position, start radius, end position and end radius.
80        <p />
81        CreateTwoPointRadial returns a shader with a reference count of 1.
82        The caller should decrement the shader's reference count when done with the shader.
83        It is an error for colorCount to be < 2, for startRadius or endRadius to be < 0, or for
84        startRadius to be equal to endRadius.
85        @param  start   The center of the start circle for this gradient
86        @param  startRadius  Must be positive.  The radius of the start circle for this gradient.
87        @param  end     The center of the end circle for this gradient
88        @param  endRadius  Must be positive. The radius of the end circle for this gradient.
89        @param  colors  The array[count] of colors, to be distributed between the center and edge of the circle
90        @param  pos     May be NULL. The array[count] of SkScalars, or NULL, of the relative position of
91                        each corresponding color in the colors array. If this is NULL,
92                        the the colors are distributed evenly between the center and edge of the circle.
93                        If this is not null, the values must begin with 0, end with 1.0, and
94                        intermediate values must be strictly increasing.
95        @param  count   Must be >= 2. The number of colors (and pos if not NULL) entries
96        @param  mode    The tiling mode
97        @param  mapper  May be NULL. Callback to modify the spread of the colors.
98    */
99    static SkShader* CreateTwoPointRadial(const SkPoint& start,
100                                          SkScalar startRadius,
101                                          const SkPoint& end,
102                                          SkScalar endRadius,
103                                          const SkColor colors[],
104                                          const SkScalar pos[], int count,
105                                          SkShader::TileMode mode,
106                                          SkUnitMapper* mapper = NULL,
107                                          uint32_t flags = 0,
108                                          const SkMatrix* localMatrix = NULL);
109
110    /**
111     *  Returns a shader that generates a conical gradient given two circles, or
112     *  returns NULL if the inputs are invalid. The gradient interprets the
113     *  two circles according to the following HTML spec.
114     *  http://dev.w3.org/html5/2dcontext/#dom-context-2d-createradialgradient
115     */
116    static SkShader* CreateTwoPointConical(const SkPoint& start,
117                                           SkScalar startRadius,
118                                           const SkPoint& end,
119                                           SkScalar endRadius,
120                                           const SkColor colors[],
121                                           const SkScalar pos[], int count,
122                                           SkShader::TileMode mode,
123                                           SkUnitMapper* mapper = NULL,
124                                           uint32_t flags = 0,
125                                           const SkMatrix* localMatrix = NULL);
126
127    /** Returns a shader that generates a sweep gradient given a center.
128        <p />
129        CreateSweep returns a shader with a reference count of 1.
130        The caller should decrement the shader's reference count when done with the shader.
131        It is an error for colorCount to be < 2.
132        @param  cx      The X coordinate of the center of the sweep
133        @param  cx      The Y coordinate of the center of the sweep
134        @param  colors  The array[count] of colors, to be distributed around the center.
135        @param  pos     May be NULL. The array[count] of SkScalars, or NULL, of the relative position of
136                        each corresponding color in the colors array. If this is NULL,
137                        the the colors are distributed evenly between the center and edge of the circle.
138                        If this is not null, the values must begin with 0, end with 1.0, and
139                        intermediate values must be strictly increasing.
140        @param  count   Must be >= 2. The number of colors (and pos if not NULL) entries
141        @param  mapper  May be NULL. Callback to modify the spread of the colors.
142    */
143    static SkShader* CreateSweep(SkScalar cx, SkScalar cy,
144                                 const SkColor colors[], const SkScalar pos[],
145                                 int count, SkUnitMapper* mapper = NULL,
146                                 uint32_t flags = 0,
147                                 const SkMatrix* localMatrix = NULL);
148
149    SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()
150};
151
152#endif
153