BugInTheSYS

gxType

Oct 2nd, 2011
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.40 KB | None | 0 0
  1. Public Structure Pixel
  2.     'Variables
  3.     Public a As Byte
  4.     Public r As Byte
  5.     Public g As Byte
  6.     Public b As Byte
  7.  
  8.     'Constructors
  9.     Public Sub New(ByVal Alpha As Byte, ByVal Red As Byte, ByVal Green As Byte, ByVal Blue As Byte)
  10.       a = Alpha
  11.       r = Red
  12.       g = Green
  13.       b = Blue
  14.     End Sub
  15.  
  16.     Public Sub New(ByVal Red As Byte, ByVal Green As Byte, ByVal Blue As Byte)
  17.       a = 255
  18.       r = Red
  19.       g = Green
  20.       b = Blue
  21.     End Sub
  22.  
  23.     Shared Function CropTo(ByVal Value As Integer, ByVal Minimum As Integer, ByVal Maximum As Integer) As Integer
  24.       Return Math.Min(Maximum, Math.Max(Minimum, Value))
  25.     End Function
  26.  
  27.     Shared Function Blend(ByVal Pixel1 As Pixel, ByVal Pixel2 As Pixel, ByVal Mode As BlendingMode) As Pixel
  28.       Select Case Mode
  29.         Case Is = BlendingMode.GX_NORMAL
  30.           Return Pixel1 & Pixel2
  31.         Case Is = BlendingMode.GX_MULTIPLY
  32.           Return Pixel1 * Pixel2
  33.         Case Is = BlendingMode.GX_ADD
  34.           Return Pixel1 + Pixel2
  35.         Case Is = BlendingMode.GX_SCREEN
  36.           Return gxScreen(Pixel1, Pixel2)
  37.         Case Is = BlendingMode.GX_OVERLAY
  38.           Return gxOverlay(Pixel1, Pixel2)
  39.         Case Is = BlendingMode.GX_SOFTLIGHT
  40.           Return gxSoftLight(Pixel1, Pixel2)
  41.         Case Is = BlendingMode.GX_HARDLIGHT
  42.           Return gxHardLight(Pixel1, Pixel2)
  43.       End Select
  44.     End Function
  45.  
  46.     'GX_ADD
  47.     Shared Operator +(ByVal Param1 As Pixel, ByVal Param2 As Pixel) As Pixel
  48.       Dim result As Pixel
  49.       Dim fDivisor As Double
  50.  
  51.       result.a = Math.Max(Param1.a, Param2.a)
  52.       fDivisor = (255 - (255 - result.a) / 2)
  53.       result.r = CropTo(CInt(Param1.r * (CDbl(Param1.a) / fDivisor)) + CInt(Param2.r * (CDbl(Param2.a) / fDivisor)), 0, 255)
  54.       result.g = CropTo(CInt(Param1.g * (CDbl(Param1.a) / fDivisor)) + CInt(Param2.g * (CDbl(Param2.a) / fDivisor)), 0, 255)
  55.       result.b = CropTo(CInt(Param1.b * (CDbl(Param1.a) / fDivisor)) + CInt(Param2.b * (CDbl(Param2.a) / fDivisor)), 0, 255)
  56.       Return result
  57.     End Operator
  58.  
  59.     Shared Function gxBlendNormal(ByVal Param1 As Integer, ByVal Param2 As Integer, ByVal Alpha As Integer) As Byte
  60.       Return Param1 + (((Param2 - Param1) * Alpha) / 255)
  61.     End Function
  62.  
  63.     'GX_NORMAL
  64.     Shared Operator &(ByVal Param1 As Pixel, ByVal Param2 As Pixel) As Pixel
  65.       If (Param2.a = 255) Then
  66.         Return Param2
  67.       ElseIf (Param2.a = 0) Then
  68.         Return Param1
  69.       End If
  70.  
  71.       Dim result As Pixel
  72.  
  73.       result.a = Math.Max(Param1.a, Param2.a)
  74.       result.r = Param1.r + Param2.a / 255 * (CShort(Param2.r) - Param1.r)
  75.       result.g = Param1.g + Param2.a / 255 * (CShort(Param2.g) - Param1.g)
  76.       result.b = Param1.b + Param2.a / 255 * (CShort(Param2.b) - Param1.b)
  77.  
  78.       Return result
  79.     End Operator
  80.  
  81.     'GX_MULTIPLY
  82.     Shared Operator *(ByVal Param1 As Pixel, ByVal Param2 As Pixel) As Pixel
  83.       Dim result As Pixel
  84.  
  85.       result.a = Math.Max(Param1.a, Param2.a)
  86.       result.r = (Param1.r * Param2.r) / 255
  87.       result.g = (Param1.g * Param2.g) / 255
  88.       result.b = (Param1.b * Param2.b) / 255
  89.       Return result
  90.     End Operator
  91.  
  92.     'GX_SCREEN
  93.     Shared Function gxScreen(ByVal Param1 As Pixel, ByVal Param2 As Pixel) As Pixel
  94.       Dim result As Pixel
  95.  
  96.       result.a = Math.Max(Param1.a, Param2.a)
  97.       result.r = CropTo(Param1.r + Param2.r - Param1.r * Param2.r / 255, 0, 255)
  98.       result.g = CropTo(Param1.g + Param2.g - Param1.g * Param2.g / 255, 0, 255)
  99.       result.b = CropTo(Param1.b + Param2.b - Param1.b * Param2.b / 255, 0, 255)
  100.       Return result
  101.     End Function
  102.  
  103.     'GX_OVERLAY
  104.     Shared Function gxOverlay(ByVal Param1 As Pixel, ByVal Param2 As Pixel) As Pixel
  105.       Dim result As Pixel
  106.  
  107.       result.a = Math.Max(Param1.a, Param2.a)
  108.  
  109.       result.r = IIf(Param2.r < 128, (2 * Param1.r * Param2.r / 255), (255 - 2 * (255 - Param1.r) * (255 - Param2.r) / 255))
  110.       result.g = IIf(Param2.g < 128, (2 * Param1.g * Param2.g / 255), (255 - 2 * (255 - Param1.g) * (255 - Param2.g) / 255))
  111.       result.b = IIf(Param2.b < 128, (2 * Param1.b * Param2.b / 255), (255 - 2 * (255 - Param1.b) * (255 - Param2.b) / 255))
  112.  
  113.       Return result
  114.     End Function
  115.  
  116.     'GX_SOFTLIGHT
  117.     Shared Function gxSoftLight(ByVal Param1 As Pixel, ByVal Param2 As Pixel) As Pixel
  118.       Dim result As Pixel
  119.  
  120.       result.a = Math.Max(Param1.a, Param2.a)
  121.  
  122.       result.r = IIf(Param1.r > 127.5, _
  123.         Param2.r + (255 - Param2.r) * ((Param1.r - 127.5) / 127.5) * (0.5 - Math.Abs(Param2.r - 127.5) / 255), _
  124.         Param2.r - Param2.r * ((127.5 - Param1.r) / 127.5) * (0.5 - Math.Abs(Param2.r - 127.5) / 255))
  125.       result.g = IIf(Param1.g > 127.5, _
  126.         Param2.g + (255 - Param2.g) * ((Param1.g - 127.5) / 127.5) * (0.5 - Math.Abs(Param2.g - 127.5) / 255), _
  127.         Param2.g - Param2.g * ((127.5 - Param1.g) / 127.5) * (0.5 - Math.Abs(Param2.g - 127.5) / 255))
  128.       result.b = IIf(Param1.b > 127.5, _
  129.         Param2.b + (255 - Param2.b) * ((Param1.b - 127.5) / 127.5) * (0.5 - Math.Abs(Param2.b - 127.5) / 255), _
  130.         Param2.b - Param2.b * ((127.5 - Param1.b) / 127.5) * (0.5 - Math.Abs(Param2.b - 127.5) / 255))
  131.  
  132.       Return result
  133.     End Function
  134.  
  135.     'GX_HARDLIGHT
  136.     Shared Function gxHardLight(ByVal Param1 As Pixel, ByVal Param2 As Pixel) As Pixel
  137.       Dim result As Pixel
  138.  
  139.       result.a = Math.Max(Param1.a, Param2.a)
  140.  
  141.       result.r = IIf(Param1.r > 127.5, _
  142.         Param2.r + (255 - Param2.r) * ((Param1.r - 127.5) / 127.5), _
  143.         Param2.r * Param1.r / 127.5)
  144.       result.g = IIf(Param1.g > 127.5, _
  145.         Param2.g + (255 - Param2.g) * ((Param1.g - 127.5) / 127.5), _
  146.         Param2.g * Param1.g / 127.5)
  147.       result.b = IIf(Param1.b > 127.5, _
  148.         Param2.b + (255 - Param2.b) * ((Param1.b - 127.5) / 127.5), _
  149.         Param2.b * Param1.b / 127.5)
  150.  
  151.       Return result
  152.     End Function
  153.  
  154.     Shared Operator =(ByVal Param1 As Pixel, ByVal Param2 As Pixel) As Boolean
  155.       If (Param1.a = Param2.a) And (Param1.r = Param2.r) And (Param1.g = Param2.g) And (Param1.b = Param2.b) Then
  156.         Return True
  157.       Else
  158.         Return False
  159.       End If
  160.     End Operator
  161.  
  162.     Shared Operator <>(ByVal Param1 As Pixel, ByVal Param2 As Pixel) As Boolean
  163.       If (Param1.a = Param2.a) And (Param1.r = Param2.r) And (Param1.g = Param2.g) And (Param1.b = Param2.b) Then
  164.         Return False
  165.       Else
  166.         Return True
  167.       End If
  168.     End Operator
  169.  
  170.   End Structure
Advertisement
Add Comment
Please, Sign In to add comment