convert rgb to hex
'#{:02x}{:02x}{:02x}'.format( 120, 0 , 255 )
'#7800ff'
convert rgba (a=alpha) to hex
'#{:02x}{:02x}{:02x}{:02x}'.format( 120, 0 , 255, 128 )
'#7800ff80'
convert numbers between 0 and 1 into hex color code
import matplotlib
matplotlib.colors.to_hex([ 0.47, 0.0, 1.0 ])
u'#7800ff'
same, but with additional transparent (alpha) value '0.5'
matplotlib.colors.to_hex([ 0.47, 0.0, 1.0, 0.5 ], keep_alpha=True)
u'#7800ff80'
rgb_to_hex((120, 0, 255))
u'#7800ff'
read more