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

smig

Registered User.
Local time
Today, 22:26
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:
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:26
Joined
Oct 29, 2018
Messages
21,473
What is the value of IColor?
 

smig

Registered User.
Local time
Today, 22:26
Joined
Nov 25, 2009
Messages
2,209
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:
 

missinglinq

AWF VIP
Local time
Today, 15:26
Joined
Jun 20, 2003
Messages
6,423
...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
 

isladogs

MVP / VIP
Local time
Today, 20:26
Joined
Jan 14, 2017
Messages
18,219
Try the code in my colour converter. It works!
 
Last edited:

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 14:26
Joined
Feb 28, 2001
Messages
27,183
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?
 

smig

Registered User.
Local time
Today, 22:26
Joined
Nov 25, 2009
Messages
2,209
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 ?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 14:26
Joined
Feb 28, 2001
Messages
27,183
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.
 

isladogs

MVP / VIP
Local time
Today, 20:26
Joined
Jan 14, 2017
Messages
18,219
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
 

smig

Registered User.
Local time
Today, 22:26
Joined
Nov 25, 2009
Messages
2,209
Thank you all

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

Users who are viewing this thread

Top Bottom