Link to home
Create AccountLog in
Avatar of ericvbnet
ericvbnet

asked on

Get a color from a ColorBlend Gradient

Hello Experts,

I have a linear gradient in the form of a ColorBlend class (in VB.NET). I would like to be able to obtain the color for a given percentage of the gradient.

For example, if I have a Color.Blue at position 0 and Color.Red at position 1, how can I get the color that is, say, 37% across the gradient.

Preferably I would not have to draw the image and obtain the color from the pixel.

Thanks.
Avatar of djon2003
djon2003
Flag of Canada image

From the ColorBlend class, it cant be done. Though, drawing + get pixel is an option, another would be to calculate the gradient yourself.
Avatar of ericvbnet
ericvbnet

ASKER

Do you have any ideas on how the gradient might be calculated?
Here is a code I used in VB6 a long a time ago to draw gradient backgrounds. You should be able to figure out the necessary code from that.
Sub Fade(obj As Object, Optional vRed As Variant, _
         Optional vGreen As Variant, Optional vBlue As Variant, _
         Optional vVert As Variant, Optional vHoriz As Variant, _
         Optional vLightToDark As Variant, Optional vInverse As Variant)
    ' Donne des valeures par défaut aux paramètres optionnels
    If IsMissing(vRed) Then vRed = False
    If IsMissing(vBlue) Then vBlue = False
    If IsMissing(vGreen) Then vGreen = False
    If Not vRed And Not vGreen Then vBlue = True ' Une couleur est requise
    If IsMissing(vVert) Then vVert = False
    If IsMissing(vHoriz) Then vHoriz = Not vVert
    If IsMissing(vInverse) Then vInverse = False
    If Not vVert And Not vHoriz Then vHoriz = True ' Une orientation est
    If IsMissing(vLightToDark) Then vLightToDark = True

    ' Gestion des erreurs
    On Error Resume Next
    With obj
        'Sauve les propriétés
        Dim fAutoRedraw As Boolean, ordDrawStyle As Integer
        Dim ordDrawMode As Integer, iDrawWidth As Integer
        Dim ordScaleMode As Integer
        Dim rScaleWidth As Single, rScaleHeight As Single
        fAutoRedraw = .AutoRedraw: iDrawWidth = .DrawWidth
        ordDrawStyle = .DrawStyle: ordDrawMode = .DrawMode
        rScaleWidth = .ScaleWidth: rScaleHeight = .ScaleHeight
        ordScaleMode = .ScaleMode
        ' Une erreur est générée si une de ces propriété est manquante
        If Err Then Exit Sub
        On Error GoTo 0
        fAutoRedraw = .AutoRedraw

        ' On fixe les paramètres pour le dégradé
        .AutoRedraw = True
        .DrawWidth = 2
        .DrawStyle = vbInsideSolid: .DrawMode = vbCopyPen
        .ScaleMode = vbPixels
        .ScaleWidth = 256 * 2: .ScaleHeight = 256 * 2

        Dim clr As Long, i As Integer, X As Integer, Y As Integer, X2 As Integer, Y2 As Integer
        Dim iRed As Integer, iGreen As Integer, iBlue As Integer
         X2 = .ScaleWidth
            Y2 = .ScaleHeight
        For i = 0 To 255
            ' Fixe les couleurs des lignes
            If vLightToDark Then
                If vRed Then iRed = 255 - i
                If vBlue Then iBlue = 255 - i
                If vGreen Then iGreen = 255 - i
            Else
                If vRed Then iRed = i
                If vBlue Then iBlue = i
                If vGreen Then iGreen = i
            End If
            clr = RGB(iRed, iGreen, iBlue)
            ' Dessine chaque ligne
            If vHorizSpace > 0 Then
                obj.Line (0, Y + vHorizSpace)-(.ScaleWidth, Y + 2 + vHorizSpace), clr, BF
                Y = Y + 2
            GoTo passe
            End If
            If vInverse Then
            If vVert Then
                obj.Line (.ScaleWidth, Y2 - 2)-(Y2, 0), clr, BF
                Y2 = Y2 - 2
            End If
            If vHoriz Then
                obj.Line (X2 - 2, .ScaleHeight)-(X2, 0), clr, BF
                X2 = X2 - 2
            End If
            Else
            If vVert Then
                obj.Line (0, Y)-(.ScaleWidth, Y + 2), clr, BF
                Y = Y + 2
            End If
            If vHoriz Then
                obj.Line (X, 0)-(X + 2, .ScaleHeight), clr, BF
                X = X + 2
            End If
            End If
passe:
        Next
        ' Restore les propriétés précédentes
        .AutoRedraw = fAutoRedraw: .DrawWidth = iDrawWidth
        .DrawStyle = ordDrawStyle: .DrawMode = ordDrawMode
        .ScaleMode = ordScaleMode
        .ScaleWidth = rScaleWidth: .ScaleHeight = rScaleHeight
    End With
End Sub

Open in new window

Avatar of Nasir Razzaq
I got a solution for you.

Let's see each colors as an axis on an 3D graph. They are graduated from 0 to 255. So the axis are R, G, B.
If you want to create a gradient from color A to color B. You have to draw from the point (red1, green1, blue1) to (red2, green2, blue2). Let say from red to blue.

(PSEUDO CODE !!)
So A (255, 0, 0) and B (0, 0, 255).
diffR = A(R) - B(R) = 255 - 0 = 255
diffG= A(G) - B(G) = 0 - 0 = 0
diffB = A(B) - B(B) = 0 - 255 = -255
For percent As Integer = 0 to 100
     nextColor = (B(R) + diffR*percent, B(G) + diffG*percent, B(B) + diffB*percent)
Next

So as you can see, having the differences between each component of the two colors, you can use the next formula to have the color you want at a given percentage :
neededColor = (B(R) + diffR*percent, B(G) + diffG*percent, B(B) + diffB*percent)
Small error ....


(PSEUDO CODE !!)
So A (255, 0, 0) and B (0, 0, 255).
diffR = A(R) - B(R) = 255 - 0 = 255
diffG= A(G) - B(G) = 0 - 0 = 0
diffB = A(B) - B(B) = 0 - 255 = -255
For percent As Double = 0 to 1 Step 0.01
     nextColor = (B(R) + diffR*percent, B(G) + diffG*percent, B(B) + diffB*percent)
Next
I found this code somewhere that works really well:

    Public Function colorMix(ByVal color1 As Color, _
ByVal color2 As Color, ByVal color1prop As Double) As Color
        Dim r As Integer, g As Integer, b As Integer
        Dim ra As Double, ga As Double, ba As Double
        Dim col As Color

        Try
            ra = CDbl(color1.R) + ((CDbl(color2.R) - CInt(color1.R)) * color1prop)
            r = CInt(Math.Min(ra, 255))
            ga = CDbl(color1.G) + ((CDbl(color2.G) - CInt(color1.G)) * color1prop)
            g = CInt(Math.Min(ga, 255))
            ba = CDbl(color1.B) + ((CDbl(color2.B) - CInt(color1.B)) * color1prop)
            b = CInt(Math.Min(ba, 255))

            col = Color.FromArgb(CByte(r), CByte(g), CByte(b))
        Catch ex As Exception
            col = color1
        End Try

        Return col
    End Function
I've requested that this question be closed as follows:

Accepted answer: 0 points for ericvbnet's comment http:/Q_27428855.html#37097190

for the following reason:

Thank you djon2003 for your efforts on my behalf.
ASKER CERTIFIED SOLUTION
Avatar of djon2003
djon2003
Flag of Canada image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Fair enough. Thank you.