Add rgb_to_hex snippet

This commit is contained in:
Isabelle Viktoria Maciohsek
2020-09-13 01:08:00 +03:00
parent b9735ecde0
commit 6d6bc52ea1

18
snippets/rgb_to_hex.md Normal file
View File

@ -0,0 +1,18 @@
---
title: rgb_to_hex
tags: 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.
```py
def rgb_to_hex(r, g, b):
return ("{:02X}" * 3).format(r, g, b)
```
```py
rgb_to_hex(255, 165, 1) # 'FFA501'
```