Framework / Presentation Layer / Graphics / Appearance Styles / Fill Styles / Advanced Gradient Fill Style

In This Topic
    Advanced Gradient Fill Style
    In This Topic

    Advanced gradients develop the idea of simple gradients further by allowing you to create gradients based on circles, lines and rectangles. An advanced gradient consists of a background color, which defines the default color of the gradient and an arbitrary number of points. Each point has properties for shape, color, position, intensity and angle. Advanced gradients are represented by an instance the NAdvancedGradientFillStyle class.

    The following picture shows an advanced gradient based on three points with white background color (the points have line, circle and rectangle shapes):

     Creating Custom Advanced Gradients

    To produce a similar advanced gradient style in your program you'll have to write the following code:

    C#
    Copy Code
    NAdvancedGradientFillStyle gradient = new NAdvancedGradientFillStyle();
    
    gradient.BackgroundColor = Color.White;
    gradient.Points.Add(new NAdvancedGradientPoint(Color.Red, 14, 16, 0, 70, AGPointShape.Circle));
    gradient.Points.Add(new NAdvancedGradientPoint(Color.Yellow, 84, 36, 25, 70, AGPointShape.Rectangle));
    gradient.Points.Add(new NAdvancedGradientPoint(Color.Magenta, 60, 89, 0, 50, AGPointShape.Line));
    
    someObject.FillStyle = gradient;
    
    Visual Basic
    Copy Code
    Dim gradient As New NAdvancedGradientFillStyle
    
    gradient.BackgroundColor = Color.White
    gradient.Points.Add(New NAdvancedGradientPoint(Color.Red, 14, 16, 0, 70, AGPointShape.Circle))
    gradient.Points.Add(New NAdvancedGradientPoint(Color.Yellow, 84, 36, 25, 70, AGPointShape.Rectangle))
    gradient.Points.Add(New NAdvancedGradientPoint(Color.Magenta, 60, 89, 0, 50, AGPointShape.Line))
    
    someObject.FillStyle = gradient
    

    Now let's investigate each line in detail in order to see the purpose behind it. The first line of code:

    C#
    Copy Code
    NAdvancedGradientFillStyle gradient = new NAdvancedGradientFillStyle();
    
    Visual Basic
    Copy Code
    Dim gradient As New NAdvancedGradientFillStyle
    

    creates a new NAdvancedGradientFillStyle object, which is used as a description of the advanced gradient. This object contains a collection of advanced gradient points as well as a color for the gradient background. After we create the gradient fill object we modify the background color:

    C#
    Copy Code
    gradient.BackgroundColor = Color.White;
    
    Visual Basic
    Copy Code
    gradient.BackgroundColor = Color.White
    

    and add some gradient points with different position, shape and intensity:

    C#
    Copy Code
    gradient.Points.Add(new NAdvancedGradientPoint(Color.Red, 14, 16, 0, 70, AGPointShape.Circle));
    gradient.Points.Add(new NAdvancedGradientPoint(Color.Yellow, 84, 36, 25, 70, AGPointShape.Rectangle));
    gradient.Points.Add(new NAdvancedGradientPoint(Color.Magenta, 60, 89, 0, 50, AGPointShape.Line));
    
    Visual Basic
    Copy Code
    gradient.Points.Add(New NAdvancedGradientPoint(Color.Red, 14, 16, 0, 70, AGPointShape.Circle))
    gradient.Points.Add(New NAdvancedGradientPoint(Color.Yellow, 84, 36, 25, 70, AGPointShape.Rectangle))
    gradient.Points.Add(New NAdvancedGradientPoint(Color.Magenta, 60, 89, 0, 50, AGPointShape.Line))
    

    The NAdvancedGradientPoint constructor accepts 6 parameters. The first one is the color of the point. The gradient is merging this color with the other point's colors for each pixel of the gradient by calculating the distance from the pixel to the point and diminishing the point color influence in the resulting image as this distance grows.

    The color is followed by the position of the point in the range [0, 100] where a position of (0, 0) means the upper left corner, whereas (100, 100) means the lower right corner. The point angle determines the tilting of the point shape and is used only if the point shape is line or rectangle. The point intensity controls the influence of the point in the resulting gradient and ranges in the interval [0, 100]. The last parameter is the shape of the point and it can accept a value from the AGPointShape enumeration e.g. Circle, Line or Rectangle.

    The NAdvancedGradientPoint object also exposes the above parameters as properties so you can create an advanced gradient point with the default constructor and afterwards modify its properties.

    The last row of the example applies the advanced gradient to a fill effect object. The visual element that is associated with that fill effect will be filled with the advanced gradient.

    C#
    Copy Code
    someObject.FillStyle = gradient;
    
    Visual Basic
    Copy Code
    someObject.FillStyle = gradient
    
     Predefined Advanced Gradients

    Now after we covered the steps needed to create your own advanced gradients lets take a look at the predefined advanced gradients. There are 24 types of predefined advanced gradient schemes each of them having 15 variants (a total of 360 predefined advanced gradients). You can load a predefined advanced gradient by using the second NAdvancedGradientFillStyle object constructor accepting an AdvancedGradientScheme and variant (int). The following line of code will load the calm water gradient scheme with variant 11:

    C#
    Copy Code
    someObject.FillStyle = new NAdvancedGradientFillStyle(AdvancedGradientScheme.CalmWater1, 11);
    
    Visual Basic
    Copy Code
    someObject.FillStyle = new NAdvancedGradientFillStyle(AdvancedGradientScheme.CalmWater1, 11)
    

    After you load a predefined advanced gradient scheme, you can afterwards manually modify it, by altering the properties of the points or the background color of the gradient.

    Showing all the possible predefined gradients will obviously take too much space in the help file, so the following table lists only one gradient from each scheme:

    Name Preview Name Preview
    White On Black Ocean 3
    White On Red Ocean 4
    White On Green Calm Water 1
    White On Blue Calm Water 2
    Red Desert 1
    Green Desert 2
    Blue Fire 1
    Sunset Fire 2
    Night Fall Fog 1
    Night Fall 2 Fog 2
    Ocean 1 Mahagony 1
    Ocean 2 Mahagony 2
    See Also