Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

1.6 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
6821ebfd237de8297eaee799 Challenge 23: RGB to Hex 29 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}'