RGB to hex

Python, String, Math · Nov 2, 2020

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'

More like this

  • Hex to RGB

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

    Python, String · Sep 15, 2020

  • String to words

    Converts a given string into a list of words.

    Python, String · Nov 2, 2020

  • String to slug

    Converts a string to a URL-friendly slug.

    Python, String · Oct 25, 2020