Long to RGB ?

  • Thread starter Thread starter Dave Ruhl
  • Start date Start date
D

Dave Ruhl

I know the RGB function returns a Long value. Are there
any functions that do the reverse ?

I built a form that allows users to customize the color
scheme of all the the data entry screens in the program.
I would like to display the RGB value of each color they
select. Is it possible ?
 
I know the RGB function returns a Long value. Are there
any functions that do the reverse ?

I built a form that allows users to customize the color
scheme of all the the data entry screens in the program.
I would like to display the RGB value of each color they
select. Is it possible ?

My memory of this is rusty, so test it out - but as I recall, the RGB
long value is a Long Integer in the range x000000 to xFFFFFF, where
the first two hex digits are Blue, the middle two Green and the last
two Red. You can extract them with arithmetic operators:

Red = RGB MOD 256
Green = RGB \ 256 MOD 256
Blue = RGB \ 65536

The expressions should be right, but the color assignments may well be
wrong!
 
Assume text boxes named:
txtRedHex (for entering a hex value for red),
txtRedDec (for entering a decimal value for red),
txtGreenHex, etc.
and one named:
txtCombined (for entering a combined value (long)).

Private Sub txtCombined_AfterUpdate()
Dim btRed As Byte, btGreen As Byte, btBlue As Byte
Dim lngColor As Long

If IsNum(Me.txtCombined) Then
btRed = Me.txtCombined Mod 256
lngColor = Me.txtCombined \ 256
btGreen = lngColor Mod 256
btBlue = lngColor \ 256
Me.Section(acDetail).BackColor = Me.txtCombined
Me.txtRedDec = btRed
Me.txtGreenDec = btGreen
Me.txtBlueDec = btBlue
Me.txtRedHex = Hex(btRed)
Me.txtGreenHex = Hex(btGreen)
Me.txtBlueHex = Hex(btBlue)
Else
Beep
End If
End Sub

Set the After Update property of the other 6 boxes to:
=SetColor()

Function SetColor()
Dim ctl As Control

Set ctl = Screen.ActiveControl
Select Case ctl.Name
Case "txtRedDec"
Me.txtRedHex = Hex(ctl.Value)
Me.txtCombined = RGB(Me.txtRedDec, Me.txtGreenDec, Me.txtBlueDec)
Case "txtGreenDec"
Me.txtGreenHex = Hex(ctl.Value)
Me.txtCombined = RGB(Me.txtRedDec, Me.txtGreenDec, Me.txtBlueDec)
Case "txtBlueDec"
Me.txtBlueHex = Hex(ctl.Value)
Me.txtCombined = RGB(Me.txtRedDec, Me.txtGreenDec, Me.txtBlueDec)

Case "txtRedHex"
Me.txtRedDec = Val("&H" & ctl.Value)
Me.txtCombined = RGB(Me.txtRedDec, Me.txtGreenDec, Me.txtBlueDec)
Case "txtGreenHex"
Me.txtGreenDec = Val("&H" & ctl.Value)
Me.txtCombined = RGB(Me.txtRedDec, Me.txtGreenDec, Me.txtBlueDec)
Case "txtBlueHex"
Me.txtBlueDec = Val("&H" & ctl.Value)
Me.txtCombined = RGB(Me.txtRedDec, Me.txtGreenDec, Me.txtBlueDec)
Case "txtCombined"
Case Else
MsgBox "Active control " & ctl.Name & " not handled."
End Select
Me.Section(acDetail).BackColor = Me.txtCombined
End Function
 
Thanks to both of you ! Your tips set me on the right
path. After testing several sets of numbers I found this
formula works every time:

Red = lngRGB Mod 256
Green = Int(lngRGB/ 256) Mod 256
Blue = Int(lngRGB/ 65536)
 
Just a comment.

Did you notice that both of the posters suggested using \ for the division,
not /?

The \ operator is used to divide two numbers and return an integer result.

In other words, Blue = lngRGB \ 65536 is the same as Blue = Int(lngRGB/
65536)
 
Well, I'm just learning all sorts of useful stuff
today ! I didn't notice the different slash, and didn't
know it worked that way. Thanks !
 
Actually, I am using the Color Dialog Window - that's
where I'm getting the long value.

What I have is an application with about a dozen data
entry forms, all based on a similar format. I created a
form in this same format to allow users to create their
own color scheme - selecting colors for 8 different areas
of the form. When they click an area I display the color
dialog window and they can change the color for that
area. Their choice is saved to a table which all forms
use to get the color for that area, for that user.

What I wanted the RGB values for was to show them in a
ControlTip, so the user could see them by holding the
mouse over the colored area of the form. That way they
could easily use that same color in another area of the
form without having to guess the RGB values.

Thanks again to all who responded, you were a great help
and I was able to accomplish what I wanted.
 
Check the GetChunk method in on-line help.

--
Rebecca Riordan, MVP

Designing Relational Database Systems
Microsoft SQL Server 2000 Programming Step by Step
Microsoft ADO.NET Step by Step

http://www.microsoft.com/mspress

Blessed are they who can laugh at themselves,
for they shall never cease to be amused...
 
Sorry about that last post -- I had the wrong message selected!

--
Rebecca Riordan, MVP

Designing Relational Database Systems
Microsoft SQL Server 2000 Programming Step by Step
Microsoft ADO.NET Step by Step

http://www.microsoft.com/mspress

Blessed are they who can laugh at themselves,
for they shall never cease to be amused...
 
Back
Top