Creating color degradations with any two colors

Page 2 of 2
  1. The Calculation
  2. Example of Use

Example of Use

Add a form to your project, called form1, then add a menu Item called mnDraw, with caption "Draw effect", then add a common dialog control called CommonDialog1, then add the following code, along with the functions in the previous page.

Private Sub mnDraw_Click()

Dim fColor As Integer
Dim arrColors() As Long
Dim steps As Variant
Dim color1 As Long
Dim color2 As Long
Dim posY As Long

   'Get color choices from user
   With CommonDialog1
       .Flags = cdlCCRGBInit
       .CancelError = True
       On Error Resume Next
       .ShowColor
       If (Err.Number <> 0) Then
           Exit Sub
       End If
       On Error GoTo 0
       color1 = .color
       On Error Resume Next
       .ShowColor
       If (Err.Number <> 0) Then
           Exit Sub
       End If
       On Error GoTo 0
       color2 = .color
   End With
   'Get amount of steps from user
   steps = InputBox("Number of steps:", , 32)
   If (steps = "") Then
       Exit Sub
   End If
   steps = CLng(steps)
   'Calculate gradient; display error message if applicable
   If Not (CalculateGradient(color1, color2, steps, arrColors)) Then
       MsgBox "An error occured while calculating the gradient!", vbExclamation
       Exit Sub
   End If
   'Get the form ready to paint horizontal lines.
   'The easiest way to do it is to use a user scalemode.
   Me.ScaleHeight = steps
   'Set starting position
   posY = Me.ScaleTop
   'Now draw boxes
   For fColor = LBound(arrColors) To UBound(arrColors)
       Me.Line (Me.ScaleLeft, posY)-(Me.ScaleWidth + Me.ScaleLeft, posY + 1), arrColors(fColor), BF
       posY = posY + 1
   Next fColor
End Sub

You could alter this to make the effect vertical, or starting from the center of the form, or alter the CalculateGradient function to introduce a weight factor to increase the gradient exponencially, or quadratic, or whatever... you name it!

You might also like...

Comments

Jose Pablo Ramirez Vargas

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Most software today is very much like an Egyptian pyramid with millions of bricks piled on top of each other, with no structural integrity, but just done by brute force and thousands of slaves” - Alan Kay