Advertisement
Regenspaziergang

Untitled

Dec 22nd, 2016
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. VERSION 5.00
  2. Begin VB.Form Form1
  3.    Caption         =   "Form1"
  4.    ClientHeight    =   8010
  5.    ClientLeft      =   120
  6.    ClientTop       =   465
  7.    ClientWidth     =   10080
  8.    LinkTopic       =   "Form1"
  9.    ScaleHeight     =   8010
  10.    ScaleWidth      =   10080
  11.    StartUpPosition =   3  'Windows Default
  12.   Begin VB.TextBox Output
  13.       Height          =   6495
  14.       Left            =   360
  15.       MultiLine       =   -1  'True
  16.      TabIndex        =   2
  17.       Text            =   "Form1.frx":0000
  18.       Top             =   720
  19.       Width           =   6855
  20.    End
  21.    Begin VB.TextBox Text1
  22.       Height          =   285
  23.       Left            =   1920
  24.       TabIndex        =   1
  25.       Text            =   "Text1"
  26.       Top             =   240
  27.       Width           =   5295
  28.    End
  29.    Begin VB.CommandButton Command1
  30.       Caption         =   "Command1"
  31.       Height          =   375
  32.       Left            =   360
  33.       TabIndex        =   0
  34.       Top             =   240
  35.       Width           =   1455
  36.    End
  37. End
  38. Attribute VB_Name = "Form1"
  39. Attribute VB_GlobalNameSpace = False
  40. Attribute VB_Creatable = False
  41. Attribute VB_PredeclaredId = True
  42. Attribute VB_Exposed = False
  43. Option Explicit
  44.  
  45. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
  46.             (destination As Any, source As Any, ByVal length As Long)
  47.  
  48. Private Type LongBytes
  49.     Byte1 As Byte
  50.     Byte2 As Byte
  51.     Byte3 As Byte
  52.     Byte4 As Byte
  53. End Type
  54.  
  55. Private Type Struct_Color
  56.     Color As Long
  57. End Type
  58.  
  59. Private Type Struct_RGBABytes
  60.     R As Byte
  61.     G As Byte
  62.     B As Byte
  63.     A As Byte
  64. End Type
  65.  
  66. Private Type Struct_RGBALongs
  67.     R As Long
  68.     G As Long
  69.     B As Long
  70.     A As Long
  71. End Type
  72.  
  73. Private Type Struct_FixedString6
  74.     FixedLenStr As String * 6
  75. End Type
  76.  
  77. Function RgbToHex_Original(l As Long) As String
  78. Dim R As Long
  79. Dim G As Long
  80. Dim B As Long
  81.     R = l And ColorConstants.vbRed
  82.     G = (l And ColorConstants.vbGreen) / (2 ^ 8)
  83.     B = (l And ColorConstants.vbBlue) / (2 ^ 16)
  84.     RgbToHex_Original = Right$("0" & Hex(R), 2) _
  85.                       & Right$("0" & Hex(G), 2) _
  86.                       & Right$("0" & Hex(B), 2)
  87. End Function
  88.  
  89.  
  90. Public Function RgbToHex_CodyGray(ByVal Color As Long) As String
  91.     ' On little-endian architectures, "RGB" color values are actually stored in memory
  92.    ' in an AABBGGRR format. However, web-style hex colors are always formatted in an
  93.    ' AARRGGBB format. Neither Windows nor HTML care about the alpha-channel value
  94.    ' (always the upper, most-significant bit), so we are going to simply ignore it.
  95.    ' Otherwise, we have to do some bit-twiddling to swap the red and blue color channels.
  96.  
  97.     ' Extract the individual red, green, and blue color channels.
  98.    ' (Although they are only bytes, continue to store these intermediate values
  99.    ' as 32-bit integers for maximum speed.)
  100.    Dim R As Long
  101.     Dim G As Long
  102.     Dim B As Long
  103.     R = (Color And &HFF)
  104.     G = ((Color \ &H100) And &HFF)
  105.     B = ((Color \ &H10000) And &HFF)
  106.  
  107.     ' Now, arrange these byte values in the correct order (RRGGBB).
  108.    Color = ((R * &H10000) + (G * &H100) + B)
  109.  
  110.     ' And finally, format it as a hexadecimal string, left-padding with zeros as necessary.
  111.    ' Note that if the red component value is large enough, padding does not need to be
  112.    ' done, which allows us to skip an expensive string operation.
  113.    If (R >= &H10) Then
  114.         RgbToHex_CodyGray = Hex$(Color)
  115.     Else
  116.         RgbToHex_CodyGray = Right$("00000" & Hex$(Color), 6)
  117.     End If
  118. End Function
  119.  
  120. Public Function RgbToHex_GrayIllig(ByVal Color As Long) As String
  121.     ' On little-endian architectures, "RGB" color values are actually stored in memory
  122.    ' in an AABBGGRR format. However, web-style hex colors are always formatted in an
  123.    ' AARRGGBB format. Neither Windows nor HTML care about the alpha-channel value
  124.    ' (always the upper, most-significant bit), so we are going to simply ignore it.
  125.    ' Otherwise, we have to do some bit-twiddling to swap the red and blue color channels.
  126.  
  127.     ' Extract the individual red, green, and blue color channels.
  128.    ' (Although they are only bytes, continue to store these intermediate values
  129.    ' as 32-bit integers for maximum speed.)
  130.    Dim R As Long
  131.     Dim G As Long
  132.     Dim B As Long
  133.     R = (Color And &HFF)
  134.     G = ((Color \ &H100) And &HFF)
  135.     B = ((Color \ &H10000) And &HFF)
  136.  
  137.     ' Now, arrange these byte values in the correct order (RRGGBB).
  138.    Color = ((R * &H10000) + (G * &H100) + B)
  139.  
  140.     ' And finally, format it as a hexadecimal string, left-padding with zeros as necessary.
  141.    RgbToHex_GrayIllig = Right$(Hex$(&H1000000 Or Color), 6)
  142. End Function
  143.  
  144.  
  145. Public Function RgbToHex2_Comintern(inVal As Long) As String
  146.     Dim bytes As LongBytes, swapped As LongBytes
  147.  
  148.     CopyMemory bytes, inVal, 4
  149.     swapped.Byte1 = bytes.Byte3
  150.     swapped.Byte2 = bytes.Byte2
  151.     swapped.Byte3 = bytes.Byte1
  152.     swapped.Byte4 = bytes.Byte4
  153.  
  154.     Dim retVal As Long
  155.     CopyMemory retVal, swapped, 4
  156.  
  157.     RgbToHex2_Comintern = Right$("0" & Hex$(retVal), 6)    '<--1 call each to Right$ and Hex$
  158. End Function
  159.  
  160. Public Function RgbToHex_MatsMug(ByVal rgbValue As Long) As String
  161. Const gOffSet As Long = 2 ^ 8
  162. Const bOffSet As Long = 2 ^ 16
  163.  
  164. Dim rValue As Long
  165. rValue = rgbValue And ColorConstants.vbRed
  166.  
  167. Dim gValue As Long
  168. gValue = (rgbValue And ColorConstants.vbGreen) / gOffSet
  169.  
  170. Dim bValue As Long
  171. bValue = (rgbValue And ColorConstants.vbBlue) / bOffSet
  172.  
  173. RgbToHex_MatsMug = Right$("0" & Hex$(rValue), 2) _
  174.                  & Right$("0" & Hex$(gValue), 2) _
  175.                  & Right$("0" & Hex$(bValue), 2)
  176. End Function
  177.  
  178. Function RgbToHex_RolandIllig(Color As Long) As String
  179.     Dim R As Long, G As Long, B As Long
  180.     R = Color And &HFF
  181.     G = (Color / &H100) And &HFF
  182.     B = (Color / &H10000) And &HFF
  183.     RgbToHex_RolandIllig = Right$(Hex$(&H1000000 + &H10000 * R + &H100 * G + B), 6)
  184. End Function
  185.  
  186.  
  187. Function RgbToHex_Thunderframe(l As Long) As String
  188. Dim R As Long
  189. Dim G As Long
  190. Dim B As Long
  191.     R = l And ColorConstants.vbRed
  192.     G = (l And ColorConstants.vbGreen) / (2 ^ 8)
  193.     B = (l And ColorConstants.vbBlue) / (2 ^ 16)
  194.     RgbToHex_Thunderframe = Right$(Hex$(256 Or R), 2) _
  195.                           & Right$(Hex$(256 Or G), 2) _
  196.                           & Right$(Hex$(256 Or B), 2)
  197. End Function
  198.  
  199. Public Function RgbToHex_UnknownUser(ByVal ColorRGB As Long) As String
  200.  
  201.     Dim InputColor                      As Struct_Color
  202.     Dim InputRGBAccessor                As Struct_RGBABytes
  203.     Dim OutputRGBAccessor               As Struct_RGBALongs
  204.     Dim IntermediateOutput              As Struct_FixedString6
  205.  
  206.     Const LowerCaseHex                  As Boolean = False
  207.     Const AsciiZero                     As Long = 48                        ' Asc("0")
  208.    Const AsciiTenToAOffset             As Long = 7 + (LowerCaseHex * -32)  ' Asc("A") - (Asc("9") + 1) = 7
  209.    Const HalfByteDivider               As Long = 16                        ' divider to do right shift by 4 bits
  210.    Const LowerToUpperWordMultiplier    As Long = &H10000
  211.  
  212.     InputColor.Color = ColorRGB                                 ' copy the RGB Long value to a simple struct container
  213.    LSet InputRGBAccessor = InputColor                          ' memory copy of struct to allow access to the individual R/G/B values
  214.  
  215.     ' convert each R/G/B value to a DWORD/Long containing the equivalent of 2 unicode characters (the high and low bits of each byte, each converted to hex)
  216.    OutputRGBAccessor.R = AsciiZero + (InputRGBAccessor.R \ HalfByteDivider) + (((InputRGBAccessor.R \ HalfByteDivider) \ 10) * AsciiTenToAOffset) + _
  217.                             (AsciiZero + (InputRGBAccessor.R Mod HalfByteDivider) + (((InputRGBAccessor.R Mod HalfByteDivider) \ 10) * AsciiTenToAOffset)) * LowerToUpperWordMultiplier
  218.     OutputRGBAccessor.G = AsciiZero + (InputRGBAccessor.G \ HalfByteDivider) + (((InputRGBAccessor.G \ HalfByteDivider) \ 10) * AsciiTenToAOffset) + _
  219.                             (AsciiZero + (InputRGBAccessor.G Mod HalfByteDivider) + (((InputRGBAccessor.G Mod HalfByteDivider) \ 10) * AsciiTenToAOffset)) * LowerToUpperWordMultiplier
  220.     OutputRGBAccessor.B = AsciiZero + (InputRGBAccessor.B \ HalfByteDivider) + (((InputRGBAccessor.B \ HalfByteDivider) \ 10) * AsciiTenToAOffset) + _
  221.                             (AsciiZero + (InputRGBAccessor.B Mod HalfByteDivider) + (((InputRGBAccessor.B Mod HalfByteDivider) \ 10) * AsciiTenToAOffset)) * LowerToUpperWordMultiplier
  222.  
  223.     LSet IntermediateOutput = OutputRGBAccessor                 ' copy the unicode equivalent struct over into a fixed-length string
  224.    RgbToHex_UnknownUser = IntermediateOutput.FixedLenStr       ' convert the fixed-length string on the stack into a real BSTR.
  225.  
  226. End Function
  227.  
  228. Private Sub Command1_Click()
  229. ' flags currently unused
  230. Dim testorig As Boolean: testorig = True
  231. Dim testcody As Boolean: testcody = True
  232. Dim testmats As Boolean: testmats = True
  233. Dim testillig As Boolean: testillig = True
  234. Dim testthunder As Boolean: testthunder = True
  235. Dim testcom As Boolean: testcom = True
  236. Dim testunknown As Boolean: testunknown = True
  237.  
  238. Dim i As Long
  239. Dim hexstr As String
  240. Dim time As Double
  241.  
  242.     time = Timer
  243.     For i = 0 To &HFFFFFF
  244.         hexstr = RgbToHex_Original(i)
  245.     Next
  246.     Text1.Text = i
  247.     time = Timer - time
  248.     Output.Text = Output.Text & "Original: " & Round(time, 3) & " s" & vbCrLf
  249.    
  250.     DoEvents
  251.    
  252.     time = Timer
  253.     For i = 0 To &HFFFFFF
  254.         hexstr = RgbToHex_CodyGray(i)
  255.     Next
  256.     Text1.Text = i
  257.     time = Timer - time
  258.     Output.Text = Output.Text & "Cody Gray: " & Round(time, 3) & " s" & vbCrLf
  259.    
  260.     DoEvents
  261.    
  262.     time = Timer
  263.     For i = 0 To &HFFFFFF
  264.         hexstr = RgbToHex_GrayIllig(i)
  265.     Next
  266.     Text1.Text = i
  267.     time = Timer - time
  268.     Output.Text = Output.Text & "Cody Gray/Roland Illig: " & Round(time, 3) & " s" & vbCrLf
  269.    
  270.     DoEvents
  271.    
  272.     time = Timer
  273.     For i = 0 To &HFFFFFF
  274.         hexstr = RgbToHex_MatsMug(i)
  275.     Next
  276.     Text1.Text = i
  277.     time = Timer - time
  278.     Output.Text = Output.Text & "Mat's Mug: " & Round(time, 3) & " s" & vbCrLf
  279.    
  280.     DoEvents
  281.    
  282.     time = Timer
  283.     For i = 0 To &HFFFFFF
  284.         hexstr = RgbToHex_RolandIllig(i)
  285.     Next
  286.     Text1.Text = i
  287.     time = Timer - time
  288.     Output.Text = Output.Text & "Roland Illig: " & Round(time, 3) & " s" & vbCrLf
  289.    
  290.     DoEvents
  291.    
  292.     time = Timer
  293.     For i = 0 To &HFFFFFF
  294.         hexstr = RgbToHex_Thunderframe(i)
  295.     Next
  296.     Text1.Text = i
  297.     time = Timer - time
  298.     Output.Text = Output.Text & "Thunderframe: " & Round(time, 3) & " s" & vbCrLf
  299.    
  300.     DoEvents
  301.    
  302.     time = Timer
  303.     For i = 0 To &HFFFFFF
  304.         hexstr = RgbToHex2_Comintern(i)
  305.     Next
  306.     Text1.Text = i
  307.     time = Timer - time
  308.     Output.Text = Output.Text & "Comintern: " & Round(time, 3) & " s" & vbCrLf
  309.    
  310.     DoEvents
  311.    
  312.     time = Timer
  313.     For i = 0 To &HFFFFFF
  314.         hexstr = RgbToHex_UnknownUser(i)
  315.     Next
  316.     Text1.Text = i
  317.     time = Timer - time
  318.     Output.Text = Output.Text & "Unknown user: " & Round(time, 3) & " s" & vbCrLf
  319. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement