Results 1 to 2 of 2

Thread: [RESOLVED] [VB6] - transparent opacy by pixel

  1. #1

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Resolved [RESOLVED] [VB6] - transparent opacy by pixel

    i build 1 sub for do the transparent opacy.
    and i belive that i have some errors on it, because i have bad results
    Code:
    'Opacy an image(alpha blend): finalrgb = a*rgb1 + (1-a)rgb2
    Public Sub TransparentAlphaBlend(ByRef SrcHDC As Long, ByRef DstHDC As Long, ByRef SrcWidth As Long, SrcHeight, ByRef Opacy As Integer, ByRef TransparentColor As Long)
        Dim x As Long, y As Long
        Dim lngsrcColor As Long
        Dim lngdstColor As Long
        Dim lngNewColor As Long
        Dim dblAlphaDst As Long
        Dim dblAlpha As Double, R As Long, G As Long, B As Long
        Dim SrcRed As Long, SrcBlue As Long, SrcGreen As Long
        Dim DstRed As Long, DstBlue As Long, DstGreen As Long
        
        'convert between 0-100 to 255 
        dblAlpha = Opacy * 255 / 100
        dblAlphaDst = dblAlpha - 1
        For x = 0 To SrcWidth - 1
            For y = 0 To SrcHeight - 1
                lngdstColor = GetPixel(DstHDC, x, y)
                lngsrcColor = GetPixel(SrcHDC, x, y)
                If lngsrcColor <> TransparentColor Then
                    DstRed = lngdstColor And 255
                    DstGreen = (lngdstColor And 65535) \ 256
                    DstBlue = (lngdstColor And &HFF0000) \ 65536
                    SrcRed = lngsrcColor And 255
                    SrcGreen = (lngsrcColor And 65535) \ 256
                    SrcBlue = (lngsrcColor And &HFF0000) \ 65536
                   
                    R = (dblAlpha * SrcRed) + dblAlphaDst * DstRed
                    G = (dblAlpha * SrcGreen) + dblAlphaDst * DstGreen
                    B = (dblAlpha * SrcBlue) + dblAlphaDst * DstBlue
                    lngNewColor = RGB(R, G, B)
                    SetPixelV DstHDC, x, y, lngNewColor
                End If
            Next y
        Next x
    End Sub
    maybe is my converstion or how i combine the colors i don't have sure. can anyone tell me how combine the colors for opacy, then maybe i can fix the sub. the R,G or B are more than 255 and i know that isn't a correct result(i recive overflow error on RGB function)
    can anyone advice me?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  2. #2

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: [VB6] - transparent opacy by pixel

    i found the error:
    Code:
    'Opacy an image(alpha blend): finalrgb = a*rgb1 + (1-a)rgb2
    Public Sub TransparentAlphaBlend(ByRef SrcHDC As Long, ByRef DstHDC As Long, ByRef SrcWidth As Long, SrcHeight, ByRef Opacy As Integer, ByRef TransparentColor As Long)
        Dim x As Long, y As Long
        Dim lngsrcColor As Long
        Dim lngdstColor As Long
        Dim lngNewColor As Long
        Dim dblAlphaDst As Long
        Dim dblAlpha As Double, R As Long, G As Long, B As Long
        Dim SrcRed As Long, SrcBlue As Long, SrcGreen As Long
        Dim DstRed As Long, DstBlue As Long, DstGreen As Long
         
        'convert the values from 0-100 to 0-255
        dblAlpha = Opacy * 255 / 100
        
        'now i must change the destination alpha value
        'dblAlphaDst = dblAlpha - 1
        
        For x = 0 To SrcWidth - 1
            For y = 0 To SrcHeight - 1
                'geting the source and destination colors\pixels
                lngdstColor = GetPixel(DstHDC, x, y)
                lngsrcColor = GetPixel(SrcHDC, x, y)
                
                'if source pixel is tranparentcolor then ignore it
                If lngsrcColor <> TransparentColor Then
                    'geting the destinations RGB values
                    DstRed = lngdstColor And 255
                    DstGreen = (lngdstColor And 65535) \ 256
                    DstBlue = (lngdstColor And &HFF0000) \ 65536
                    
                    'geting the source destination RGB values
                    SrcRed = lngsrcColor And 255
                    SrcGreen = (lngsrcColor And 65535) \ 256
                    SrcBlue = (lngsrcColor And &HFF0000) \ 65536
                    
                    'FinalPixel = (AlphaValue * (Source + 256 - Destination)) / 256 + Destination - AlphaValue
                    'now i must combine them with alpha values for make the opacy\fade
                    R = (dblAlpha * (SrcRed + 256 - DstRed)) / 256 + DstRed - dblAlpha
                    G = (dblAlpha * (SrcGreen + 256 - DstGreen)) / 256 + DstGreen - dblAlpha
                    B = (dblAlpha * (SrcBlue + 256 - DstBlue)) / 256 + DstBlue - dblAlpha
                    
                    lngNewColor = RGB(R, G, B)
                    'now that i have the new color, i use it
                    SetPixelV DstHDC, x, y, lngNewColor
                End If
            Next y
        Next x
    End Sub
    these is the correct formula: FinalPixel = (AlphaValue * (Source + 256 - Destination)) / 256 + Destination - AlphaValue
    (if anyone have a question how these works, just ask)
    thanks for all
    VB6 2D Sprite control

    To live is difficult, but we do it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width