Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert RGBA tuple to hex with matplotlib

Conversion of rgb to hex is straightforward with matplotlib, but the alpha value seems to be ignored.

For example the output of the two are the same:

mpl.colors.rgb2hex((0.1, 0.1, 0.1, 0.1))
mpl.colors.rgb2hex((0.1, 0.1, 0.1, 0.8))

Which creates the same hex code (#1a1a1a), which isn’t what I’d like. I’m not sure exactly which codes I would like – but I know I would like to be able to have a "lighter" hex code if the alpha value is lower.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Typically I might use an alpha parameter to control the lightness/saturation (sorry I don’t have very good language for describing colours) – so it’s important that I’m able to do so – but I need to be able to do so by converting RGBA tuples to hex.

>Solution :

You need to specify keep_alpha=True:

mpl.colors.rgb2hex((0.1, 0.1, 0.1, 0.1), keep_alpha=True)
# out: #1a1a1a1a
mpl.colors.rgb2hex((0.1, 0.1, 0.1, 0.8), keep_alpha=True)
# out: #1a1a1acc
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading