mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-11 09:01:44 -04:00
Co-authored-by: Kolade Chris <65571316+Ksound22@users.noreply.github.com> Co-authored-by: Huyen Nguyen <25715018+huyenltnguyen@users.noreply.github.com>
1.6 KiB
1.6 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 6821ecc5237de8297eaee7a5 | Python Challenge 23: RGB to Hex | 29 | python-challenge-23 |
--description--
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" |
- Make any letters lowercase.
- Return a
#followed by six characters. Don't use any shorthand values.
--hints--
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")`)
}})
--seed--
--seed-contents--
def rgb_to_hex(rgb):
return rgb
--solutions--
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}'