30秒学会 Python 片段 · 2022年12月16日

30秒学会 Python 片段 – Hex to RGB

Converts a hexadecimal color code to a tuple of integers corresponding to its RGB components.

  • Use a list comprehension in combination with int() and list slice notation to get the RGB components from the hexadecimal string.
  • Use tuple() to convert the resulting list to a tuple.

代码实现

def hex_to_rgb(hex):
  return tuple(int(hex[i:i+2], 16) for i in (0, 2, 4))

使用样例

hex_to_rgb('FFA501') # (255, 165, 1)

翻译自:https://www.30secondsofcode.org/python/s/hex-to-rgb