BrushCreateLinearGradient(Single, Color32, Color32, WorkingColorSpace) Method

Creates a linear gradient Brush that blends from one color to another along a straight line. The line is specified by an angle only, whereas the actual start and end points are determined by the bounding rectangle of the current Path in each session.

See the online help for an example with image.

Definition

Namespace: KGySoft.Drawing.Shapes
Assembly: KGySoft.Drawing.Core (in KGySoft.Drawing.Core.dll) Version: 10.0.0-rc.1
C#
public static Brush CreateLinearGradient(
	float angle,
	Color32 startColor,
	Color32 endColor,
	WorkingColorSpace workingColorSpace = WorkingColorSpace.Default
)

Parameters

angle  Single
The angle of the gradient line in degrees. The angle is measured clockwise from the positive x-axis.
startColor  Color32
The starting color of the gradient.
endColor  Color32
The end color of the gradient.
workingColorSpace  WorkingColorSpace  (Optional)
Determines the color space in which the gradient is calculated. This parameter is optional.
Default value: Default, which means Srgb for this overload.

Return Value

Brush
A linear gradient Brush that uses the specified parameters.

Remarks

This overload creates a gradient with dynamic coordinates, applying the gradient automatically to the bounds of each Path. To use fix coordinates instead, use the overloads with a startPoint and endPoint parameters, such as the CreateLinearGradient(PointF, PointF, Color32, Color32, GradientWrapMode, WorkingColorSpace) overload.

The workingColorSpace parameter affects only the color interpolation, not the blending with the background, which is determined by the color space of the target bitmap data (or by DrawingOptions.Quantizer, if specified).

In this overload the default value of workingColorSpace maps to WorkingColorSpace.Srgb, because the Color32 type represents a color in the sRGB color space. When creating a monochromatic gradient, specifying Srgb can be better, because then the perceived mid-tone brightness will be at the middle of the gradient. Otherwise, using Linear may be more appropriate, so the transition between the colors will seem more "natural".

Examples

The following example demonstrates how to create a horizontal linear gradient brush that blends from white to black, adjusted always to the currently filled shape:

C#
public IReadWriteBitmapData CreateBitmap()
{
    var bitmap = BitmapDataFactory.CreateBitmapData(100, 150);
    bitmap.Clear(Color.Cyan);

    // Zero angle means horizontal gradient
    var brush = Brush.CreateLinearGradient(angle: 0f, Color.White, Color.Black);

    // Filling the same ellipse three times with different offsets
    foreach (int offset in new[] { -50, 0, 50 })
    {
        var options = new DrawingOptions
        {
            AntiAliasing = true,
            Transformation = TransformationMatrix.CreateTranslation(offset, offset + 50)
        };

        bitmap.FillEllipse(brush, new Rectangle(0, 0, 100, 50), options);
    }

    return bitmap;
}

The example above produces the result below. Note that the gradient always starts from the left edge of the ellipse and ends at the right edge, regardless of the ellipse's location. As a contrast, see the examples at the GradientWrapMode enumeration, where the gradients always use specific start and end points.
Three ellipses filled with horizontal linear gradient from white to black.

See Also