Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Color Gradient capture in Vb.Net

Status
Not open for further replies.

ASPNETnewbie

Programmer
May 9, 2006
93
US
I was wondering if anyone has an idea of how I can program a color-gradient value capturing function. My program will recieve 2 values as input ( StartColor and EndColor). The function will then return an array of 20 colors that exist between the two inputted colors. Can anyone help please?

AspNetNewbie
 
Try this: I just worte it and you just have to pass it the start and end colors and a number of gradients.

Code:
    Public Function GetGradients(ByVal StartColor As Drawing.Color, ByVal EndColor As Drawing.Color, ByVal NumGradients As Integer) As Drawing.Color()
        Dim clrRet(NumGradients - 1) As Drawing.Color
        Dim i As Integer
        For i = 1 To NumGradients + 2
            If i > 1 And i < NumGradients + 2 Then
                Dim ThisClr As New Drawing.Color()
                Dim iR As Integer = 0
                Dim iG As Integer = 0
                Dim iB As Integer = 0
                If StartColor.R < EndColor.R Then
                    iR = Math.Round(((CInt(EndColor.R) - CInt(StartColor.R)) / (NumGradients + 1)), 0) * i + CInt(StartColor.R)
                Else
                    iR = Math.Round(((CInt(StartColor.R) - CInt(EndColor.R)) / (NumGradients + 1)), 0) * i + CInt(EndColor.R)
                End If
                If StartColor.G < EndColor.G Then
                    iG = Math.Round(((CInt(EndColor.G) - CInt(StartColor.G)) / (NumGradients + 1)), 0) * i + CInt(StartColor.G)
                Else
                    iG = Math.Round(((CInt(StartColor.G) - CInt(EndColor.G)) / (NumGradients + 1)), 0) * i + CInt(EndColor.G)
                End If
                If StartColor.B < EndColor.B Then
                    iB = Math.Round(((CInt(EndColor.B) - CInt(StartColor.B)) / (NumGradients + 1)), 0) * i + CInt(StartColor.B)
                Else
                    iB = Math.Round(((CInt(StartColor.B) - CInt(EndColor.B)) / (NumGradients + 1)), 0) * i + CInt(EndColor.B)
                End If
                clrRet(i - 2) = Drawing.Color.FromArgb(iR, iG, iB)
            End If
        Next
        Return clrRet
    End Function

Enjoy!!! :)

Senior Software Developer
 
Thank you for the code you provided me. I did not expect someone to actually write one but thank you very much.

Could you please include an explanation on how the above code works please? I tried it but I keep running into an error stating

"ARGUEMENT EXCEPTION WAS UNHANDLED BY USER CODE: Value of '260' is not valid for 'red'. 'red' should be greater than or equal to 0 and less than or equal to 255."

and the problem seems to be on this line

Line 45: clrRet(i - 2) = Drawing.Color.FromArgb(iR, iG, iB)

could you please help a bit more ?


ASPNETNEWBIE

 
Nevermind the last post, I finally had to change the 2's to one's and got it to work. Below is the modified code for the gradient. I forgot to include that I needed the gradient to include the start and end colors.


Public Function GetGradients(ByVal StartColor As Drawing.Color, ByVal EndColor As Drawing.Color, ByVal NumGradients As Integer) As Drawing.Color()
Dim clrRet(NumGradients - 1) As Drawing.Color
Dim i As Integer
clrRet(0) = StartColor
clrRet(NumGradients - 1) = EndColor

For i = 1 To NumGradients
If i > 1 And i < NumGradients Then

Dim ThisClr As New Drawing.Color()
Dim iR As Integer = 0
Dim iG As Integer = 0
Dim iB As Integer = 0

If StartColor.R < EndColor.R Then

iR = Math.Round(((CInt(EndColor.R) - CInt(StartColor.R)) / (NumGradients + 1)), 0) * i + CInt(StartColor.R)
Else
iR = Math.Round(((CInt(StartColor.R) - CInt(EndColor.R)) / (NumGradients + 1)), 0) * i + CInt(EndColor.R)
End If

If StartColor.G < EndColor.G Then
iG = Math.Round(((CInt(EndColor.G) - CInt(StartColor.G)) / (NumGradients + 1)), 0) * i + CInt(StartColor.G)
Else
iG = Math.Round(((CInt(StartColor.G) - CInt(EndColor.G)) / (NumGradients + 1)), 0) * i + CInt(EndColor.G)
End If

If StartColor.B < EndColor.B Then
iB = Math.Round(((CInt(EndColor.B) - CInt(StartColor.B)) / (NumGradients + 1)), 0) * i + CInt(StartColor.B)
Else
iB = Math.Round(((CInt(StartColor.B) - CInt(EndColor.B)) / (NumGradients + 1)), 0) * i + CInt(EndColor.B)
End If

clrRet(i - 1) = Drawing.Color.FromArgb(iR, iG, iB)
End If
Next
Return clrRet
End Function

 
Nice Job.

The other thing I was thinking about last night is that this code may still not be quite right...

Test it by going from red to blue... and then from blue to red.

I'm curious to know if: When the RGB values of StartColor are Greater than the EndColor, if it walks the value from the start to the end... it could produce an inverted progression. If this happens then you need to change the "i" to something like "(NumGradients - i)" where the "i" is in bold red below.


If StartColor.R < EndColor.R Then
iR = Math.Round(((CInt(EndColor.R) - CInt(StartColor.R)) / (NumGradients + 1)), 0) * i + CInt(StartColor.R)
Else
iR = Math.Round(((CInt(StartColor.R) - CInt(EndColor.R)) / (NumGradients + 1)), 0) * i + CInt(EndColor.R)
End If

Senior Software Developer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top