From 6d6bc52ea17862015c8a2d6008b64b34357971ab Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Sun, 13 Sep 2020 01:08:00 +0300 Subject: [PATCH] Add rgb_to_hex snippet --- snippets/rgb_to_hex.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 snippets/rgb_to_hex.md diff --git a/snippets/rgb_to_hex.md b/snippets/rgb_to_hex.md new file mode 100644 index 000000000..15132e904 --- /dev/null +++ b/snippets/rgb_to_hex.md @@ -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' +```