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

后端 未结 10 1470
孤街浪徒
孤街浪徒 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:03

    >>> import struct
    >>> rgbstr='aabbcc'
    >>> struct.unpack('BBB',rgbstr.decode('hex'))
    (170, 187, 204)
    

    and

    >>> rgb = (50,100,150)
    >>> struct.pack('BBB',*rgb).encode('hex')
    '326496'
    

提交回复
热议问题