Skip to content

RGB To Hex Conversion

The rgb function is incomplete. Complete it so that passing in RGB decimal values will result in a hexadecimal representation being returned. Valid decimal values for RGB are 0 - 255. Any values that fall out of that range must be rounded to the closest valid value.

Note

Your answer should always be 6 characters long, the shorthand with 3 will not work here.

Example

The following are examples of expected output values:

rgb_to_hex_conversion(255, 255, 255) # returns FFFFFF
rgb_to_hex_conversion(255, 255, 300) # returns FFFFFF
rgb_to_hex_conversion(0,0,0) # returns 000000
rgb_to_hex_conversion(148, 0, 211) # returns 9400D3

Solution

py
def rgb_to_hex_conversion(r, g, b):
    return '{:02X}{:02X}{:02X}'.format(
        min(max(r, 0), 255),
        min(max(g, 0), 255),
        min(max(b, 0), 255)
    )


print(rgb_to_hex_conversion(255, 255, 256))

my thoughts are neither my employer's nor my wife's