reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef SkGradientShader_DEFINED |
| 9 | #define SkGradientShader_DEFINED |
| 10 | |
| 11 | #include "SkShader.h" |
| 12 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | /** \class SkGradientShader |
| 14 | |
| 15 | SkGradientShader hosts factories for creating subclasses of SkShader that |
| 16 | render linear and radial gradients. |
| 17 | */ |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 18 | class SK_API SkGradientShader { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | public: |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 20 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | /** 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.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 42 | @param count Must be >=2. The number of colors (and pos if not NULL) entries. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | @param mode The tiling mode |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 44 | */ |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 45 | static SkShader* CreateLinear(const SkPoint pts[2], |
| 46 | const SkColor colors[], const SkScalar pos[], int count, |
| 47 | SkShader::TileMode mode, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame^] | 48 | uint32_t flags, const SkMatrix* localMatrix); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | |
commit-bot@chromium.org | 7b17152 | 2014-05-19 15:46:09 +0000 | [diff] [blame] | 50 | static SkShader* CreateLinear(const SkPoint pts[2], |
| 51 | const SkColor colors[], const SkScalar pos[], int count, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame^] | 52 | SkShader::TileMode mode) { |
| 53 | return CreateLinear(pts, colors, pos, count, mode, 0, NULL); |
commit-bot@chromium.org | 7b17152 | 2014-05-19 15:46:09 +0000 | [diff] [blame] | 54 | } |
| 55 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | /** 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | */ |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 72 | static SkShader* CreateRadial(const SkPoint& center, SkScalar radius, |
| 73 | const SkColor colors[], const SkScalar pos[], int count, |
| 74 | SkShader::TileMode mode, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame^] | 75 | uint32_t flags, const SkMatrix* localMatrix); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | |
commit-bot@chromium.org | 7b17152 | 2014-05-19 15:46:09 +0000 | [diff] [blame] | 77 | static SkShader* CreateRadial(const SkPoint& center, SkScalar radius, |
| 78 | const SkColor colors[], const SkScalar pos[], int count, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame^] | 79 | SkShader::TileMode mode) { |
| 80 | return CreateRadial(center, radius, colors, pos, count, mode, 0, NULL); |
commit-bot@chromium.org | 7b17152 | 2014-05-19 15:46:09 +0000 | [diff] [blame] | 81 | } |
| 82 | |
senorblanco@chromium.org | 7ef071f | 2009-09-22 17:25:29 +0000 | [diff] [blame] | 83 | /** Returns a shader that generates a radial gradient given the start position, start radius, end position and end radius. |
| 84 | <p /> |
| 85 | CreateTwoPointRadial returns a shader with a reference count of 1. |
| 86 | The caller should decrement the shader's reference count when done with the shader. |
| 87 | It is an error for colorCount to be < 2, for startRadius or endRadius to be < 0, or for |
| 88 | startRadius to be equal to endRadius. |
| 89 | @param start The center of the start circle for this gradient |
| 90 | @param startRadius Must be positive. The radius of the start circle for this gradient. |
| 91 | @param end The center of the end circle for this gradient |
| 92 | @param endRadius Must be positive. The radius of the end circle for this gradient. |
| 93 | @param colors The array[count] of colors, to be distributed between the center and edge of the circle |
| 94 | @param pos May be NULL. The array[count] of SkScalars, or NULL, of the relative position of |
| 95 | each corresponding color in the colors array. If this is NULL, |
| 96 | the the colors are distributed evenly between the center and edge of the circle. |
| 97 | If this is not null, the values must begin with 0, end with 1.0, and |
| 98 | intermediate values must be strictly increasing. |
| 99 | @param count Must be >= 2. The number of colors (and pos if not NULL) entries |
| 100 | @param mode The tiling mode |
senorblanco@chromium.org | 7ef071f | 2009-09-22 17:25:29 +0000 | [diff] [blame] | 101 | */ |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame^] | 102 | static SkShader* CreateTwoPointRadial(const SkPoint& start, SkScalar startRadius, |
| 103 | const SkPoint& end, SkScalar endRadius, |
| 104 | const SkColor colors[], const SkScalar pos[], int count, |
senorblanco@chromium.org | 7ef071f | 2009-09-22 17:25:29 +0000 | [diff] [blame] | 105 | SkShader::TileMode mode, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame^] | 106 | uint32_t flags, const SkMatrix* localMatrix); |
reed@google.com | cb7be69 | 2012-06-06 20:31:56 +0000 | [diff] [blame] | 107 | |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame^] | 108 | static SkShader* CreateTwoPointRadial(const SkPoint& start, SkScalar startRadius, |
| 109 | const SkPoint& end, SkScalar endRadius, |
| 110 | const SkColor colors[], const SkScalar pos[], int count, |
| 111 | SkShader::TileMode mode) { |
commit-bot@chromium.org | 7b17152 | 2014-05-19 15:46:09 +0000 | [diff] [blame] | 112 | return CreateTwoPointRadial(start, startRadius, end, endRadius, colors, pos, count, mode, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame^] | 113 | 0, NULL); |
commit-bot@chromium.org | 7b17152 | 2014-05-19 15:46:09 +0000 | [diff] [blame] | 114 | } |
| 115 | |
reed@google.com | cb7be69 | 2012-06-06 20:31:56 +0000 | [diff] [blame] | 116 | /** |
| 117 | * Returns a shader that generates a conical gradient given two circles, or |
| 118 | * returns NULL if the inputs are invalid. The gradient interprets the |
| 119 | * two circles according to the following HTML spec. |
| 120 | * http://dev.w3.org/html5/2dcontext/#dom-context-2d-createradialgradient |
| 121 | */ |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame^] | 122 | static SkShader* CreateTwoPointConical(const SkPoint& start, SkScalar startRadius, |
| 123 | const SkPoint& end, SkScalar endRadius, |
| 124 | const SkColor colors[], const SkScalar pos[], int count, |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 125 | SkShader::TileMode mode, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame^] | 126 | uint32_t flags, const SkMatrix* localMatrix); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 127 | |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame^] | 128 | static SkShader* CreateTwoPointConical(const SkPoint& start, SkScalar startRadius, |
| 129 | const SkPoint& end, SkScalar endRadius, |
| 130 | const SkColor colors[], const SkScalar pos[], int count, |
| 131 | SkShader::TileMode mode) { |
commit-bot@chromium.org | 7b17152 | 2014-05-19 15:46:09 +0000 | [diff] [blame] | 132 | return CreateTwoPointConical(start, startRadius, end, endRadius, colors, pos, count, mode, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame^] | 133 | 0, NULL); |
commit-bot@chromium.org | 7b17152 | 2014-05-19 15:46:09 +0000 | [diff] [blame] | 134 | } |
| 135 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 136 | /** Returns a shader that generates a sweep gradient given a center. |
| 137 | <p /> |
senorblanco@chromium.org | 7ef071f | 2009-09-22 17:25:29 +0000 | [diff] [blame] | 138 | CreateSweep returns a shader with a reference count of 1. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 139 | The caller should decrement the shader's reference count when done with the shader. |
| 140 | It is an error for colorCount to be < 2. |
| 141 | @param cx The X coordinate of the center of the sweep |
| 142 | @param cx The Y coordinate of the center of the sweep |
| 143 | @param colors The array[count] of colors, to be distributed around the center. |
| 144 | @param pos May be NULL. The array[count] of SkScalars, or NULL, of the relative position of |
| 145 | each corresponding color in the colors array. If this is NULL, |
| 146 | the the colors are distributed evenly between the center and edge of the circle. |
| 147 | If this is not null, the values must begin with 0, end with 1.0, and |
| 148 | intermediate values must be strictly increasing. |
| 149 | @param count Must be >= 2. The number of colors (and pos if not NULL) entries |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 150 | */ |
| 151 | static SkShader* CreateSweep(SkScalar cx, SkScalar cy, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame^] | 152 | const SkColor colors[], const SkScalar pos[], int count, |
| 153 | uint32_t flags, const SkMatrix* localMatrix); |
caryclark@google.com | d26147a | 2011-12-15 14:16:43 +0000 | [diff] [blame] | 154 | |
commit-bot@chromium.org | 7b17152 | 2014-05-19 15:46:09 +0000 | [diff] [blame] | 155 | static SkShader* CreateSweep(SkScalar cx, SkScalar cy, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame^] | 156 | const SkColor colors[], const SkScalar pos[], int count) { |
| 157 | return CreateSweep(cx, cy, colors, pos, count, 0, NULL); |
commit-bot@chromium.org | 7b17152 | 2014-05-19 15:46:09 +0000 | [diff] [blame] | 158 | } |
| 159 | |
djsollen@google.com | a2ca41e | 2012-03-23 19:00:34 +0000 | [diff] [blame] | 160 | SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | #endif |