rgb2hex

Convert RGB to hex colors - Python

a) convert range 0 ...255 to 00 ...ff

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'

b) convert plot color range 0 ...1 to 00 ...ff

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'

c) using python package webcolors

rgb_to_hex((120, 0, 255))

u'#7800ff'



read more

https://pyformat.info/

https://matplotlib.org/api/colors_api.html

http://webcolors.readthedocs.io/