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 13 class SkUnitMapper; 14 15 /** \class SkGradientShader 16 17 SkGradientShader hosts factories for creating subclasses of SkShader that 18 render linear and radial gradients. 19 */ 20 class SK_API SkGradientShader { 21 public: 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 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 */ 71 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); 76 77 /** 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, 104 SkUnitMapper* mapper = NULL, 105 uint32_t flags = 0); 106 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, 114 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); 122 123 /** Returns a shader that generates a sweep gradient given a center. 124 <p /> 125 CreateSweep returns a shader with a reference count of 1. 126 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[], 141 int count, SkUnitMapper* mapper = NULL, 142 uint32_t flags = 0); 143 144 SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() 145 }; 146 147 #endif 148