用api函數取色后,如何將其分成rgb顏色?
問題:
用api函數取色后,是一個10進制的數值,如何將其分成rgb顏色?
方法一:
用 hex 函數將數值轉換為 16 進制,然后,每兩個切分一下就可以得到 rgb 數值了
function c10torgb_1(lngcolor as long) as string
dim strr as string
dim strg as string
dim strb as string
strr = lngcolor mod 256
strg = lngcolor / 256 mod 256
strb = lngcolor / 256 / 256
c10torgb_1 = strr & "_" & strg & "_" & strb
end function
sub test1()
debug.print c10torgb_1(33023)
debug.print rgb(255, 128, 0)
end sub
方法二:
【轉載】
如果要將vb的顏色轉換為colorref,需要使用oletranslatecolor函數。例子:
private declare function oletranslatecolor lib "olepro32.dll" _
(byval ole_color as long, _
byval hpalette as long, _
pccolorref as long) as long
private sub getrbgfromolecolour(byval dwolecolour as long, r as long, g as long, b as long)
'pass a hex colour, return the rgb components
dim clrref as long
'translate ole color to valid color if passed
oletranslatecolor dwolecolour, 0, clrref
b = (clrref / 65536) and &hff
g = (clrref / 256) and &hff
r = clrref and &hff
text1(0).text = dwolecolour
text1(1).text = clrref
end sub
更完整的例子參考:http://www.mvps.org/vbnet/index.html?code/system/oletranslatecolor.htm
方法三:
用 hex 函數將數值轉換為 16 進制,然后,每兩個切分一下就可以得到 rgb 數值了
sub test1()
debug.print c10torgb(33023)
debug.print rgb(255, 128, 0)
end sub
function c10torgb(lnga as long) as string
dim strr as string
dim strg as string
dim strb as string
dim strhex as string
strhex = right("00000" & hex(lnga), 6)
'debug.print "b" & mid(strhex, 1, 2)
'debug.print "g" & mid(strhex, 3, 2)
'debug.print "r" & mid(strhex, 5, 2)
strb = c16to10(mid(strhex, 1, 2))
strg = c16to10(mid(strhex, 3, 2))
strr = c16to10(mid(strhex, 5, 2))
c10torgb = strr & "," & strg & "," & strb
'debug.print c10torgb
end function
'以下函數將 16 進制數值轉換為 10 進制數值
private function c16to10(stra as string) as double
dim a as double
dim b as string
dim c as double
dim l as integer
dim i as long
l = len(stra)
for i = 1 to l
b = mid(stra, i, 1)
select case b
case "a"
b = 10
case "b"
b = 11
case "c"
b = 12
case "d"
b = 13
case "e"
b = 14
case "f"
b = 15
end select
c = c + b * 16 ^ (l - 1)
l = l - 1
next
c16to10 = c
'debug.print c16to10
end function
如何取色你可以參考本站
http://access911.net/index.asp?board=4&recordid=71fab31e16dc