Converting Hex to RGB value in Python

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

    This function will return the RGB values in float from a Hex code.

    def hextofloats(h):
        '''Takes a hex rgb string (e.g. #ffffff) and returns an RGB tuple (float, float, float).'''
        return tuple(int(h[i:i + 2], 16) / 255. for i in (1, 3, 5)) # skip '#'
    

    This function will return Hex code from RGB value.

    def floatstohex(rgb):
        '''Takes an RGB tuple or list and returns a hex RGB string.'''
        return f'#{int(rgb[0]*255):02x}{int(rgb[1]*255):02x}{int(rgb[2]*255):02x}'
    

提交回复
热议问题