|
|||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThis is a two for one article -- learn to build a brush of blended colors, and get a couple of cool color blending user controls in the process. There can be a lot of trial and error, tweaking the code and running it many times, to create a color blend. I thought it would be nice to have a control like those seen in drawing programs that would create the blend visually to get a proper placement of the colors. I will also try to explain the basics of making a color blended brush to paint with. Build-A-BlendBuilding a two color color blend is fairly simple. Using a Dim rect As New Rectangle(0, 0, 100, 100)
Using br As New LinearGradientBrush( _
rect, _
Color.White, _
Color.Black, _
LinearGradientMode.Horizontal)
'Fill the rect with the blend
g.FillRectangle(br, rect)
End Using
If you want more than two colors, a Dim blend As ColorBlend = New ColorBlend()
'Add the Array of Color
Dim bColors As Color() = New Color() { _
Color.Red, _
Color.Yellow, _
Color.Lime, _
Color.Cyan, _
Color.Blue, _
Color.Violet}
blend.Colors = bColors
'Add the Array Single (0-1) colorpoints to place each Color
Dim bPts As Single() = New Single() { _
0, _
0.327, _
0.439, _
0.61, _
0.777, _
1}
blend.Positions = bPts
Dim rect As New Rectangle(0, 0, 100, 100)
Using br As New LinearGradientBrush( _
rect, _
Color.White, _
Color.Black, _
LinearGradientMode.Horizontal)
'Blend the colors into the Brush
br.InterpolationColors = blend
'Fill the rect with the blend
g.FillRectangle(br, rect)
End Using
ColorBlender ControlThe Events
This event will fire in the Control PropertiesHere is a list of the primary properties:
Array of colors used in Array of color positions used in Type of brush used to paint the Type of linear gradient color blend. Shape of path for the Position of the center of the path Height of color blender bar. MethodsMethods to use outside the control. Currently, just one to convert the center point to another area's dimensions. Mouse EventsTrack if the cursor is over a pointer to select or add a pointer, or to adjust the position of the center point of a path. DrawingContains the routines to draw the pointers, build the brushes, and build the PaintingPaint the control to a Protected Overrides Sub _
OnPaintBackground(ByVal e As System.Windows.Forms.PaintEventArgs)
'Do Nothing
End Sub
Protected Overrides Sub _
OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
'Go through each Pointer in the collection to get
'the current Color and Position arrays
BuildBlend()
'Create a canvas to aint on the same size as the control
Dim bitmapBuffer As Bitmap = _
New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
Dim g As Graphics = Graphics.FromImage(bitmapBuffer)
g.Clear(Me.BackColor)
g.SmoothingMode = SmoothingMode.AntiAlias
' Paint the ColorBlender Bar with the Linear Brush
Dim barRect As Rectangle = _
New Rectangle(10, 0, Me.ClientSize.Width - 20, BarHeight)
Dim br As Brush = LinearBrush(barRect, LinearGradientMode.Horizontal)
g.FillRectangle(br, barRect)
' Paint the ColorBlender Sample with the chosen Brush
Dim sampleRect As Rectangle = _
New Rectangle(Me.Width - 85, BarHeight + 20, 75, 75)
If BlendGradientType = eBlendGradientType.Linear Then
br = LinearBrush(sampleRect, GetBrushMode)
Else
br = PathBrush(sampleRect)
g.DrawString(String.Format("X: {0} Y: {1}", _
BlendPathCenterPoint.X - (Width - 85), _
BlendPathCenterPoint.Y - (BarHeight + 20)), _
New Font("Arial", 8, FontStyle.Regular), _
Brushes.Black, Width - 85, BarHeight + 100)
End If
g.FillRectangle(br, sampleRect)
'Draw all the pointers in their Color,
'at their Position along the Bar
Using pn As New Pen(Color.Gray, 1)
pn.DashStyle = DashStyle.Dash
g.DrawLine(pn, 10, BarHeight + 7, _
Me.ClientSize.Width - 15, BarHeight + 7)
pn.Color = Color.Black
pn.DashStyle = DashStyle.Solid
DrawPointer(g, StartPointer.pColor, 0, StartPointer.pIsCurr)
DrawPointer(g, EndPointer.pColor, 1, EndPointer.pIsCurr)
If MiddlePointers IsNot Nothing Then
For I As Integer = 1 To MiddlePointers.Count
DrawPointer(g, MiddlePointers(I).pColor, _
MiddlePointers(I).pPos, I = CurrPointer)
Next
End If
End Using
'Draw the entire image to the control
'in one shot to eliminate flicker
e.Graphics.DrawImage(bitmapBuffer.Clone, 0, 0)
bitmapBuffer.Dispose()
br.Dispose()
g.Dispose()
End Sub
SortCollectionUse the ControlsContains the events for the controls on the ColorBoxContains the PointerThe
History
|
||||||||||||||||||||||||||||||||||||||||||||