Valhalla Legends Forums Archive | Visual Basic Programming | RGB Revert

AuthorMessageTime
Networks
Any clue how to get the three R-G-B values from the long RGB() outputs?
November 20, 2005, 11:32 PM
Networks
Never mind I figured it out and made my own function for it:

[code]
Public Function LongToRGBString(ByVal Color As Long) As String
    Dim HexColor    As String
    HexColor = CStr(Hex(Color))
    If (HexColor <> 0) Then
        LongToRGBString = CByte("&H" & Right$(HexColor, 2)) & ", " & CByte("&H" & Mid$(HexColor, 3, 2)) & ", " & CByte("&H" & Left$(HexColor, 2))
    Else
        LongToRGBString = "255, 255, 255"
    End If
End Function
[/code]
November 20, 2005, 11:53 PM
JoeTheOdd
Yeah, you figured it out. In &HFF5522, &H = long, hex; FF = Red; 55 = Green; 22 = Blue.
November 22, 2005, 12:57 PM
Myndfyr
[quote author=Joe link=topic=13303.msg135004#msg135004 date=1132664250]
Yeah, you figured it out. In &HFF5522, &H = long, hex; FF = Red; 55 = Green; 22 = Blue.
[/quote]

&H doesn't indicate that it's a Long variable, only that the following value is encoded in hex.
November 22, 2005, 9:39 PM
Networks
It bugged out a bit so I googled a bit and found something much more efficient and stable:

[code]
Public Function ColorCodeToRGB(lColorCode As Long) As String
    Dim lColor As Long
    lColor = lColorCode      'work long
    iRed = lColor Mod &H100  'get red component
    lColor = lColor \ &H100  'divide
    iGreen = lColor Mod &H100 'get green component
    lColor = lColor \ &H100  'divide
    iBlue = lColor Mod &H100 'get blue component

    ColorCodeToRGB = iRed & ", " & iGreen & ", " & iBlue
End Function
[/code]
November 23, 2005, 1:14 AM
JoeTheOdd
[quote author=MyndFyre link=topic=13303.msg135048#msg135048 date=1132695595]
[quote author=Joe link=topic=13303.msg135004#msg135004 date=1132664250]
Yeah, you figured it out. In &HFF5522, &H = long, hex; FF = Red; 55 = Green; 22 = Blue.
[/quote]

&H doesn't indicate that it's a Long variable, only that the following value is encoded in hex.
[/quote]

& represents long.

Dim Val&
Dim Val as Long

Dim Val$
Dim Val as String

Dim Val#
Dim Val as Double

Etc.
November 24, 2005, 3:53 AM
Networks
I think it's a hex based representation of a long value which is converted later during storage or w/e.
November 24, 2005, 10:24 PM
rabbit
[quote author=Joe link=topic=13303.msg135198#msg135198 date=1132804416]
[quote author=MyndFyre link=topic=13303.msg135048#msg135048 date=1132695595]
[quote author=Joe link=topic=13303.msg135004#msg135004 date=1132664250]
Yeah, you figured it out. In &HFF5522, &H = long, hex; FF = Red; 55 = Green; 22 = Blue.
[/quote]

&H doesn't indicate that it's a Long variable, only that the following value is encoded in hex.
[/quote]

& represents long.

Dim Val&
Dim Val as Long

Dim Val$
Dim Val as String

Dim Val#
Dim Val as Double

Etc.
[/quote]
&H means "base 16", just like &O means "base 8".  Non-base 10 values default to Long anyway, unless assigned to an Integer.  Bad Joe.
November 25, 2005, 3:49 AM
JoeTheOdd
Damn misleading language. & still means long, usually. =p

EDIT -
My bad, I guess its stored in Blue-Green-Red instead of Red-Green-Blue.

[tt]Option Explicit

Public Sub Main()
    Dim m_lRGB As Long
    Dim m_bRed1 As Byte, m_bGreen1 As Byte, m_bBlue1 As Byte
    Dim m_bRed2 As Byte, m_bGreen2 As Byte, m_bBlue2 As Byte
    m_bRed1 = 1
    m_bGreen1 = 2
    m_bBlue1 = 3
    m_lRGB = RGB(m_bRed1, m_bGreen1, m_bBlue1)
    Call ReverseRGB(m_lRGB, m_bRed2, m_bGreen2, m_bBlue2)
    Call MsgBox("RGB(" & m_bRed1 & ", " & m_bGreen1 & ", " & m_bBlue1 & ")")
    Call MsgBox("RGB(" & m_bRed2 & ", " & m_bGreen2 & ", " & m_bBlue2 & ")")
End Sub

Public Sub ReverseRGB(ByVal p_lRGB As Long, ByRef r_bRed As Byte, ByRef r_bGreen As Byte, ByRef r_bBlue As Byte)
    r_bBlue = Val("&H" & Mid(Right("000000" & Hex(p_lRGB), 6), 1, 2))
    r_bGreen = Val("&H" & Mid(Right("000000" & Hex(p_lRGB), 6), 3, 2))
    r_bRed = Val("&H" & Mid(Right("000000" & Hex(p_lRGB), 6), 5, 2))
End Sub[/tt]
November 26, 2005, 6:48 AM
Networks
[quote author=Joe link=topic=13303.msg135365#msg135365 date=1132987729]
Damn misleading language. & still means long, usually. =p

EDIT -
My bad, I guess its stored in Blue-Green-Red instead of Red-Green-Blue.

[tt]Option Explicit

Public Sub Main()
    Dim m_lRGB As Long
    Dim m_bRed1 As Byte, m_bGreen1 As Byte, m_bBlue1 As Byte
    Dim m_bRed2 As Byte, m_bGreen2 As Byte, m_bBlue2 As Byte
    m_bRed1 = 1
    m_bGreen1 = 2
    m_bBlue1 = 3
    m_lRGB = RGB(m_bRed1, m_bGreen1, m_bBlue1)
    Call ReverseRGB(m_lRGB, m_bRed2, m_bGreen2, m_bBlue2)
    Call MsgBox("RGB(" & m_bRed1 & ", " & m_bGreen1 & ", " & m_bBlue1 & ")")
    Call MsgBox("RGB(" & m_bRed2 & ", " & m_bGreen2 & ", " & m_bBlue2 & ")")
End Sub

Public Sub ReverseRGB(ByVal p_lRGB As Long, ByRef r_bRed As Byte, ByRef r_bGreen As Byte, ByRef r_bBlue As Byte)
    r_bBlue = Val("&H" & Mid(Right("000000" & Hex(p_lRGB), 6), 1, 2))
    r_bGreen = Val("&H" & Mid(Right("000000" & Hex(p_lRGB), 6), 3, 2))
    r_bRed = Val("&H" & Mid(Right("000000" & Hex(p_lRGB), 6), 5, 2))
End Sub[/tt]
[/quote]

No I believe it's stored as R-G-B, the code I provided above works fine.
November 26, 2005, 6:18 PM
JoeTheOdd
Yes, it extracts the value red, which is at the end of it. Then it divides by 0x100, taking the next byte one value to the right. Then it removes blue, which was in the middle. Then it divides by 0x100 again. Then it finally extracts blue, which was at the high end of it. Its extracting it in the order R G B, but its doing it backwards, so its B G R.
November 26, 2005, 10:05 PM
iNsaNe
Why don't you just use code already given in vb6...

Form1.BackColor = RGB(R, G, B)

R = Red as Integer
G = Green as Integer
B = Blue as Integer
March 5, 2006, 10:46 PM
UserLoser
[quote author=iNsaNe link=topic=13303.msg147659#msg147659 date=1141598798]
Why don't you just use code already given in vb6...

Form1.BackColor = RGB(R, G, B)

R = Red as Integer
G = Green as Integer
B = Blue as Integer
[/quote]

Read the topic title, RGB revert.  Meaning: he wants to get the red, green, blue values from a value already assigned from RGB()
March 9, 2006, 9:28 PM
RealityRipple
Why not Just convert the decimal to hex using the Hex() function? Then you can split it up into pairs... (remember to add trailing zeroes first)...
March 10, 2006, 12:26 AM
MyStiCaL
Jesus your sig laggs... when i scoll past it..
March 12, 2006, 11:55 AM

Search