用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