Files
30-seconds-of-code/snippets/rgb_to_hex.md
2020-09-15 16:25:15 +03:00

442 B

title, tags
title tags
rgb_to_hex string,math,intermediate

Converts the values of RGB components to a hexadecimal color code.

  • Create a placeholder for a zero-padded hexadecimal value using "{:02X}", copy it three times.
  • Use str.format() on the resulting string to replace the placeholders with the given values.
def rgb_to_hex(r, g, b):
  return ("{:02X}" * 3).format(r, g, b)
rgb_to_hex(255, 165, 1) # 'FFA501'