how do i get RGB from HEX?

Robin Becker robin at jessikat.fsnet.co.uk
Fri Mar 22 11:49:24 EST 2002


In article <a7flgu$cpg$1 at newsg1.svr.pol.co.uk>, G. Willoughby
<sab at NOSPAM.freeuk.com> writes
>Hi,
>    i have been trying to do this for to long now, i know there is an easy
>way, please somebody put me out of my misery! i need to get the RGB value of
>a HEX number (eg, #DCDCDC). can anyone help?
>
>thanks,
>
>G. Willoughby
>
>
split into pairs after stripping the #

>>> def rgb(h):
...     n = eval('0x'+h[1:])
...     return (n>>16)&0xff, (n>>8)&0xff, n&0xff
... 
>>> rgb('#DCDC0a')
(220, 220, 220)
>>> rgb('#DCDC0a')
(220, 220, 10)
>>> 

I'm sure there's probably some better way, but the above works.
-- 
Robin Becker



More information about the Python-list mailing list