Library code snippets

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!

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of Creating color degradations with any two colors.

Leave a comment

Sign in or Join us (it's free).

AddThis

Related discussion