Files
30-seconds-of-code/snippets/rgb-to-hex.md
Angelos Chalaris f6a215e9e3 Kebab file names
2023-04-27 22:00:06 +03:00

22 lines
524 B
Markdown

---
title: RGB to hex
tags: string,math
cover: campfire
firstSeen: 2020-09-13T01:08:00+03:00
lastUpdated: 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.
```py
def rgb_to_hex(r, g, b):
return ('{:02X}' * 3).format(r, g, b)
```
```py
rgb_to_hex(255, 165, 1) # 'FFA501'
```