SkGradientShader.h revision fbfcd5602128ec010c82cb733c9cdc0a3254f9f3
1
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#ifndef SkGradientShader_DEFINED
11#define SkGradientShader_DEFINED
12
13#include "SkShader.h"
14
15class SkUnitMapper;
16
17/** \class SkGradientShader
18
19    SkGradientShader hosts factories for creating subclasses of SkShader that
20    render linear and radial gradients.
21*/
22class SK_API SkGradientShader {
23public:
24    /** Returns a shader that generates a linear gradient between the two
25        specified points.
26        <p />
27        CreateLinear returns a shader with a reference count of 1.
28        The caller should decrement the shader's reference count when done with the shader.
29        It is an error for count to be < 2.
30        @param  pts The start and end points for the gradient.
31        @param  colors  The array[count] of colors, to be distributed between the two points
32        @param  pos     May be NULL. array[count] of SkScalars, or NULL, of the relative position of
33                        each corresponding color in the colors array. If this is NULL,
34                        the the colors are distributed evenly between the start and end point.
35                        If this is not null, the values must begin with 0, end with 1.0, and
36                        intermediate values must be strictly increasing.
37        @param  count   Must be >=2. The number of colors (and pos if not NULL) entries.
38        @param  mode    The tiling mode
39        @param  mapper  May be NULL. Callback to modify the spread of the colors.
40    */
41    static SkShader* CreateLinear(  const SkPoint pts[2],
42                                    const SkColor colors[], const SkScalar pos[], int count,
43                                    SkShader::TileMode mode,
44                                    SkUnitMapper* mapper = NULL);
45
46    /** Returns a shader that generates a radial gradient given the center and radius.
47        <p />
48        CreateRadial returns a shader with a reference count of 1.
49        The caller should decrement the shader's reference count when done with the shader.
50        It is an error for colorCount to be < 2, or for radius to be <= 0.
51        @param  center  The center of the circle for this gradient
52        @param  radius  Must be positive. The radius of the circle for this gradient
53        @param  colors  The array[count] of colors, to be distributed between the center and edge of the circle
54        @param  pos     May be NULL. The array[count] of SkScalars, or NULL, of the relative position of
55                        each corresponding color in the colors array. If this is NULL,
56                        the the colors are distributed evenly between the center and edge of the circle.
57                        If this is not null, the values must begin with 0, end with 1.0, and
58                        intermediate values must be strictly increasing.
59        @param  count   Must be >= 2. The number of colors (and pos if not NULL) entries
60        @param  mode    The tiling mode
61        @param  mapper  May be NULL. Callback to modify the spread of the colors.
62    */
63    static SkShader* CreateRadial(  const SkPoint& center, SkScalar radius,
64                                    const SkColor colors[], const SkScalar pos[], int count,
65                                    SkShader::TileMode mode,
66                                    SkUnitMapper* mapper = NULL);
67
68    /** Returns a shader that generates a radial gradient given the start position, start radius, end position and end radius.
69        <p />
70        CreateTwoPointRadial returns a shader with a reference count of 1.
71        The caller should decrement the shader's reference count when done with the shader.
72        It is an error for colorCount to be < 2, for startRadius or endRadius to be < 0, or for
73        startRadius to be equal to endRadius.
74        @param  start   The center of the start circle for this gradient
75        @param  startRadius  Must be positive.  The radius of the start circle for this gradient.
76        @param  end     The center of the end circle for this gradient
77        @param  endRadius  Must be positive. The radius of the end circle for this gradient.
78        @param  colors  The array[count] of colors, to be distributed between the center and edge of the circle
79        @param  pos     May be NULL. The array[count] of SkScalars, or NULL, of the relative position of
80                        each corresponding color in the colors array. If this is NULL,
81                        the the colors are distributed evenly between the center and edge of the circle.
82                        If this is not null, the values must begin with 0, end with 1.0, and
83                        intermediate values must be strictly increasing.
84        @param  count   Must be >= 2. The number of colors (and pos if not NULL) entries
85        @param  mode    The tiling mode
86        @param  mapper  May be NULL. Callback to modify the spread of the colors.
87    */
88    static SkShader* CreateTwoPointRadial(const SkPoint& start,
89                                          SkScalar startRadius,
90                                          const SkPoint& end,
91                                          SkScalar endRadius,
92                                          const SkColor colors[],
93                                          const SkScalar pos[], int count,
94                                          SkShader::TileMode mode,
95                                          SkUnitMapper* mapper = NULL);
96
97    /**
98     *  Returns a shader that generates a conical gradient given two circles, or
99     *  returns NULL if the inputs are invalid. The gradient interprets the
100     *  two circles according to the following HTML spec.
101     *  http://dev.w3.org/html5/2dcontext/#dom-context-2d-createradialgradient
102     */
103    static SkShader* CreateTwoPointConical(const SkPoint& start,
104                                          SkScalar startRadius,
105                                          const SkPoint& end,
106                                          SkScalar endRadius,
107                                          const SkColor colors[],
108                                          const SkScalar pos[], int count,
109                                          SkShader::TileMode mode,
110                                          SkUnitMapper* mapper = NULL);
111
112    /** Returns a shader that generates a sweep gradient given a center.
113        <p />
114        CreateSweep returns a shader with a reference count of 1.
115        The caller should decrement the shader's reference count when done with the shader.
116        It is an error for colorCount to be < 2.
117        @param  cx      The X coordinate of the center of the sweep
118        @param  cx      The Y coordinate of the center of the sweep
119        @param  colors  The array[count] of colors, to be distributed around the center.
120        @param  pos     May be NULL. The array[count] of SkScalars, or NULL, of the relative position of
121                        each corresponding color in the colors array. If this is NULL,
122                        the the colors are distributed evenly between the center and edge of the circle.
123                        If this is not null, the values must begin with 0, end with 1.0, and
124                        intermediate values must be strictly increasing.
125        @param  count   Must be >= 2. The number of colors (and pos if not NULL) entries
126        @param  mapper  May be NULL. Callback to modify the spread of the colors.
127    */
128    static SkShader* CreateSweep(SkScalar cx, SkScalar cy,
129                                 const SkColor colors[], const SkScalar pos[],
130                                 int count, SkUnitMapper* mapper = NULL);
131
132    SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()
133};
134
135#endif
136
137