def hex_to_rgb(value):
value = value.lstrip('#')
lv = len(value)
return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))
def rgb_to_hex(rgb):
return '#%02x%02x%02x' % rgb
# Resultados
print hex_to_rgb("#123456") # (18, 52, 86)
print hex_to_rgb("#ffffff") # (255, 255, 255)
print hex_to_rgb("#ffffffffffff") # (65535, 65535, 65535)
print rgb_to_hex((18, 52, 86)) # #123456
print rgb_to_hex((255, 255, 255)) # #ffffff
print rgb_to_hex((65535, 65535, 65535)) # #ffffffffffff
ZGVmIGhleF90b19yZ2IodmFsdWUpOgogICAgdmFsdWUgPSB2YWx1ZS5sc3RyaXAoJyMnKQogICAgbHYgPSBsZW4odmFsdWUpCiAgICByZXR1cm4gdHVwbGUoaW50KHZhbHVlW2k6aSArIGx2IC8vIDNdLCAxNikgZm9yIGkgaW4gcmFuZ2UoMCwgbHYsIGx2IC8vIDMpKQoKZGVmIHJnYl90b19oZXgocmdiKToKICAgIHJldHVybiAnIyUwMnglMDJ4JTAyeCcgJSByZ2IKICAgIAkJCQkJCQkJCQkjIFJlc3VsdGFkb3MKcHJpbnQgaGV4X3RvX3JnYigiIzEyMzQ1NiIpCQkJCQkjICgxOCwgNTIsIDg2KQpwcmludCBoZXhfdG9fcmdiKCIjZmZmZmZmIikJCQkJCSMgKDI1NSwgMjU1LCAyNTUpCnByaW50IGhleF90b19yZ2IoIiNmZmZmZmZmZmZmZmYiKQkJCSMgKDY1NTM1LCA2NTUzNSwgNjU1MzUpCnByaW50IHJnYl90b19oZXgoKDE4LCA1MiwgODYpKQkJCQkjICMxMjM0NTYKcHJpbnQgcmdiX3RvX2hleCgoMjU1LCAyNTUsIDI1NSkpCQkJIyAjZmZmZmZmCnByaW50IHJnYl90b19oZXgoKDY1NTM1LCA2NTUzNSwgNjU1MzUpKQkJIyAjZmZmZmZmZmZmZmZmCg==