PDF Graphics .NET supports colour gradients through gradient brushes. Two types of gradient are supported, linear and radial. The following sections describe each of the types in detail.
Graphics can be filled with a colour gradient by using a LinearGradientBrush instance. To create a linear gradient brush, specify the start and end colour as well as the start and end coordinates, as in the following example.
The gradient brush is set up to use green and yellow as the start and end colour, and flow along a line defined by the coordinates (10,10) and (60,60). This corresponds to the rectangle that is then filled, because it has a top-left corner at (10,10) and width and height of 50 points.
The coordinates for the gradient do not imply a bounding box. That is, painting a larger area with the same brush fills the entire area, just like with regular brushes. The start and end points define the scope of the colour transition, not of the brush itself:
In this sample, the rectangle is drawn larger. In its top-left hand corner, the fill colour is a gradient between yellow and green; for the rest of the rectangle, the colour is a uniform green. The rectangle extends beyond the range of the gradient.
A gradient is defined by start and end coordinates. That means its range is fixed to those coordinates. If the example above was altered to position the rectangle further down the page then it would not be painted with the gradient. If it's moved just a little bit, the gradient will seem offset. Moving the rectangle away far enough and it will be of a uniform colour:
In this example, four rectangles are painted using the same brush. Only the first rectangle receives the full gradient, since its bounding box matches that of the gradient.
To move a gradient, use transformations. The above can be re-written as follows:
Each of the four rectangles now receives a full gradient.
A radial gradient is painted with the RadialGradientBrush class. Given a bounding box, it will paint an elliptic gradient, with the start colour in the center and the end colour at the edge of the ellipse. Again, the gradient is bound to its start and end coordinates, and transformations can be used to move it around.
The previous example of four rectangles can be altered to use a radial gradient:
By default, linear and radial gradients perform a straight transition between the start and end colour; halfway down, the colour will be exactly between the two endpoint colours. It is possible to change this transition to follow a convex or concave curve, and thereby alter the gradient. The following example illustrates this: