RGB的问题,算法。

dongge2000 2004-07-17 10:56:59
?RGB(0,0,0)
0
?RGB(1,0,0)
1
?RGB(0,1,0)
256
?RGB(0,0,1)
65536
?RGB(1,1,1)
65793
?RGB(10,11,12)
789258
?HEX(789258)
C0B0A
?RGB(8,9,10)
657672
?HEX(657672)
A0908
?RGB(2,3,4)
262914
?1*2+256*3+65536*4
262914
=================================
看得出来,RGB函数就是(1*R+256*G+65536*B)
我的问题是如何把(1*R+256*G+65536*B)的值还原成RGB的三个值。
如:
?1*2+256*3+65536*4
262914
如何把(262914)还原成
R=2,G=3,B=4

用HEX函数得到的是反的。
...全文
362 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
hxy2003 2004-09-10
  • 打赏
  • 举报
回复
UP
xuboying 2004-07-18
  • 打赏
  • 举报
回复
WallesCai(WallesCai) 快, northwolves(狼行天下)好懂,当然最好是选快的啦
熊孩子开学喽 2004-07-18
  • 打赏
  • 举报
回复
大家都好快啊,看看我的写法:
R = Color And &HFF& '通过和16进制数FF进行“与”操作取得低8位
G=(Color\256) And &HFF& '先右移8位,再取低8位,相当于原来的中间8位
B = Col \ 65536       '右移16位,得到的8位,相当于原来的高8位

还有一种写法:
R = Color Mod 256
G = (Color\256) Mod 256
B = Color \65536
这种写法和上面的区别在于使用了“取余数”运算符Mod,结果和上面一样
northwolves 2004-07-18
  • 打赏
  • 举报
回复
Option Explicit

Private Sub Command1_Click()
getrgb RGB(16, 39, 0)
End Sub
Sub getrgb(ByVal color As Long, Optional ByRef red As Byte, Optional ByRef green As Byte, Optional ByRef blue As Byte)
Dim temp As String
temp = Right("000000" & Hex(color), 6)
red = "&H" & Right(temp, 2)
green = "&H" & Mid(temp, 3, 2)
blue = "&H" & Left(temp, 2)
MsgBox "red: " & red & vbCrLf & "green: " & green & vbCrLf & "blue: " & blue
End Sub
dongge2000 2004-07-18
  • 打赏
  • 举报
回复
To:thirdapple(.:RNPA:.陨落雕-鍾意吊帶MM)
GetGreenValue = (Color And 65280) \ 256
中的65280是什么。为什么是65280
BlueBeer 2004-07-17
  • 打赏
  • 举报
回复
对,还有一个办法

以262914为例,把它转成16进制

?hex(262914)
40302

高位的是蓝色值的16进制,中间是绿色,低位是红色

再比如白色:16777215
?hex(16777215)
FFFFFF
就可以看出,红绿蓝的值均为FF,即255
thirdapple 2004-07-17
  • 打赏
  • 举报
回复
Public Function GetRedValue(Color As Long) As Integer
GetRedValue = Color And &HFF
End Function

Public Function GetGreenValue(Color As Long) As Integer
GetGreenValue = (Color And 65280) \ 256
End Function

Public Function GetBlueValue(Color As Long) As Integer
GetBlueValue = (Color And &HFF0000) \ 65536
End Function
dongge2000 2004-07-17
  • 打赏
  • 举报
回复
自己顶一下!

809

社区成员

发帖
与我相关
我的任务
社区描述
VB 多媒体
社区管理员
  • 多媒体
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧