Files
30-seconds-of-code/snippets/rgb_to_hex.md
Isabelle Viktoria Maciohsek cbc78ee450 Bake dates into snippets
2021-06-13 19:38:10 +03:00

521 B

title, tags, firstSeen, lastUpdated
title tags firstSeen lastUpdated
rgb_to_hex string,math,intermediate 2020-09-13T01:08:00+03:00 2020-11-02T19:28:27+02:00

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

  • Create a placeholder for a zero-padded hexadecimal value using '{:02X}' and 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'