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

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

    Trying to be pythonic:

    >>> rgbstr='aabbcc'
    >>> tuple(ord(c) for c in rgbstr.decode('hex'))
    (170, 187, 204)
    >>> tuple(map(ord, rgbstr.decode('hex'))
    (170, 187, 204)
    

    and

    >>> rgb=(12,50,100)
    >>> "".join(map(chr, rgb)).encode('hex')
    '0c3264'
    

提交回复
热议问题