Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Structure Pixel
- 'Variables
- Public a As Byte
- Public r As Byte
- Public g As Byte
- Public b As Byte
- 'Constructors
- Public Sub New(ByVal Alpha As Byte, ByVal Red As Byte, ByVal Green As Byte, ByVal Blue As Byte)
- a = Alpha
- r = Red
- g = Green
- b = Blue
- End Sub
- Public Sub New(ByVal Red As Byte, ByVal Green As Byte, ByVal Blue As Byte)
- a = 255
- r = Red
- g = Green
- b = Blue
- End Sub
- Shared Function CropTo(ByVal Value As Integer, ByVal Minimum As Integer, ByVal Maximum As Integer) As Integer
- Return Math.Min(Maximum, Math.Max(Minimum, Value))
- End Function
- Shared Function Blend(ByVal Pixel1 As Pixel, ByVal Pixel2 As Pixel, ByVal Mode As BlendingMode) As Pixel
- Select Case Mode
- Case Is = BlendingMode.GX_NORMAL
- Return Pixel1 & Pixel2
- Case Is = BlendingMode.GX_MULTIPLY
- Return Pixel1 * Pixel2
- Case Is = BlendingMode.GX_ADD
- Return Pixel1 + Pixel2
- Case Is = BlendingMode.GX_SCREEN
- Return gxScreen(Pixel1, Pixel2)
- Case Is = BlendingMode.GX_OVERLAY
- Return gxOverlay(Pixel1, Pixel2)
- Case Is = BlendingMode.GX_SOFTLIGHT
- Return gxSoftLight(Pixel1, Pixel2)
- Case Is = BlendingMode.GX_HARDLIGHT
- Return gxHardLight(Pixel1, Pixel2)
- End Select
- End Function
- 'GX_ADD
- Shared Operator +(ByVal Param1 As Pixel, ByVal Param2 As Pixel) As Pixel
- Dim result As Pixel
- Dim fDivisor As Double
- result.a = Math.Max(Param1.a, Param2.a)
- fDivisor = (255 - (255 - result.a) / 2)
- result.r = CropTo(CInt(Param1.r * (CDbl(Param1.a) / fDivisor)) + CInt(Param2.r * (CDbl(Param2.a) / fDivisor)), 0, 255)
- result.g = CropTo(CInt(Param1.g * (CDbl(Param1.a) / fDivisor)) + CInt(Param2.g * (CDbl(Param2.a) / fDivisor)), 0, 255)
- result.b = CropTo(CInt(Param1.b * (CDbl(Param1.a) / fDivisor)) + CInt(Param2.b * (CDbl(Param2.a) / fDivisor)), 0, 255)
- Return result
- End Operator
- Shared Function gxBlendNormal(ByVal Param1 As Integer, ByVal Param2 As Integer, ByVal Alpha As Integer) As Byte
- Return Param1 + (((Param2 - Param1) * Alpha) / 255)
- End Function
- 'GX_NORMAL
- Shared Operator &(ByVal Param1 As Pixel, ByVal Param2 As Pixel) As Pixel
- If (Param2.a = 255) Then
- Return Param2
- ElseIf (Param2.a = 0) Then
- Return Param1
- End If
- Dim result As Pixel
- result.a = Math.Max(Param1.a, Param2.a)
- result.r = Param1.r + Param2.a / 255 * (CShort(Param2.r) - Param1.r)
- result.g = Param1.g + Param2.a / 255 * (CShort(Param2.g) - Param1.g)
- result.b = Param1.b + Param2.a / 255 * (CShort(Param2.b) - Param1.b)
- Return result
- End Operator
- 'GX_MULTIPLY
- Shared Operator *(ByVal Param1 As Pixel, ByVal Param2 As Pixel) As Pixel
- Dim result As Pixel
- result.a = Math.Max(Param1.a, Param2.a)
- result.r = (Param1.r * Param2.r) / 255
- result.g = (Param1.g * Param2.g) / 255
- result.b = (Param1.b * Param2.b) / 255
- Return result
- End Operator
- 'GX_SCREEN
- Shared Function gxScreen(ByVal Param1 As Pixel, ByVal Param2 As Pixel) As Pixel
- Dim result As Pixel
- result.a = Math.Max(Param1.a, Param2.a)
- result.r = CropTo(Param1.r + Param2.r - Param1.r * Param2.r / 255, 0, 255)
- result.g = CropTo(Param1.g + Param2.g - Param1.g * Param2.g / 255, 0, 255)
- result.b = CropTo(Param1.b + Param2.b - Param1.b * Param2.b / 255, 0, 255)
- Return result
- End Function
- 'GX_OVERLAY
- Shared Function gxOverlay(ByVal Param1 As Pixel, ByVal Param2 As Pixel) As Pixel
- Dim result As Pixel
- result.a = Math.Max(Param1.a, Param2.a)
- result.r = IIf(Param2.r < 128, (2 * Param1.r * Param2.r / 255), (255 - 2 * (255 - Param1.r) * (255 - Param2.r) / 255))
- result.g = IIf(Param2.g < 128, (2 * Param1.g * Param2.g / 255), (255 - 2 * (255 - Param1.g) * (255 - Param2.g) / 255))
- result.b = IIf(Param2.b < 128, (2 * Param1.b * Param2.b / 255), (255 - 2 * (255 - Param1.b) * (255 - Param2.b) / 255))
- Return result
- End Function
- 'GX_SOFTLIGHT
- Shared Function gxSoftLight(ByVal Param1 As Pixel, ByVal Param2 As Pixel) As Pixel
- Dim result As Pixel
- result.a = Math.Max(Param1.a, Param2.a)
- result.r = IIf(Param1.r > 127.5, _
- Param2.r + (255 - Param2.r) * ((Param1.r - 127.5) / 127.5) * (0.5 - Math.Abs(Param2.r - 127.5) / 255), _
- Param2.r - Param2.r * ((127.5 - Param1.r) / 127.5) * (0.5 - Math.Abs(Param2.r - 127.5) / 255))
- result.g = IIf(Param1.g > 127.5, _
- Param2.g + (255 - Param2.g) * ((Param1.g - 127.5) / 127.5) * (0.5 - Math.Abs(Param2.g - 127.5) / 255), _
- Param2.g - Param2.g * ((127.5 - Param1.g) / 127.5) * (0.5 - Math.Abs(Param2.g - 127.5) / 255))
- result.b = IIf(Param1.b > 127.5, _
- Param2.b + (255 - Param2.b) * ((Param1.b - 127.5) / 127.5) * (0.5 - Math.Abs(Param2.b - 127.5) / 255), _
- Param2.b - Param2.b * ((127.5 - Param1.b) / 127.5) * (0.5 - Math.Abs(Param2.b - 127.5) / 255))
- Return result
- End Function
- 'GX_HARDLIGHT
- Shared Function gxHardLight(ByVal Param1 As Pixel, ByVal Param2 As Pixel) As Pixel
- Dim result As Pixel
- result.a = Math.Max(Param1.a, Param2.a)
- result.r = IIf(Param1.r > 127.5, _
- Param2.r + (255 - Param2.r) * ((Param1.r - 127.5) / 127.5), _
- Param2.r * Param1.r / 127.5)
- result.g = IIf(Param1.g > 127.5, _
- Param2.g + (255 - Param2.g) * ((Param1.g - 127.5) / 127.5), _
- Param2.g * Param1.g / 127.5)
- result.b = IIf(Param1.b > 127.5, _
- Param2.b + (255 - Param2.b) * ((Param1.b - 127.5) / 127.5), _
- Param2.b * Param1.b / 127.5)
- Return result
- End Function
- Shared Operator =(ByVal Param1 As Pixel, ByVal Param2 As Pixel) As Boolean
- If (Param1.a = Param2.a) And (Param1.r = Param2.r) And (Param1.g = Param2.g) And (Param1.b = Param2.b) Then
- Return True
- Else
- Return False
- End If
- End Operator
- Shared Operator <>(ByVal Param1 As Pixel, ByVal Param2 As Pixel) As Boolean
- If (Param1.a = Param2.a) And (Param1.r = Param2.r) And (Param1.g = Param2.g) And (Param1.b = Param2.b) Then
- Return False
- Else
- Return True
- End If
- End Operator
- End Structure
Advertisement
Add Comment
Please, Sign In to add comment