Converting Hex to RGB value in Python

前端 未结 10 1082
一生所求
一生所求 2020-12-04 21:19

Working off Jeremy\'s response here: Converting hex color to RGB and vice-versa I was able to get a python program to convert preset colour hex codes (example #B4FBB8), howe

10条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-04 21:45

    Just another option: matplotlib.colors module.

    Quite simple:

    >>> import matplotlib.colors
    >>> matplotlib.colors.to_rgb('#B4FBB8')
    (0.7058823529411765, 0.984313725490196, 0.7215686274509804)
    

    Note that the input of to_rgb need not to be hexadecimal color format, it admits several color formats.

    You can also use the deprecated hex2color

    >>> matplotlib.colors.hex2color('#B4FBB8')
    (0.7058823529411765, 0.984313725490196, 0.7215686274509804)
    

    The bonus is that we have the inverse function, to_hex and few extra functions such as, rgb_to_hsv.

提交回复
热议问题