VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Function to perform 24-bit alpha blending between two 24-bit colors. Also provides function using G

by Michael A. Seel (4 Submissions)
Category: Graphics
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Wed 15th November 2000
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Function to perform 24-bit alpha blending between two 24-bit colors. Also provides function using GetPixel and SetPixel API calls to blend

API Declarations


Public Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long


Rate Function to perform 24-bit alpha blending between two 24-bit colors. Also provides function using G



    ' SrcColor: "Top" Color
    ' DstColor: "Bottom" Color
    ' Alpha: Alpha transparency levels (24-bit)
    ' ** Alpha is used as a long in order to specify Red, Green, and Blue alpha
    ' ** transparency levels (24-bit). To use a single-byte (8-bit) alpha transparency
    ' ** level, use the Simplify function to convert.
    
    Dim bSrcR As Byte, bSrcG As Byte, bSrcB As Byte
    Dim bDstR As Byte, bDstG As Byte, bDstB As Byte
    Dim bAlpR As Byte, bAlpG As Byte, bAlpB As Byte
    Dim bResR As Byte, bResG As Byte, bResB As Byte
    
    ' Split colors and alpha into RGB components

    bSrcR = CByte(SrcColor And 255)
    bSrcG = CByte(Int(SrcColor / 256) And 255)
    bSrcB = CByte(Int(SrcColor / 65536) And 255)
    
    bDstR = CByte(DstColor And 255)
    bDstG = CByte(Int(DstColor / 256) And 255)
    bDstB = CByte(Int(DstColor / 65536) And 255)
    
    bAlpR = CByte(Alpha And 255)
    bAlpG = CByte(Int(Alpha / 256) And 255)
    bAlpB = CByte(Int(Alpha / 65536) And 255)
    
    ' Calculate 24-bt alpha values
    bResR = CInt(bDstR) + ((CInt(bSrcR) - CInt(bDstR)) * (bAlpR / 255))
    bResG = CInt(bDstG) + ((CInt(bSrcG) - CInt(bDstG)) * (bAlpG / 255))
    bResB = CInt(bDstB) + ((CInt(bSrcB) - CInt(bDstB)) * (bAlpB / 255))
    
    ' Return blended 24-bit color
    Blend = RGB(bResR, bResG, bResB)
End Function

Public Function BlendPixels(ByVal SrcDC As Long, ByVal SrcX As Integer, ByVal SrcY As Integer, ByVal DstDC As Long, ByVal DstX As Integer, ByVal DstY As Integer, ByVal Alpha As Long) As Long
    ' Blends the color pixel SrcX, SrcY from SrcDC with the color of
    ' pixel DstX, DstY from DstDC and paints it to pixel DstX, DstY of DstDC
    ' SrcDC: hDC of image to use as source pixel in blend
    ' DstDC: hDC of image to use as destination in blend and to paint output to
    BlendPixels = SetPixel(DstDC, DstX, DstY, Blend(GetPixel(SrcDC, SrcX, SrcY), GetPixel(DstDC, DstX, DstY), Alpha))
End Function

Public Function Simplify(ByVal Alpha As Byte) As Long
    ' Converts an 8-bit alpha transparency level to 24-bit (RGB) alpha transparency levels
    Simplify = RGB(Alpha, Alpha, Alpha)
End Function


Download this snippet    Add to My Saved Code

Function to perform 24-bit alpha blending between two 24-bit colors. Also provides function using G Comments

No comments have been posted about Function to perform 24-bit alpha blending between two 24-bit colors. Also provides function using G. Why not be the first to post a comment about Function to perform 24-bit alpha blending between two 24-bit colors. Also provides function using G.

Post your comment

Subject:
Message:
0/1000 characters