Solved Is VBA.Hex(lColor) give a wrong color code ?

smig

Registered User.
Local time
Today, 23:39
Joined
Nov 25, 2009
Messages
2,209
is it possible that VBA.Hex(lColor) give a wrong color code ?
Seems I have to take it like Last2 - Middle - First2 :rolleyes:
 
What is the value of IColor?
 
What is the value of IColor?
As example

The correct Color code for RGB(74, 169, 194) is 12757322
The correct Hex code is #4AA9C2

VBA.Hex(12757322) will result in C2A94A :oops:
 
...Seems I have to take it like Last2 - Middle - First2 :rolleyes:
Found this Function in my archive files, and the Comments included seems to say that the answer is 'Yes.'
Code:
Public Function HexColor(strHex As String) As Long

'converts Hex string to long number, for colors

'the leading # is optional

'example usage

'Me.iSupplier.BackColor = HexColor("FCA951")

'Me.iSupplier.BackColor = HexColor("#FCA951")

'the reason for this function is to programmatically use the
'Hex colors generated by the color picker.
 'The trick is, you need to reverse the first and last hex of the
'R G B combination and convert to Long
'so that if the color picker gives you this color #FCA951
'to set this in code, we need to return CLng(&H51A9FC)

Dim strColor As String
Dim strR As String
Dim strG As String
Dim strB As String

'strip the leading # if it exists

If Left(strHex, 1) = "#" Then

strHex = Right(strHex, Len(strHex) - 1)

End If

'reverse the first two and last two hex numbers of the R G B values

strR = Left(strHex, 2)

strG = Mid(strHex, 3, 2)

strB = Right(strHex, 2)

strColor = strB & strG & strR

HexColor = CLng("&H" & strColor)

End Function
 
Try the code in my colour converter. It works!
 
Last edited:
As it turns out, Access and Excel can use the same general scheme of supplying red, green, and blue - except that one of them reverses red and blue when compared to the other. In which library did you find that routine?
 
As it turns out, Access and Excel can use the same general scheme of supplying red, green, and blue - except that one of them reverses red and blue when compared to the other. In which library did you find that routine?
Ouch :oops:

How can I know where the VBA.Hex(lColor) is ?
 
To find where you got the routine, open a VBA code page. Then open Object Explorer and search for the HEX routine. The response will tell you where it is by naming the containing library. You DO realize that the return is a string value?


This seems to be coming from a general library, neither Excel-specific nor Access-specific. At least that's what I see when I read the article.

The correct Color code for RGB(74, 169, 194) is 12757322
The correct Hex code is #4AA9C2

VBA.Hex(12757322) will result in C2A94A

Well.... it should. The correct HEX code, as confirmed using the Windows Calculator in "Programming" mode, IS C2A94A - so rather obviously, you have a "swap red and blue" case. Have you considered using RGB( 194, 169, 74) for your conversion? The color you get depends on the values you enter, so enter them in the other order.
 
Taking the values provided by Doc as an example, the screenshot below shows the conversion values: OLE=>RGB=>HEX

Capture.PNG



Enter any one set of values and the other two are automatically displayed.
Or you can use the sliders to choose a colour and from that get the three sets of values.

The screenshot is from the utility I mentioned in post #5 & always gives the correct values
 
Thank you all

Once I know the Red and Blue are swapped I can live with that :)
 

Users who are viewing this thread

Back
Top Bottom