Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> Visual Basic >> Snippet

Gradient - full spectrum


Function GetColor(Zvalue As Single , ByVal Hi As Single , ByVal Low As Single , R As Integer , G As Integer , B As Integer ) As Long
'returns Long color, and RGB components, across the entire spectrum based on position of a number between two limits

Dim CRatio As Single , Exponent As Single
Exponent = 0.365
If Hi <> Low Then
   CRatio = Abs((Zvalue - Low) / (Hi - Low))
Else
   CRatio = 0
End If
   
If CRatio > 1 Then CRatio = 1
If CRatio < 0 Then CRatio = 0

Select Case CRatio
    Case Is < 0.25
       r = 0
       g = 255 * (((CRatio - 0) * 4) ^ Exponent)
       b = 255
    Case Is < 0.5
       r = 0
       g = 255
       b = 255 * ((1 - (CRatio - 0.25) * 4) ^ Exponent)
    Case Is < 0.75
       r = 255 * (((CRatio - 0.5) * 4) ^ Exponent)
       g = 255
       b = 0
    Case Else
       r = 255
       g = 255 * ((1 - (CRatio - 0.75) * 4) ^ Exponent)
       b = 0

End Select
GetColor = RGB(r, g, b)

End Function