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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I convert a hex colour to RGB() 3

Status
Not open for further replies.

ChrisRChamberlain

Programmer
Mar 23, 2000
3,392
GB
FAQ184-4203 Bin - Dec - Hex - RGB Converter

written by slighthaze is a generic converter.


faq184-4204 How can I convert a hex colour to RGB(),

now showing on a screen near you, specifically converts a hex colour to RGB(), allowing you to use the function Hex2RGB() programatically.

Code for a RGB2Hex() function can be found in thread184-658948.



FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Chris,

Nice FAQ! I'm still waiting for Gagnon to write that FAQ on converting Twips - Pixels - Foxtels - Inches [smile] I could have sworn he said he'd have it done by today.

Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
slighthaze

Thanks for the star! [smile]

Looks like these FAQs/Thread covers most conversion aspects now.

FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
This is what I use to get Hex to Dec..
*****************************************
FUNCTION Hex2Dec
LPARAMETER cHex
LOCAL nDec
cHex = UPPER(cHex)
nDec = 0
FOR i=1 TO LEN(cHex)
nDec = nDec + ;
IIF(ISDIGIT(SUBSTR(cHex,i,1)), ;
VAL(SUBSTR(cHex,i,1)), ;
ASC(SUBSTR(cHex,i,1))-ASC('A') ;
+ 10) * 16 ^ (LEN(cHex)-i)
ENDFOR
RETURN nDec
ENDFUNC
****************************************
:)

ramani :)
(Subramanian.G)
 
Chris
*****
If you like that, you can edit and change your FAQ using that. But basicaly it is the same since the priciples are same.

Slighthaze
**********
Units of measures

One Inch = 6 Pica = 72 Points = 1440 Twips
One Pica = 12 Points = 240 Twips
One Point = 20 Twips

Pixels are screen dependent measure.
How to convert Twips to Pixels.. see the link

:)


ramani :)
(Subramanian.G)
 
Here's what I use to convert hex to dec:

FUNCTION Hex2Dec
LPARAMETER cHex
RETURN val("0x" + alltrim(cHex))
ENDFUNC

...or just to be safer...

FUNCTION Hex2Dec
LPARAMETER cHex
RETURN val("0x" + STRTRAN(STRTRAN(ALLTRIM(cHex),"#",""),"0x",""))
ENDFUNC



Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
Chris,

There appears to me to be an error in your FAQ. The red value and the blue value of the resultant RGB are swapped.

Also, here is my submission for the Hex2RGB function:

lcRGB = Hex2RGB([#ff2b5d])
? lcRGB

FUNCTION Hex2RGB
*!* Returns empty length string if Str2Convert is invalid
LPARAMETERS Str2Convert
LOCAL cTemp, cRVal, cGVal, cBVal, cReturn
cTemp = LOWER(STRTRAN(STRTRAN(ALLTRIM(Str2Convert),"#",""),"0x",""))
IF LEN(cTemp) != 6
cReturn = ""
ELSE
nRVal = "0x" + RIGHT(cTemp,2)
nGVal = "0x" + substr(cTemp,3,2)
nBVal = "0x" + left(cTemp,2)
IF TYPE("&nRVal") != "N" OR TYPE("&nGVal") != "N" OR TYPE("&nBVal") != "N"
cReturn = ""
ELSE
cReturn = "RGB(" + ALLTRIM(STR(&nRVal)) + ", " + ALLTRIM(STR(&nGVal)) + ", " + ALLTRIM(STR(&nBVal)) + ")"
ENDIF
ENDIF
RETURN cReturn

Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
...lower() is not necessary in code I posted above, was left in there from when I was using it for something else and wanted to make sure my hex string was in lowercase given what I learned in Thread184-658948

Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
SlightHaze

Since the Twips to Pixel has never been asked before in this forum, I'll only post my version (In VFP code) here :)
Code:
#DEFINE TWIPS_IN_CM 567
#DEFINE HWND_DESKTOP 0
#DEFINE LOGPIXELSX 88
#DEFINE LOGPIXELSY 90

?TwipsToMM(5300)
?TwipsPerPixelX()
?TwipsPerPixelY()

FUNCTION TwipsToMM
 LPARAMETER tnTwips
 RETURN INT(((tnTwips + 0.0005) / TWIPS_IN_CM) * 10)
ENDFUNC

FUNCTION TwipsPerPixelX
 LOCAL lnDC, lnTwipsPerpixelX
 DECLARE LONG GetDC IN "user32" LONG hwnd
 DECLARE LONG ReleaseDC IN "user32" LONG hwnd, LONG hdc
 DECLARE LONG GetDeviceCaps IN "gdi32" LONG hdc, LONG nIndex
 lnDC = GetDC(HWND_DESKTOP)
 lnTwipsPerpixelX = 1440 / GetDeviceCaps(lnDC, LOGPIXELSX)
 ReleaseDC(HWND_DESKTOP, lnDC)
 RETURN lnTwipsPerpixelX
ENDFUNC

FUNCTION TwipsPerPixelY
 LOCAL lnDC
 DECLARE LONG GetDC IN "user32" LONG hwnd
 DECLARE LONG ReleaseDC IN "user32" LONG hwnd, LONG hdc
 DECLARE LONG GetDeviceCaps IN "gdi32" LONG hdc, LONG nIndex
 lnDC = GetDC(HWND_DESKTOP)
 lnTwipsInPixelY = 1440 / GetDeviceCaps(lnDC, LOGPIXELSY)
 ReleaseDC(HWND_DESKTOP, lnDC)
 RETURN lnTwipsInPixelY
ENDFUNC

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Mike,

thread184-660118

Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
slighthaze

'There appears to me to be an error in your FAQ. The red value and the blue value of the resultant RGB are swapped.'

Can't replicate your error

lcRGB = Hex2RGB([#000000])

returns

RGB(0,0,0)


lcRGB = Hex2RGB([#ff0000])

returns

RGB(255,0,0)


lcRGB = Hex2RGB([#ffff00])

returns

RGB(255,255,0)


FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Hello All.

I did not see anyone here mention the RGBComp function in FoxTools.fll:

SET LIBRARY TO HOME() + 'FoxTools.fll' ADDITIVE
lnColor = VAL( '0x' + ALLTRIM( lcHex )
lnRed = 0
lnBlue = 0
lnGreen = 0
IF ( RGBComp, lnColor, @lnRed, @lnBlue, @lnGreen )
*** construct the color string here from
*** the return values
ENDIF




Marcia G. Akins
 
Hello All.

Sorry abou the type in my previous message. This line:

IF ( RGBComp, lnColor, @lnRed, @lnBlue, @lnGreen )

should read like this:

IF RGBComp( lnColor, @lnRed, @lnBlue, @lnGreen )


Marcia G. Akins
 
Chris,

The code below illustrates what I am talking about...the code I am using was just cut-n-pasted from the FAQ:
Code:
lcRGB = Hex2RGB([#112233])
? lcRGB
? TRANSFORM(&lcRGB, "@0") + " should be: 0x00112233" 

lcRGB = Hex2RGB([#aabbcc])
? lcRGB
? TRANSFORM(&lcRGB, "@0") + " should be: 0x00AABBCC" 

****************
FUNCTION Hex2RGB
LPARAMETERS Str2Convert
LOCAL i, lnPos

DO CASE
CASE SUBSTR(Str2Convert,1,1) # [#]
    MESSAGEBOX([Invalid string])
    Str2Return = [Error!]
CASE LEN(Str2Convert) # 7
    MESSAGEBOX([Invalid string])
    Str2Return = [Error!]
OTHE
    Str2Return = [RGB(]
    lnPos = 2
    FOR i = 1 TO 3
        lcValue = Hex2Dec(SUBSTR(Str2Convert,lnPos,2))

        IF i # 3
            Str2Return = Str2Return + lcValue + [,]
        ELSE
            Str2Return = Str2Return + lcValue + [)]        
        ENDI
        lnPos = lnPos + 2
    ENDF
ENDCASE

RETURN Str2Return

****************
FUNCTION Hex2Dec
LPARAMETERS HexString
LOCAL i, llcChar, lnLen, lnSum, lnPos

lnLen =    LEN(HexString)
lnSum =    0
lnPos =    0
FOR i =    1 TO lnLen
    lcChar = SUBSTR(HexString,lnLen-lnPos,1)
    DO CASE
    CASE UPPER(lcChar) = [A]
        lcChar = [10]
    CASE UPPER(lcChar) = "B"
        lcChar = [11]
    CASE UPPER(lcChar) = [C]
        lcChar = [12]
    CASE UPPER(lcChar) = [D]
        lcChar = [13]
    CASE UPPER(lcChar) = [E]
        lcChar = [14]
    CASE UPPER(lcChar) = [F]
        lcChar = [15]
    ENDCASE
    lnSum =    lnSum +    VAL(UPPER(lcChar)) * 16^(i-1)
    lnPos =    lnPos +    1
ENDFOR

RETURN TRANSFORM(lnSum)

Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
slighthaze

The function Hex2RGB() function will only return the RGB() values of Windows colours expressed in hex.

In the FAQ, you will find the sentence - 'Depending on your use, you may need to add additional error trapping to validate the string passed to the function Hex2RGB()'.

Assuming you had that in place, then the strings you have passed would have caused an error, as they are not Windows colours expressed in hex.

I will edit the FAQ to clear up any misunderstanding on that issue.

FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Chris,

I am getting very confused...ok so let's do this with #FF0000 which is Blue and #0000FF which is Red...

lcRGB = Hex2RGB([#ff0000]) &&Blue
? lcRGB
? TRANSFORM(&lcRGB, "@0") + "Red should be: 0x00FF0000 Blue"

lcRGB = Hex2RGB([#0000ff]) &&Red
? lcRGB
? TRANSFORM(&lcRGB, "@0") + " Blue should be: 0x000000FF Red"

...now I could use the extra code that you posted in the other thread to make them come out in lowercase with a # at the front of them, but I am hoping you can see what I mean. The Red and Blue values of the resultant RGB are definately switched, or I am really really missing something here.

Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
All we really need to do is look at one of these:
#ff0000 is the hex for Blue but the generated RGB string is wrong, it shows RGB(255,0,0) which is Red...

?Hex2RGB([#ff0000]) && Should be RGB(0,0,255)

Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
Chris,

My apologies. I just went and checked out #ff0000 in a webpage and I stand corrected, even though 0x00ff0000 is blue in VFP, #ff0000 is definately Red in a webpage. The least significant and the most significant have been switched for webpages I see. Your FAQ is correct as it stands and I apologize for any inconvenience my confusion may have caused.

Also, for my alternate Hex2RGB function to be correct it would need to be...

lcRGB = Hex2RGB([#ff0000])
? lcRGB

FUNCTION Hex2RGB
*!* Returns empty length string if Str2Convert is invalid
LPARAMETERS Str2Convert
LOCAL cTemp, cRVal, cGVal, cBVal, cReturn
cTemp = STRTRAN(ALLTRIM(Str2Convert),"#","")
IF LEN(cTemp) != 6
cReturn = ""
ELSE
nRVal = "0x" + Left(cTemp,2)
nGVal = "0x" + Substr(cTemp,3,2)
nBVal = "0x" + Right(cTemp,2)
IF TYPE("&nRVal") != "N" OR TYPE("&nGVal") != "N" OR TYPE("&nBVal") != "N"
cReturn = ""
ELSE
cReturn = "RGB(" + ALLTRIM(STR(&nRVal)) + ", " + ALLTRIM(STR(&nGVal)) + ", " + ALLTRIM(STR(&nBVal)) + ")"
ENDIF
ENDIF
RETURN cReturn

Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
slighthaze

Glad you have resolved the issues. [smile]

The reason for not offering more formal string validation in the FAQ is that I have assumed, apart from typos etc, that the hex string is a 'legal' Windows colour, and left it up to a developer to add their own validation code should that not be the case.

FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top