INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Usefull Functions & Procedures

How can I convert a hex colour to RGB() by ChrisRChamberlain
Posted: 21 Sep 03 (Edited 21 Sep 03)

If you want to convert a colour expressed in hex to RGB() format try the following.

Depending on your needs and use, you may need to add additional error trapping to validate the string passed to the function Hex2RGB(), as the function assumes, apart from typos, that the hex string passed is a 'legal' Windows colour.

Thread184-658948 discusses a RGB2Hex() function, and FAQ184-4203 is a Bin - Dec - Hex - RGB Converter
    
lcRGB = Hex2RGB([#ff00ff])
? lcRGB

****************
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)

Have fun!

Back to Microsoft: Visual FoxPro FAQ Index
Back to Microsoft: Visual FoxPro Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close