HEX color to INT value

Post Reply
User avatar
Otto
Posts: 4470
Joined: Fri Oct 07, 2005 7:07 pm
Contact:

HEX color to INT value

Post by Otto »

Hello,
is there a function to change the color HEX value to an INT value.
Thank you in advance
Otto
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org

********************************************************************
User avatar
Silvio.Falconi
Posts: 4956
Joined: Thu Oct 18, 2012 7:17 pm

Re: HEX color to INT value

Post by Silvio.Falconi »

https://toolset.mrwebmaster.it/colori/hex-to-rgb.html
http://www.javascripter.net/faq/rgbtohex.htm


From manuel calero solis

Code: Select all

FUNCTION RgbToRgbHex( nColorRgb )

   local cRgbHex  := ""

   cRgbHex        += "#"
   cRgbHex        += NumToHex( nRgbRed( nColorRgb ), 2 )
   cRgbHex        += NumToHex( nRgbGreen( nColorRgb ), 2 )
   cRgbHex        += NumToHex( nRgbBlue( nColorRgb ), 2 )

RETURN cRgbHex
Last edited by Silvio.Falconi on Tue Dec 25, 2018 8:02 pm, edited 1 time in total.
I use : FiveWin for Harbour August 2020 (Revision) - Harbour 3.2.0dev (r1712141320) - Bcc7.30 - xMate ver. 1.15.3 - PellesC
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: HEX color to INT value

Post by nageswaragunupudi »

If you have the color in the hex-format "#rrggbb"

then

nRGB( cHexClr ) --> nColor
Regards

G. N. Rao.
Hyderabad, India
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: HEX color to INT value

Post by Antonio Linares »

There is also:

function nHex( cHex ) --> nValue
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: HEX color to INT value

Post by nageswaragunupudi »

The difference is this:

Windows COLORREF constants, when expressed in hex are 0xBBGGRR
If we have such a color string "BBGGRR"
then nHex( cHex ) or HEXTONUM( cHex ) would convert the hex string to numeric value.

Mostly in web and other platforms colors are expressed as "#RRGGBB"
In such cases nHex( cHex ) or HEXTONUM( cHex ) do not give the correct color constant.

nRGB( cHexClr ) gives the correct color constant.

What is important is the source of cHex Mr Otto is referring to.
Regards

G. N. Rao.
Hyderabad, India
User avatar
Otto
Posts: 4470
Joined: Fri Oct 07, 2005 7:07 pm
Contact:

Re: HEX color to INT value

Post by Otto »

Dear Mr. Rao, dear Antonio,

I am sorry I asked the wrong way.

I have the INT value, for example: 16711680 and I need the HEX value: #0000ff
NumToHex(16711680 ) gives #ff0000.

Do I only have to convert "BBGGRR" to "#RRGGBB".
Best regards
Otto
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org

********************************************************************
User avatar
Giovany Vecchi
Posts: 129
Joined: Mon Jun 05, 2006 9:39 pm
Location: Brasil

Re: HEX color to INT value

Post by Giovany Vecchi »

OI Otto, I do not know if that's what you're looking for.

Code: Select all

FUNCTION RGB_TO_HTML(f_nColor)
    Local cColorReturn := "#", aTmpRGB := {}
    
    aadd(aTmpRGB,decToHex(nRGBRed(f_nColor)))
    aadd(aTmpRGB,decToHex(nRGBgreen(f_nColor)))
    aadd(aTmpRGB,decToHex(nRGBBlue(f_nColor)))
    
    If Len(aTmpRGB[1]) == 1
        aTmpRGB[1] := "0"+aTmpRGB[1]
    EndIf
    
    If Len(aTmpRGB[2]) == 1 
        aTmpRGB[2] := "0"+aTmpRGB[2]
    EndIf

    If Len(aTmpRGB[3]) == 1
        aTmpRGB[3] := "0"+aTmpRGB[3]
    EndIf

    cColorReturn += (aTmpRGB[1]+aTmpRGB[2]+aTmpRGB[3]) 

Return cColorReturn
 
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: HEX color to INT value

Post by nageswaragunupudi »

Mr. Otto
Do I only have to convert "BBGGRR" to "#RRGGBB"
Yes, exactly.

For your purpose, you can use the function RGB_TO_HTML() posted by Mr. Giovanny Vecchi or the function RgbToRgbHex() posted by Mr. Silvio.


You can also use this function:

Code: Select all

function CLRTOHTML( nClr )

   local cHex  := LOWER( NUMTOHEX( nClr, 6 ) )

return "#" + RIGHT( cHex, 2 ) + SUBSTR( cHex, 3, 2 ) + LEFT( cHex, 2 )
 
? CLRTOHTML( CLR_HRED ) // --> #ff0000
Regards

G. N. Rao.
Hyderabad, India
User avatar
Otto
Posts: 4470
Joined: Fri Oct 07, 2005 7:07 pm
Contact:

Re: HEX color to INT value

Post by Otto »

Dear Mr. Rao,
thank you for the function.
Now I have more or less my offline colors also in the PHP scheduler.
Best regards
Otto

Image
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org

********************************************************************
User avatar
Otto
Posts: 4470
Joined: Fri Oct 07, 2005 7:07 pm
Contact:

Re: HEX color to INT value

Post by Otto »

Best regards
Otto

Image
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org

********************************************************************
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: HEX color to INT value

Post by nageswaragunupudi »

Look Great
Regards

G. N. Rao.
Hyderabad, India
Post Reply