blob: ae11e2dafd1fb81f0580f8e6d71c2d2d56d93fac [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
reed@android.com8a1c16f2008-12-17 15:59:43 +000013/** \class SkGradientShader
14
15 SkGradientShader hosts factories for creating subclasses of SkShader that
16 render linear and radial gradients.
17*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000018class SK_API SkGradientShader {
reed@android.com8a1c16f2008-12-17 15:59:43 +000019public:
reed@google.com3d3a8602013-05-24 14:58:44 +000020 enum Flags {
21 /** By default gradients will interpolate their colors in unpremul space
22 * and then premultiply each of the results. By setting this flag, the
23 * gradients will premultiply their colors first, and then interpolate
24 * between them.
25 */
26 kInterpolateColorsInPremul_Flag = 1 << 0,
27 };
28
reed@android.com8a1c16f2008-12-17 15:59:43 +000029 /** Returns a shader that generates a linear gradient between the two
30 specified points.
31 <p />
32 CreateLinear returns a shader with a reference count of 1.
33 The caller should decrement the shader's reference count when done with the shader.
34 It is an error for count to be < 2.
35 @param pts The start and end points for the gradient.
36 @param colors The array[count] of colors, to be distributed between the two points
37 @param pos May be NULL. array[count] of SkScalars, or NULL, of the relative position of
38 each corresponding color in the colors array. If this is NULL,
39 the the colors are distributed evenly between the start and end point.
40 If this is not null, the values must begin with 0, end with 1.0, and
41 intermediate values must be strictly increasing.
rmistry@google.comfbfcd562012-08-23 18:09:54 +000042 @param count Must be >=2. The number of colors (and pos if not NULL) entries.
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 @param mode The tiling mode
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 */
reed@google.com3d3a8602013-05-24 14:58:44 +000045 static SkShader* CreateLinear(const SkPoint pts[2],
46 const SkColor colors[], const SkScalar pos[], int count,
47 SkShader::TileMode mode,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000048 uint32_t flags, const SkMatrix* localMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +000049
commit-bot@chromium.org7b171522014-05-19 15:46:09 +000050 static SkShader* CreateLinear(const SkPoint pts[2],
51 const SkColor colors[], const SkScalar pos[], int count,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000052 SkShader::TileMode mode) {
53 return CreateLinear(pts, colors, pos, count, mode, 0, NULL);
commit-bot@chromium.org7b171522014-05-19 15:46:09 +000054 }
55
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 /** Returns a shader that generates a radial gradient given the center and radius.
57 <p />
58 CreateRadial returns a shader with a reference count of 1.
59 The caller should decrement the shader's reference count when done with the shader.
60 It is an error for colorCount to be < 2, or for radius to be <= 0.
61 @param center The center of the circle for this gradient
62 @param radius Must be positive. The radius of the circle for this gradient
63 @param colors The array[count] of colors, to be distributed between the center and edge of the circle
64 @param pos May be NULL. The array[count] of SkScalars, or NULL, of the relative position of
65 each corresponding color in the colors array. If this is NULL,
66 the the colors are distributed evenly between the center and edge of the circle.
67 If this is not null, the values must begin with 0, end with 1.0, and
68 intermediate values must be strictly increasing.
69 @param count Must be >= 2. The number of colors (and pos if not NULL) entries
70 @param mode The tiling mode
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 */
reed@google.com3d3a8602013-05-24 14:58:44 +000072 static SkShader* CreateRadial(const SkPoint& center, SkScalar radius,
73 const SkColor colors[], const SkScalar pos[], int count,
74 SkShader::TileMode mode,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000075 uint32_t flags, const SkMatrix* localMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +000076
commit-bot@chromium.org7b171522014-05-19 15:46:09 +000077 static SkShader* CreateRadial(const SkPoint& center, SkScalar radius,
78 const SkColor colors[], const SkScalar pos[], int count,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000079 SkShader::TileMode mode) {
80 return CreateRadial(center, radius, colors, pos, count, mode, 0, NULL);
commit-bot@chromium.org7b171522014-05-19 15:46:09 +000081 }
82
reed@google.comcb7be692012-06-06 20:31:56 +000083 /**
84 * Returns a shader that generates a conical gradient given two circles, or
85 * returns NULL if the inputs are invalid. The gradient interprets the
86 * two circles according to the following HTML spec.
87 * http://dev.w3.org/html5/2dcontext/#dom-context-2d-createradialgradient
88 */
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000089 static SkShader* CreateTwoPointConical(const SkPoint& start, SkScalar startRadius,
90 const SkPoint& end, SkScalar endRadius,
91 const SkColor colors[], const SkScalar pos[], int count,
reed@google.com3d3a8602013-05-24 14:58:44 +000092 SkShader::TileMode mode,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000093 uint32_t flags, const SkMatrix* localMatrix);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000094
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000095 static SkShader* CreateTwoPointConical(const SkPoint& start, SkScalar startRadius,
96 const SkPoint& end, SkScalar endRadius,
97 const SkColor colors[], const SkScalar pos[], int count,
98 SkShader::TileMode mode) {
commit-bot@chromium.org7b171522014-05-19 15:46:09 +000099 return CreateTwoPointConical(start, startRadius, end, endRadius, colors, pos, count, mode,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000100 0, NULL);
commit-bot@chromium.org7b171522014-05-19 15:46:09 +0000101 }
102
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 /** Returns a shader that generates a sweep gradient given a center.
104 <p />
senorblanco@chromium.org7ef071f2009-09-22 17:25:29 +0000105 CreateSweep returns a shader with a reference count of 1.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 The caller should decrement the shader's reference count when done with the shader.
107 It is an error for colorCount to be < 2.
108 @param cx The X coordinate of the center of the sweep
109 @param cx The Y coordinate of the center of the sweep
110 @param colors The array[count] of colors, to be distributed around the center.
111 @param pos May be NULL. The array[count] of SkScalars, or NULL, of the relative position of
112 each corresponding color in the colors array. If this is NULL,
113 the the colors are distributed evenly between the center and edge of the circle.
114 If this is not null, the values must begin with 0, end with 1.0, and
115 intermediate values must be strictly increasing.
116 @param count Must be >= 2. The number of colors (and pos if not NULL) entries
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 */
118 static SkShader* CreateSweep(SkScalar cx, SkScalar cy,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000119 const SkColor colors[], const SkScalar pos[], int count,
120 uint32_t flags, const SkMatrix* localMatrix);
caryclark@google.comd26147a2011-12-15 14:16:43 +0000121
commit-bot@chromium.org7b171522014-05-19 15:46:09 +0000122 static SkShader* CreateSweep(SkScalar cx, SkScalar cy,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000123 const SkColor colors[], const SkScalar pos[], int count) {
124 return CreateSweep(cx, cy, colors, pos, count, 0, NULL);
commit-bot@chromium.org7b171522014-05-19 15:46:09 +0000125 }
126
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000127 SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128};
129
130#endif