' Blends two colors based on the alpha value of the top color
Private Sub BlendColors(topColor As Int, bottomColor As Int) As Int()
Dim topARGB() As Int = GetARGB(topColor)
Dim bottomARGB() As Int = GetARGB(bottomColor)
Dim blended(3) As Int
Dim alphaTop As Float = topARGB(0) / 255.0
Dim inverseAlphaTop As Float = 1 - alphaTop
For i = 1 To 3
blended(i) = topARGB(i) * alphaTop + bottomARGB(i) * inverseAlphaTop
Next
Return blended ' Return the blended RGB values; Alpha is ignored for luminance calculation
End Sub
' Modified Luminance function to accept RGB values directly
Private Sub LuminanceFromRGB(rgb() As Int) As Float
Dim a(3) As Float
For i = 1 To 3
Dim v As Float = rgb(i) / 255
If v <= 0.03928 Then
a(i - 1) = v / 12.92
Else
a(i - 1) = Power((v + 0.055) / 1.055, 2.4)
End If
Next
Return a(0) * 0.2126 + a(1) * 0.7152 + a(2) * 0.0722
End Sub
' Adjust your button click event or wherever you calculate contrast
Sub Button1_Click
' Example colors (These would be dynamically determined in your actual code)
Dim labelColor As Int = 0x0A969696 ' ARGB 10, 150, 150, 150
Dim panelColor As Int = 0xFFFFFFFF ' ARGB 255, 255, 255, 255
' Blend the label color with the panel color
Dim blendedLabelRGB() As Int = BlendColors(labelColor, panelColor)
' Calculate luminance of the blended color
Dim lumLabel As Float = LuminanceFromRGB(blendedLabelRGB)
Dim lumPanel As Float = Luminance(panelColor) ' Panel color is already opaque, so direct luminance calculation is fine
' Continue with contrast calculation as before
Dim contrastRatio As Float = ContrastUsingLuminance(lumLabel, lumPanel)
Log(contrastRatio)
End Sub