Converting Hex to RGB value in Python

前端 未结 10 1100
一生所求
一生所求 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:22

    The following function will convert hex string to rgb values:

    def hex_to_rgb(hex_string):
        r_hex = hex_string[1:3]
        g_hex = hex_string[3:5]
        b_hex = hex_string[5:7]
        return int(r_hex, 16), int(g_hex, 16), int(b_hex, 16)
    

    This will convert the hexadecimal_string to decimal number

    int(hex_string, 16)
    

    For example:

    int('ff', 16) # Gives 255 in integer data type
    

提交回复
热议问题