Requires: Direct2D.lg32
Creates and returns a Direct2D gradient brush for the current render target.
Set brObj = D2BrushGradien(type%, GS(), nElem%, x0!, y0!, x1!, y1![,Opacity!] [,rx!, ry!]
brObj | : D2Brush object |
type | : integer expression |
GS() | : array of D2D1_GRADIENT_STOP |
nElem% | : integer expression |
x0!, y0!, x1!, y1! ,Opacity!, rx!, ry! | : float expression |
The type% specifies the type of gradient brush to create. Use type% = 0 for a linear gradient brush and type% = 1 for a radial gradient brush.
For a linear gradient brush specify the gradient axis's starting point (x0, y0) and endpoint (x1,y1) within the shapes boundaries. These coordinates need to be within the shapes's position and size. Optionally, Opacity may be set to a value between 0.0 and 1.0, default is 1.0 (no opacity).
For a radial gradient brush (x0,y0) define the center of the ellipse or circle. The values for (x1,y1) define the gradient origin offset relative to center position, (0,0) starts the gradient from the center. Optionally, Opacity may be set to a value between 0.0 and 1.0, default is 1.0 (no opacity). Finally, specify the radius for the shape, rx is the x-radius of the gradient shape, ry is the y-radius of the gradient shape. For a circle both values should be the same rx = ry.
The GS() array is an array of D2Gradient structures (the gradient stop collection) that describe the colors in the brush's gradient and their locations along the gradient line. nElem% is a value greater than or equal to 1 that specifies the number of gradient stops in the GS() array (one array element is one gradient stop). D2Gradient is defined in and imported from the Direct2D library:
Type D2Gradient
position As Float
argbcolor As Long
EndType
The position member indicates the relative position of the gradient stop in the brush. This value must be in the 0.0 - 1.0 range if the gradient stop is to be seen explicitly. The argbcolor member must specify the ARGB color for the gradient stop.
Gradient stops can be specified in any order if they are at different positions. Typically, there are at least two points in a collection. For example, one point is at position 0.0f, another point is at position 1.0f, and additional points are distributed in the [0, 1] range. Where the gradient progression is beyond the range of [0, 1], the stops are stored, but may affect the gradient. Gradient stops with a position outside the [0, 1] range cannot be seen explicitly, but they can still affect the colors produced in the [0, 1] range.
The return value of D2BrushGradient is a D2Brush object that can be used with the D2DefFill command or as an argument in the drawing commands.
'
' D2BrushGradient sample
'
$Library "direct2d"
Global Object Win1RT
OpenW 1, 0, 0, 320, 260, ~15
Set Win1RT = D2GetRT(Me.hWnd, _X, _Y) ' window RenderTarget
' Create a GradientBrush for the D2PBox and D2PCircle commands,
' the gradientbrush must have the 'same size' as the shapes.
Global oLGrBrush As Object, oRGrBrush As Object
Local GS(1 .. 2) As D2Gradient ' the stop collection
GS(1).position = 0.0 ' beginning of gradient is yellow
GS(1).argbcolor = D2C_Yellow
GS(2).position = 1.0 ' end of gradient is ForestGreen
GS(2).argbcolor = D2C_ForestGreen
' Create a gradientbrush that fits in D2PBox; specify the gradient
' axis's starting point (10,45) and endpoint (80,45).
Set oLGrBrush = D2BrushGradient(0, GS(), 2, 10, 45, 80, 45) ' gradient axis
' Create a gradientbrush that fits in D2PCircle; specify the
' the center (45,120), gradient origin offset (-20,0),
' and x-radius (35) and y-radius (35), opacity = 0.5
Set oRGrBrush = D2BrushGradient(1, GS(), 2, 45, 120, -20, 0, 0.5, 35, 35)
Do
Sleep
Until Me Is Nothing
Sub Win_1_Paint
' Embed everything between D2BeginDraw/D2EndDraw
D2BeginDraw Win1RT
D2Clear ARGB(0, 255, 255, 255) ' clear with white
D2PBox 10, 10, 80, 80, oLGrBrush
D2Transform D2TF_MOVE, 20, 0 ' translate over 20,0
D2DefFill oRGrBrush
D2PCircle 45, 120, 35
D2EndDraw
EndSub
Before using D2BrushGradient() make sure the proper render target is activated using D2GetRT, D2SetRT or D2BeginDraw.
D2BrushGradient invokes ID2D1RenderTarget::CreateGradientStopCollection to create the stop collection and than ID2D1RenderTarget::CreateLinearGradientBrush or ID2D1RenderTarget::CreateRadialGradientBrush.
D2Brush, D2SetRT, D2DefFill, D2Line, D2PBox, D2PRBox, D2Ellipse, D2PCircle, D2PEllipse, ARGB
{Created by Sjouke Hamstra; Last updated: 22/03/2020 by James Gaite}