curriculum/challenges/english/blocks/daily-coding-challenges-python/6821ebfd237de8297eaee799.md
Given a CSS rgb(r, g, b) color string, return its hexadecimal equivalent.
Here are some example outputs for a given input:
| Input | Output |
|---|---|
"rgb(255, 255, 255)" | "#ffffff" |
"rgb(1, 2, 3)" | "#010203" |
# followed by six characters. Don't use any shorthand values.rgb_to_hex("rgb(255, 255, 255)") should return "#ffffff".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(rgb_to_hex("rgb(255, 255, 255)"), "#ffffff")`)
}})
rgb_to_hex("rgb(1, 11, 111)") should return "#010b6f".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(rgb_to_hex("rgb(1, 11, 111)"), "#010b6f")`)
}})
rgb_to_hex("rgb(173, 216, 230)") should return "#add8e6".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(rgb_to_hex("rgb(173, 216, 230)"), "#add8e6")`)
}})
rgb_to_hex("rgb(79, 123, 201)") should return "#4f7bc9".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(rgb_to_hex("rgb(79, 123, 201)"), "#4f7bc9")`)
}})
def rgb_to_hex(rgb):
return rgb
def rgb_to_hex(rgb):
import re
match = re.findall(r'\d+', rgb)
r, g, b = [max(0, min(255, int(x))) for x in match[:3]]
return f'#{r:02x}{g:02x}{b:02x}'