Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying color select dialog and saving hex nbr in db

Status
Not open for further replies.

Maim

Programmer
Jun 25, 1999
106
CA
2 questions...

I'm trying to create a setup form where a user can select a color, the code would save the hex value in a field and an ASP page would read thid field to color the text applicable...

This is what I have...

Private Sub Command1_Click()
CommonDialog1.flags = cdlCCRGBInit
CommonDialog1.ShowColor
text1.ForeColor = CommonDialog1.Color
Dim txt As String
txt = Hex(CommonDialog1.Color)
While Len(Trim(txt)) < 6 'Make sure number is 6 chars
txt = txt & &quot;0&quot;
Wend
text1.Caption = txt 'for test purposes
End Sub

1.
I tried checking the result on an HTML page but sometimes I get the correct number which would display the correct color, sometimes I don't(???).

I also tried leaving txt as is from Hex(CommonDialog1.Color) and also adding the 0s to the beginning of the string. Same result, sometimes the number's good, sometimes not.

2.
The above constant for Flags, cdlCCRGBInit, displays a dialog with all possible colors on my system, if I use cdlCCPreventFullOpen, only the custom colors is disabled. What I would like to have is a dialog like the one found in Display Properties->Appearance->Color, which is much neater. Anyone know how I can get this one to display instead?

Thanks in advance ;)
 
The color from the dialog box is returned as a Long Variable. In reality it is not a Long, but a group of four bytes (one for Red,Green, Blue and an additional Pad byte) which should be converted separately.

The Hex function takes the total value of the Long variable and converts it into its Hexadecimal equivalent, which is not what you want to do.

Following routine performs the color conversion correctly:
Private Type rv_typLongColor
LongColor As Long
End Type

Private Type rv_typRGBColor

'Byte Color Index is 0 for Red, 1 for Green, 2 for Blue
' 3 for the Pad Byte (or System Color Flag)

bytColor(3) As Byte
End Type

Private Function LongColor2HexRGB(lngColor As Long) As String


'* Purpose : Transforms a Long Variable containing a color code into
'* a 10 character string, representing its Hexadecimal
'* string equivalent
'* Accepts : lngColor - A color code, specified as a long variable
'* Returns : A 10 Character string that contains &quot;&HPPBBGGRR&quot;, where PP
'* stands for a Pad Character, that is normally &quot;0&quot;.
'* BB, GG, RR are the Blue, Green and Red color codes.


Dim typColorIn As rv_typLongColor
Dim typColorOut As rv_typRGBColor

typColorIn.LongColor = lngColor
LSet typColorOut = typColorIn

Dim strColorOut As String * 8

With typColorOut
Dim intIndex As Integer

For intIndex = 0 To 3
Mid$(strColorOut, 2 * intIndex + 1, 2) = Format$(Hex(.bytColor(3 - intIndex)), &quot;00&quot;)
Next intIndex

End With

LongColor2HexRGB = &quot;&H&quot; & strColorOut

End Function


Your program could then be simplified as follows:
Private Sub Command1_Click()
CommonDialog1.flags = cdlCCRGBInit
CommonDialog1.ShowColor
text1.ForeColor = CommonDialog1.Color
Dim txt As String

txt = LongColor2HexRGB(CommonDialog1.Color)

text1.Caption = txt 'for test purposes

End Sub


I do not have an answer for the second question. _________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Thanks rvBasic, I had to change your code a little because there were too many numbers returned...

Since HTML colors are applied as &quot;#RRGGBB&quot; and the output of the above formula was &quot;&H00BBGGRR&quot;, I had to format the output a little more.

Once again, thanks :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top