Advertisement
tic

CreateGradient and Supporting Procedures

tic
Oct 6th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.59 KB | None | 0 0
  1. Private Sub CreateGradient()
  2.     Dim newGraphics As Graphics
  3.     Dim pgb As PathGradientBrush
  4.    
  5.     Try
  6.         ' Create a new PathGradientBrush, supplying an array of points created
  7.         ' by calling the GetPoints method.
  8.         pgb = New PathGradientBrush(GetPoints(radius, New Point(radius, radius)))
  9.  
  10.         ' Set the various properties. Note the SurroundColors property, which
  11.         ' contains an array of points, in a one-to-one relationship with the
  12.         ' points that created the gradient.
  13.         pgb.CenterColor = Color.White
  14.         pgb.CenterPoint = New PointF(radius, radius)
  15.         pgb.SurroundColors = GetColors()
  16.  
  17.         ' Create a new bitmap containing the color wheel gradient, so the
  18.         ' code only needs to do all this work once. Later code uses the bitmap
  19.         ' rather than recreating the gradient.
  20.         colorImage = New Bitmap(colorRectangle.Width, colorRectangle.Height, PixelFormat.Format32bppArgb)
  21.         newGraphics = Graphics.FromImage(colorImage)
  22.         newGraphics.FillEllipse(pgb, 0, 0, colorRectangle.Width, colorRectangle.Height)
  23.     Finally
  24.         If Not pgb Is Nothing Then
  25.             pgb.Dispose()
  26.         End If
  27.  
  28.         If Not newGraphics Is Nothing Then
  29.         newGraphics.Dispose()
  30.         End If
  31.     End Try
  32. End Sub
  33.  
  34. Private Function GetColors() As Color()
  35.     ' Create an array of COLOR_COUNT colors, looping through all the hues
  36.     ' between 0 and 255, broken into COLOR_COUNT intervals. HSV is
  37.     ' particularly well suited for this because the only value that changes
  38.     ' as you create colors is the Hue.
  39.     Dim Colors(COLOR_COUNT - 1) As Color
  40.     Dim i As Integer
  41.    
  42.     For i = 0 To COLOR_COUNT - 1
  43.         Colors(i) = ColorHandler.HSVtoColor(i * 255 \ COLOR_COUNT, 255, 255)
  44.     Next
  45.     Return Colors
  46. End Function
  47.  
  48. Private Function GetPoints(ByVal radius As Double, ByVal centerPoint As Point) _ As Point()
  49.     ' Generate the array of points that describe the locations of the
  50.     ' COLOR_COUNT colors to be displayed on the color wheel.
  51.     Dim Points(COLOR_COUNT - 1) As Point
  52.     Dim i As Integer
  53.    
  54.     For i = 0 To COLOR_COUNT - 1
  55.         Points(i) = GetPoint(i * 360 / COLOR_COUNT, radius, centerPoint)
  56.     Next
  57.     Return Points
  58. End Function
  59.  
  60. Private Function GetPoint(ByVal degrees As Double, ByVal radius As Double, ByVal centerPoint As Point) As Point
  61.     ' Given the center of a circle and its radius, along with the angle
  62.     ' corresponding to the point, find the coordinates. In other words,
  63.     ' convert from polar to rectangular coordinates.
  64.     Dim radians As Double = degrees / DEGREES_PER_RADIAN
  65.     Return New Point(CInt(centerPoint.X + Math.Floor(radius * Math.Cos(radians))), CInt(centerPoint.Y - Math.Floor(radius * Math.Sin(radians))))
  66. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement