Jeg kender ikke Visual Basic, men kunne dette virke? (googled)
Option Explicit
' 32 x 1 = 32 bits
Public Type Long32
Value As Long
End Type
' 8 x 4 = 32 bits
Public Type RGBA
Red As Byte
Green As Byte
Blue As Byte
Alpha As Byte
End Type
Public Function ColorToHex(ByVal Color As Long) As String
' 12 bytes = 6 characters = 0 To 11
Dim bytOut(11) As Byte
' make a character: take 4 bits, move them to be the lowest 4 bits,
' combine bitwise with character code 48, you will get a value from range 48 to 63 (&H30 to &H3F)
bytOut(0) = &H30& Or ((Color And &HF0&) \ &H10&)
' take 4 bits and combine bitwise with 48
bytOut(2) = &H30& Or (Color And &HF&)
' take 4 bits and move them to be the lowest 4 bits
bytOut(4) = &H30& Or ((Color And &HF000&) \ &H1000&)
' I guess you got it by now
bytOut(6) = &H30& Or ((Color And &HF00&) \ &H100&)
bytOut(8) = &H30& Or ((Color And &HF00000) \ &H100000)
bytOut(10) = &H30& Or ((Color And &HF0000) \ &H10000)
' because the resulting characters are until now from range
'
' 0123456789:;<=>?
'
' and we want them to be from range
'
' 0123456789 ABCDEF
' ....... <- 7 characters we do not want
'
' we increase character codes above 57 by 7 - this gives us a range from 48 - 57 and 65 to 70
If bytOut(0) > &H39 Then bytOut(0) = bytOut(0) + 7
If bytOut(2) > &H39 Then bytOut(2) = bytOut(2) + 7
If bytOut(4) > &H39 Then bytOut(4) = bytOut(4) + 7
If bytOut(6) > &H39 Then bytOut(6) = bytOut(6) + 7
If bytOut(8) > &H39 Then bytOut(8) = bytOut(8) + 7
If bytOut(10) > &H39 Then bytOut(10) = bytOut(10) + 7
' finally, make a real string out of the byte array
ColorToHex = bytOut
End Function
Public Function ColorToRGB(ByVal Color As Long) As RGBA
Dim lngRGB As Long32
' copy value to a User Defined Type
lngRGB.Value = Color
' now we can make a direct bitwise copy to another UDT of the same size, easy as a pie!
LSet ColorToRGB = lngRGB
End Function
Public Function HexRGB(ByVal Red As Byte, ByVal Green As Byte, ByVal Blue As Byte) As String
' same things happening as in ColorToHex, just from a different kind of source
' the biggest difference is that &H30& is now &H30, we are working as 8-bit Byte and not 32-bit Long
Dim bytOut(11) As Byte
bytOut(0) = &H30 Or ((Red And &HF0) \ &H10)
bytOut(2) = &H30 Or (Red And &HF)
bytOut(4) = &H30 Or ((Green And &HF0) \ &H10)
bytOut(6) = &H30 Or (Green And &HF)
bytOut(8) = &H30 Or ((Blue And &HF0) \ &H10)
bytOut(10) = &H30 Or (Blue And &HF)
If bytOut(0) > &H39 Then bytOut(0) = bytOut(0) + 7
If bytOut(2) > &H39 Then bytOut(2) = bytOut(2) + 7
If bytOut(4) > &H39 Then bytOut(4) = bytOut(4) + 7
If bytOut(6) > &H39 Then bytOut(6) = bytOut(6) + 7
If bytOut(8) > &H39 Then bytOut(8) = bytOut(8) + 7
If bytOut(10) > &H39 Then bytOut(10) = bytOut(10) + 7
HexRGB = bytOut
End Function
Public Function HexToColor(ByRef HexColor As String) As Long
' variable size byte array
Dim bytHex() As Byte
' we only accept one length, 6 characters = 12 bytes
If LenB(HexColor) = 12 Then
' convert string to byte array
bytHex = HexColor
' if a value is now higher than 57, we reduce it by 7
If bytHex(0) > &H39 Then bytHex(0) = bytHex(0) - 7
If bytHex(2) > &H39 Then bytHex(2) = bytHex(2) - 7
If bytHex(4) > &H39 Then bytHex(4) = bytHex(4) - 7
If bytHex(6) > &H39 Then bytHex(6) = bytHex(6) - 7
If bytHex(8) > &H39 Then bytHex(8) = bytHex(8) - 7
If bytHex(10) > &H39 Then bytHex(10) = bytHex(10) - 7
' this function is "stupid", it assumes it gets correct data...
' makes it faster, but you can give it any string that is 6 characters long, no error, ever!
' we take 4 bits for each six characters, and place it in the correct position of a Long,
' making up 24 bits that are required to represent a color value
HexToColor = ((bytHex(0) And &HF&) * &H10&) Or (bytHex(2) And &HF&) _
Or ((bytHex(4) And &HF&) * &H1000&) Or ((bytHex(6) And &HF&) * &H100&) _
Or ((bytHex(8) And &HF&) * &H100000) Or ((bytHex(10) And &HF&) * &H10000)
End If
End Function
Public Function HexToRGB(ByRef HexColor As String) As RGBA
' does the same as HexToColor and ColorToRGB
Dim bytHex() As Byte, lngRGB As Long32
If LenB(HexColor) = 12 Then
bytHex = HexColor
If bytHex(0) > &H39 Then bytHex(0) = bytHex(0) - 7
If bytHex(2) > &H39 Then bytHex(2) = bytHex(2) - 7
If bytHex(4) > &H39 Then bytHex(4) = bytHex(4) - 7
If bytHex(6) > &H39 Then bytHex(6) = bytHex(6) - 7
If bytHex(8) > &H39 Then bytHex(8) = bytHex(8) - 7
If bytHex(10) > &H39 Then bytHex(10) = bytHex(10) - 7
lngRGB.Value = ((bytHex(0) And &HF&) * &H10&) Or (bytHex(2) And &HF&) _
Or ((bytHex(4) And &HF&) * &H1000&) Or ((bytHex(6) And &HF&) * &H100&) _
Or ((bytHex(8) And &HF&) * &H100000) Or ((bytHex(10) And &HF&) * &H10000)
LSet HexToRGB = lngRGB
End If
End Function
Public Function RGBtoColor(ByRef RGB As RGBA) As Long
' does the same as ColorToRGB, just in opposite order
Dim lngRGB As Long32
LSet lngRGB = RGB
RGBtoColor = lngRGB.Value
End Function
Public Function RGBtoHex(ByRef RGB As RGBA) As String
' same things happening as in ColorToHex, just from a different kind of source
' the biggest difference is that &H30& is now &H30, we are working as 8-bit Byte and not 32-bit Long
Dim bytOut(11) As Byte
bytOut(0) = &H30 Or ((RGB.Red And &HF0) \ &H10)
bytOut(2) = &H30 Or (RGB.Red And &HF)
bytOut(4) = &H30 Or ((RGB.Green And &HF0) \ &H10)
bytOut(6) = &H30 Or (RGB.Green And &HF)
bytOut(8) = &H30 Or ((RGB.Blue And &HF0) \ &H10)
bytOut(10) = &H30 Or (RGB.Blue And &HF)
If bytOut(0) > &H39 Then bytOut(0) = bytOut(0) + 7
If bytOut(2) > &H39 Then bytOut(2) = bytOut(2) + 7
If bytOut(4) > &H39 Then bytOut(4) = bytOut(4) + 7
If bytOut(6) > &H39 Then bytOut(6) = bytOut(6) + 7
If bytOut(8) > &H39 Then bytOut(8) = bytOut(8) + 7
If bytOut(10) > &H39 Then bytOut(10) = bytOut(10) + 7
RGBtoHex = bytOut
End Function
Indlæg senest redigeret d. 10.01.2012 16:04 af Bruger #16191