''' <summary>
''' A gradient class. You can find the color on gradient, of two colors, at any give point.
''' </summary>
Public Class Gradient

    Private ccolStart, ccolEnd As Color

    ''' <summary>
    ''' Gets or sets the start color.
    ''' </summary>
    ''' <value>The start color.</value>
    Public Property StartColor() As Color
        Get
            Return ccolStart
        End Get
        Set(ByVal Value As Color)
            ccolStart = Value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the end color.
    ''' </summary>
    ''' <value>The end color.</value>
    Public Property EndColor() As Color
        Get
            Return ccolEnd
        End Get
        Set(ByVal Value As Color)
            ccolEnd = Value
        End Set
    End Property

    ''' <summary>
    ''' Colors at the given point in percentage.
    ''' </summary>
    ''' <param name="pPer">The point in percentage.</param>
    ''' <returns>Color value</returns>
    Public Function ColorAt(ByVal pPer As Single) As Color
        If pPer < -1 Or pPer > 1 Then pPer = pPer * 100
        If pPer < 0 Then pPer = pPer Mod 100

        If pPer < 0 Then
            pPer += 100
        ElseIf pPer > 100 Then
            pPer = pPer Mod 100
        End If
        If pPer > 1 Then pPer = pPer / 100

        Dim A, R, G, B As Byte

        A = GetByte(ccolStart.A, ccolEnd.A, pPer)
        R = GetByte(ccolStart.R, ccolEnd.R, pPer)
        G = GetByte(ccolStart.G, ccolEnd.G, pPer)
        B = GetByte(ccolStart.B, ccolEnd.B, pPer)

        Return Color.FromArgb(A, R, G, B)
    End Function

    Private Function GetByte(ByVal b1 As Byte, ByVal b2 As Byte, ByVal p As Single) As Byte
        Dim b As Integer
        If b1 = b2 Then
            b = b1
        Else
            b = CInt((CInt(b2) - CInt(b1)) * p) + CInt(b1)
        End If

        Return CByte(b)
    End Function
End Class
