如何将十六进制三元组转换为RGB元组并返回?

rec*_*gle 31 python colors

我想将十六进制三元组转换为RGB元组,然后将元组转换为十六进制三元组.

ada*_*amk 73

>>> import struct
>>> rgbstr='aabbcc'
>>> struct.unpack('BBB',rgbstr.decode('hex'))
(170, 187, 204)
Run Code Online (Sandbox Code Playgroud)

>>> rgb = (50,100,150)
>>> struct.pack('BBB',*rgb).encode('hex')
'326496'
Run Code Online (Sandbox Code Playgroud)

  • 在python 3.0中,将`str.decode('hex')`替换为`bytes.fromhex(str)`.对于另一个方向,使用`binascii.hexlify`在打包后转换回字符串. (9认同)
  • 如果我没弄错的话,`binascii.hexlify`会产生一个字节对象.你需要调用`.decode('utf-8')`来得到一个字符串,对吗?总命令是`binascii.hexlify(struct.pack('BBB',*rgb)).decode('utf-8')`.我认为''%02x%02x%02x'%rgb`更简单,并且具有处理浮点值和整数的好处. (3认同)

eci*_*cik 25

试图成为pythonic:

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

>>> rgb=(12,50,100)
>>> "".join(map(chr, rgb)).encode('hex')
'0c3264'
Run Code Online (Sandbox Code Playgroud)

  • ```rgbstr.decode('hex')```它对python3来说会怎么样? (2认同)

Eli*_*ria 18

我找到了一个简单的方法:

red, green, blue = bytes.fromhex("aabbcc")
Run Code Online (Sandbox Code Playgroud)

  • `bytearray.fromhex()`for python 2.7 (5认同)

mar*_*eau 17

您可以使用具有一些切片和移位的查找表 - 所有相对快速的操作 - 来创建一些在Python 2和3中都可以保持不变的函数:

_NUMERALS = '0123456789abcdefABCDEF'
_HEXDEC = {v: int(v, 16) for v in (x+y for x in _NUMERALS for y in _NUMERALS)}
LOWERCASE, UPPERCASE = 'x', 'X'

def rgb(triplet):
    return _HEXDEC[triplet[0:2]], _HEXDEC[triplet[2:4]], _HEXDEC[triplet[4:6]]

def triplet(rgb, lettercase=LOWERCASE):
    return format(rgb[0]<<16 | rgb[1]<<8 | rgb[2], '06'+lettercase)

if __name__ == '__main__':
    print('{}, {}'.format(rgb('aabbcc'), rgb('AABBCC')))
    # -> (170, 187, 204), (170, 187, 204)

    print('{}, {}'.format(triplet((170, 187, 204)),
                          triplet((170, 187, 204), UPPERCASE)))
    # -> aabbcc, AABBCC

    print('{}, {}'.format(rgb('aa0200'), rgb('AA0200')))
    # -> (170, 2, 0), (170, 2, 0)

    print('{}, {}'.format(triplet((170, 2, 0)),
                          triplet((170, 2, 0), UPPERCASE)))
    # -> aa0200, AA0200
Run Code Online (Sandbox Code Playgroud)


tsv*_*kas 11

matplotlib

matplotlib使用RGB元组,其值介于0和1之间:

from matplotlib.colors import hex2color, rgb2hex

hex_color = '#00ff00'
rgb_color = hex2color(hex_color)
hex_color_again = rgb2hex(rgb_color)
Run Code Online (Sandbox Code Playgroud)

二者rgb_colorhex_color处于由格式可接受matplotlib.

webcolors

html使用RGB元组,其值介于0到255之间.

你可以使用模块webcolors在它们之间进行转换,使用这些函数hex_to_rgb,rgb_to_hex


pyf*_*unc 10

将rgb转换为十六进制的一种非常简单的方法

>>> rgb = (255, 255, 255)
>>> r, g , b = rgb
>>> hex(r)
'0xff'
>>> hex(r) + hex(g)[2:] + hex(b)[2:]
'0xffffff'
>>>
Run Code Online (Sandbox Code Playgroud)

将Hex转换为rgb的简单方法

>>> h  = '0xffffff'
>>> h1, h2, h3 = h[0:4], '0x' + h[4:6], '0x' + h[6:8]
>>> h1, h2, h3
('0xff', '0xff', '0xff')
>>> r, g , b = int(h1, 16), int(h2, 16), int(h3, 16)
>>> r, g, b
(255, 255, 255)
Run Code Online (Sandbox Code Playgroud)

使用提供这些功能的模块:webcolors

>>> hex_to_rgb('#000080')
(0, 0, 128)
>>> rgb_to_hex((255, 255, 255))
'#ffffff'
Run Code Online (Sandbox Code Playgroud)

功能文档:

hex_to_rgb(hex_value)将十六进制颜色值转换为适合在指定该颜色的rgb()三元组中使用的3元组整数.

rgb_to_hex(rgb_triplet):将适合在rgb()颜色三元组中使用的3元组整数转换为该颜色的标准化十六进制值.


dan*_*rth 5

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
Run Code Online (Sandbox Code Playgroud)


Int*_*nti 5

HEX 到 RGB 元组

>>> tuple(bytes.fromhex('61559a'))
(97, 85, 154)
Run Code Online (Sandbox Code Playgroud)

RGB 元组到 HEX

>>> bytes((97, 85, 154)).hex()
'61559a'
Run Code Online (Sandbox Code Playgroud)

无需进口!


这是什么魔法?!

由于字节对象是整数序列(类似于元组),对于字节对象 b,b[0] 将是一个整数,而 b[0:1] 将是一个长度为 1 的字节对象

...

字节对象的表示使用文字格式 (b'...'),因为它通常比例如 bytes([46, 46, 46]) 更有用。您始终可以使用 list(b) 将字节对象转换为整数列表。

来源:https : //docs.python.org/3/library/stdtypes.html#bytes-objects