How do I convert a hex triplet to an RGB tuple and back?

后端 未结 10 1472
孤街浪徒
孤街浪徒 2020-12-14 00:24

I\'d like to convert a hex triplet to an RGB tuple and then convert a tuple to a hex triplet.

10条回答
  •  盖世英雄少女心
    2020-12-14 01:09

    def hex_to_int_color(v):
        if v[0] == '#':
            v = v[1:]
        assert(len(v) == 6)
        return int(v[:2], 16), int(v[2:4], 16), int(v[4:6], 16)
    
    def int_to_hex_color(v):
        assert(len(v) == 3)
        return '#%02x%02x%02x' % v
    

提交回复
热议问题