Function RGB2Hex(ByVal Color as long)
bgrhex="000000"
RSet bgrhex = Hex(Color)
bgrhex = Replace(bgrhex, " ", "0")
RGB2Hex = "#" + Mid$(bgrhex, 5,2) + Mid$(bgrhex, 3,2) +Mid$(bgrhex, 5,2)
End Function
MsgBox RGB2Hex( RGB( 32, 20, 256)) ' Выдаст #2014FF
Function RGB2Hex(ByVal Color as long)
Dim bRed As Byte
Dim bGreen As Byte
Dim bBlue As Byte
bRed = (Color And vbRed)
bGreen = (Color And vbGreen) \ 255
bBlue = (Color And vbBlue) \ 255 \ 255
RGB2Hex = "#" + CStr(bRed) + CStr(bGreen) + CStr(bBlue)
End Function
Sub main()
Dim tk As Long, n As Long, result As String
tk = GetTickCount
For n = 0 To 599999
result = RGB2Hex2(&HA0D0F0)
Next
MsgBox "Мой вариант за " & (GetTickCount - tk) & " миллисекунд выполнился 50000 раз"
tk = GetTickCount
For n = 0 To 599999
result = RGB2Hex(&HA0D0F0)
Next
MsgBox "Твой первый вариант за " & (GetTickCount - tk) & " миллисекунд выполнился 50000 раз"
End Sub
Function RGB2Hex2(ByVal Color As Long) As String
RGB2Hex2 = "#" + Hex$((Color And vbRed)) + Hex$((Color And vbGreen) \ 255) + Hex$((Color And vbBlue) \ 255 \ 255)
End Function
Function RGB2Hex(ByVal Color As Long)
bgrhex = "000000"
RSet bgrhex = Hex(Color)
bgrhex = Replace(bgrhex, " ", "0")
RGB2Hex = "#" + Mid$(bgrhex, 5, 2) + Mid$(bgrhex, 3, 2) + Mid$(bgrhex, 5, 2)
End Function
keks-n писал(а):...Мой вариант за ...
keks-n писал(а):
- Код: Выделить всё
Function RGB2Hex2(ByVal Color As Long) As String
RGB2Hex2 = "#" + Hex$((Color And vbRed)) + Hex$((Color And vbGreen) \ 255) + Hex$((Color And vbBlue) \ 255 \ 255)
End Function
Function RGB2Hex(ByVal Color as long)
RGB2Hex = "#" + Format$(Hex$(Color And vbRed) , "00") + _
Format$(Hex$((Color And vbGreen) \ 255),"00") + _
Format$(Hex$((Color And vbBlue) \ 65025), "00")
End Function
Function RGB2Hex(ByVal Color As Long)
Dim hexval As String * 6
RSet hexval = Hex$(RGB((Color And vbBlue) \ 65025, (Color And vbGreen) \ 255, (Color And vbRed)))
RGB2Hex = "#" + Replace(hexval, " ", "0")
End Function
Как там у нас в бриллиантовой руке... Странные, если не сказать большеХакер писал(а):И также \255\255 а не \ 65025
странные совпадения, не правда ли?
Private Function RGB2Hex(ByVal Color As Long) As String
RGB2Hex = "#" & Format$(Hex$(Color), "000000")
End Function
Т.е. твой неработающий код с тремя хексами и тройным сложением строк работает быстрее моего "со строковыми операциями"? Надо проверитьkeks-n писал(а):vvs_adm
Скорость... Строковые операции выполняются медленнее.
Right$("000000" & Hex$(Color),6)
Function RGB2Hex(color As Long) As String
RGB2Hex = "#" + Hex$(color And &HFF) + Hex$((color And &HFF00) / &H100 And &HFF) + Hex$((color And &HFF0000) / &H10000)
End Function
Хакер писал(а):Lumen. Мне этот способ жутко не нравится, по сравнению с тем, который я предложил.
Но у автора вопроса должно быть право выбора поэтому вот:
- Код: Выделить всё
Function RGB2Hex(ByVal Color as long)
Dim bRed As Byte
Dim bGreen As Byte
Dim bBlue As Byte
bRed = (Color And vbRed)
bGreen = (Color And vbGreen) \ 255
bBlue = (Color And vbBlue) \ 255 \ 255
RGB2Hex = "#" + CStr(bRed) + CStr(bGreen) + CStr(bBlue)
End Function
использование функции такое же
vvs_adm писал(а):И зачем вообще извращаться, если можно написать
- Код: Выделить всё
Private Function RGB2Hex(ByVal Color As Long) As String
RGB2Hex = "#" & Format$(Hex$(Color), "000000")
End Function
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Sub main()
Dim tk As Long, n As Long, result As String
tk = GetTickCount()
For n = 0 To 599999
result = RGB2Hex2(&HA0D0F0)
Next
MsgBox "Ìîé âàðèàíò çà " & (GetTickCount - tk) & " ìèëëèñåêóíä âûïîëíèëñÿ 50000 ðàç"
tk = GetTickCount
For n = 0 To 599999
result = RGB2Hex(&HA0D0F0)
Next
MsgBox "Òâîé âàðèàíò çà " & (GetTickCount - tk) & " ìèëëèñåêóíä âûïîëíèëñÿ 50000 ðàç"
End Sub
Function RGB2Hex2(color As Long) As String
RGB2Hex2 = "#" + Hex$(color And &HFF) + Hex$((color And &HFF00) / &H100 And &HFF) + Hex$((color And &HFF0000) / &H10000)
End Function
Function RGB2Hex(ByVal color As Long)
Dim bRed As Byte
Dim bGreen As Byte
Dim bBlue As Byte
bRed = (color And vbRed)
bGreen = (color And vbGreen) \ 255
bBlue = (color And vbBlue) \ 255 \ 255
RGB2Hex = "#" + CStr(bRed) + CStr(bGreen) + CStr(bBlue)
End Function
Function RGB2Hex(ByVal Color As Long)
RGB2Hex = "#" + Right$("000000" + Hex$(RGB((Color And vbBlue) \ 65536, (Color And vbGreen) \ 256, (Color And vbRed))), 6)
End Function
Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 33