Colours in charts and on the web are written in hex. Convert them to the rgb(...) form.
What to build
A program that converts hex colours to RGB.
Requirements
- For each colour print
rgb(R, G, B), with each channel from 0 to 255. - Accept six-digit colours (#RRGGBB) and three-digit shorthand (#RGB, where each digit is doubled), in upper or lower case.
Input
Line 1: n. Then n colours, one per line.
Output
n lines.
Constraints
1 ≤ n ≤ 100; every colour is valid.
Example
Input:
2
#1C3028
#ffffff
Output:
rgb(28, 48, 40)
rgb(255, 255, 255)
Why it works
Each pair of hexadecimal digits is one byte, a number from 0 to 255 for one channel, and the shorthand repeats each digit to make that pair.
Examples
Example 1 · Input
2 #1C3028 #ffffff
Expected output
rgb(28, 48, 40) rgb(255, 255, 255)
Example 2 · Input
1 #000
Expected output
rgb(0, 0, 0)
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
int("1C", 16) converts a hexadecimal pair to a number.
Hint 2
A three-digit colour such as #abc means #aabbcc.
Solution
Show one correct solution (try it yourself first)
n = int(input())
for _ in range(n):
h = input()[1:]
if len(h) == 3:
h = "".join(ch * 2 for ch in h)
print(f"rgb({int(h[0:2], 16)}, {int(h[2:4], 16)}, {int(h[4:6], 16)})")