The program uses a PathGradientBrush to fill the color wheel. It's basically automatic but takes some effort to set up.
First, the program makes a GraphicsPeth object and adds an ellipse to it. This will be the ellipse occupied by the wheel.
When you make a PathGradientBrush, you specify colors for the points along the path. Unfortunately the points for an ellipse are control points not points on the ellipse themselves so the program calls the path object's Flatten method to convert the ellipse into a series of line segments. Now the points do lie along the flattened path.
The program loops through the points building an array of corresponding colors. The colors vary smoothly from red to blue to green.
The code creates a PathGradientBrush, sets the central color to be white, and assigns the array of colors to the points on the path. Finally it fills the path with the brush.
|
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
System.Windows.Forms.PaintEventArgs) Handles _
MyBase.Paint
Dim rect As New Rectangle(10, 10, 200, 200)
Dim wheel_path As New GraphicsPath
wheel_path.AddEllipse(rect)
wheel_path.Flatten()
Dim r, g, b, dr, dg, db As Single
Dim num_pts As Integer = (wheel_path.PointCount - 1) \ 3
Dim surround_colors(wheel_path.PointCount - 1) As Color
r = 255 : g = 0 : b = 0
dr = -255 / num_pts : db = 255 / num_pts
For i As Integer = 0 To num_pts - 1
surround_colors(i) = Color.FromArgb(255, r, g, b)
r += dr : b += db
Next i
r = 0 : g = 0 : b = 255
dg = 255 / num_pts : db = -255 / num_pts
For i As Integer = num_pts To 2 * num_pts - 1
surround_colors(i) = Color.FromArgb(255, r, g, b)
g += dg : b += db
Next i
r = 0 : g = 255 : b = 0
dr = 255 / (wheel_path.PointCount - 2 * num_pts) : dg = _
-255 / (wheel_path.PointCount - 2 * num_pts)
For i As Integer = 2 * num_pts To wheel_path.PointCount _
- 1
surround_colors(i) = Color.FromArgb(255, r, g, b)
r += dr : g += dg
Next i
Dim br As New PathGradientBrush(wheel_path)
br.CenterColor = Color.White
br.SurroundColors = surround_colors
e.Graphics.FillPath(br, wheel_path)
e.Graphics.DrawPath(Pens.Blue, wheel_path)
' Uncomment the following to draw the path's points.
'For i As Integer = 0 To wheel_path.PointCount - 1
' e.Graphics.DrawEllipse(Pens.Red, _
' wheel_path.PathPoints(i).X, _
' wheel_path.PathPoints(i).Y, _
' 2, 2)
'Next i
End Sub
|