License New BSD license
Lines 5
Keywords
color (8) css (7) hex (1) hexadecimal (1) html (6) rgb (1)
Permissions
Owner: impiaaa
Viewable by Everyone
Editable by All Siafoo Users
Hide
Siafoo is here to make coding less frustrating and to save you time. Join Siafoo Now or Learn More

Hex2RGB Atom Feed 0

In Brief This function converts a hex color triplet, like those found in HTML or CSS, and converts it to a Python 3-integer tuple. I've found it to be useful for more than one program.
# 's
1def hex2rgb(hexColor):
2 if hexColor[0] == '#': hexColor = hexColor[1:]
3 elif hexColor[0:2].lower() == '0x': hexColor = hexColor[2:]
4 if len(hexColor) < 6: hexColor = hexColor[0]+'0'+hexColor[1]+'0'+hexColor[2]+'0'
5 return int(hexColor[0:2], 16), int(hexColor[2:4], 16), int(hexColor[4:6], 16)

This function converts a hex color triplet, like those found in HTML or CSS, and converts it to a Python 3-integer tuple. I've found it to be useful for more than one program.

Comments

over 8 years ago (13 Aug 2008 at 12:51 AM) by David Isaacson
Gah, seriously.... this is the *other* thing I was going to write : ) Crazy.
over 8 years ago (13 Aug 2008 at 08:39 PM) by Theodore Test
Quite handy!