DEV Community

Discussion on: Daily Challenge #236 - RGB to Hex Conversion

Collapse
 
vidit1999 profile image
Vidit Sarkar

Here is a short Python solution,

def rgb(r, g, b):
    return ('{:02x}'*3).format(*map(lambda x : max(0, min(255, x)), (r, g, b))).upper()

Output,

print(rgb(255, 255, 300)) # output -> FFFFFF
print(rgb(0,0,0)) # output -> 000000
print(rgb(148, 0, 211)) # output -> 9400D3
print(rgb(-20,275,125)) # output -> 00FF7D
print(rgb(255, 255, 255)) # output -> FFFFFF