Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Private Sub CreateGradient()
- Dim newGraphics As Graphics
- Dim pgb As PathGradientBrush
- Try
- ' Create a new PathGradientBrush, supplying an array of points created
- ' by calling the GetPoints method.
- pgb = New PathGradientBrush(GetPoints(radius, New Point(radius, radius)))
- ' Set the various properties. Note the SurroundColors property, which
- ' contains an array of points, in a one-to-one relationship with the
- ' points that created the gradient.
- pgb.CenterColor = Color.White
- pgb.CenterPoint = New PointF(radius, radius)
- pgb.SurroundColors = GetColors()
- ' Create a new bitmap containing the color wheel gradient, so the
- ' code only needs to do all this work once. Later code uses the bitmap
- ' rather than recreating the gradient.
- colorImage = New Bitmap(colorRectangle.Width, colorRectangle.Height, PixelFormat.Format32bppArgb)
- newGraphics = Graphics.FromImage(colorImage)
- newGraphics.FillEllipse(pgb, 0, 0, colorRectangle.Width, colorRectangle.Height)
- Finally
- If Not pgb Is Nothing Then
- pgb.Dispose()
- End If
- If Not newGraphics Is Nothing Then
- newGraphics.Dispose()
- End If
- End Try
- End Sub
- Private Function GetColors() As Color()
- ' Create an array of COLOR_COUNT colors, looping through all the hues
- ' between 0 and 255, broken into COLOR_COUNT intervals. HSV is
- ' particularly well suited for this because the only value that changes
- ' as you create colors is the Hue.
- Dim Colors(COLOR_COUNT - 1) As Color
- Dim i As Integer
- For i = 0 To COLOR_COUNT - 1
- Colors(i) = ColorHandler.HSVtoColor(i * 255 \ COLOR_COUNT, 255, 255)
- Next
- Return Colors
- End Function
- Private Function GetPoints(ByVal radius As Double, ByVal centerPoint As Point) _ As Point()
- ' Generate the array of points that describe the locations of the
- ' COLOR_COUNT colors to be displayed on the color wheel.
- Dim Points(COLOR_COUNT - 1) As Point
- Dim i As Integer
- For i = 0 To COLOR_COUNT - 1
- Points(i) = GetPoint(i * 360 / COLOR_COUNT, radius, centerPoint)
- Next
- Return Points
- End Function
- Private Function GetPoint(ByVal degrees As Double, ByVal radius As Double, ByVal centerPoint As Point) As Point
- ' Given the center of a circle and its radius, along with the angle
- ' corresponding to the point, find the coordinates. In other words,
- ' convert from polar to rectangular coordinates.
- Dim radians As Double = degrees / DEGREES_PER_RADIAN
- Return New Point(CInt(centerPoint.X + Math.Floor(radius * Math.Cos(radians))), CInt(centerPoint.Y - Math.Floor(radius * Math.Sin(radians))))
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement