_HEXDEC = {v: int(v, 16) for v in (x + y for x in _NUMERALS for y in _NUMERALS)
    }
LOWERCASE, UPPERCASE = 'x', 'X'

def rgb(triplet):
    return _HEXDEC[triplet[0:2]], _HEXDEC[triplet[2:4]], _HEXDEC[triplet[4:6]]

def triplet(rgb, lettercase=LOWERCASE):
    return format(rgb[0] << 16 | rgb[1] << 8 | rgb[2], '06' + lettercase)
if __name__ == '__main__':
    print('{}, {}'.format(rgb('aabbcc'), rgb('AABBCC')))
print('{}, {}'.format(triplet((170, 187, 204)), triplet((170, 187, 204),
    UPPERCASE)))
print('{}, {}'.format(rgb('aa0200'), rgb('AA0200')))
print('{}, {}'.format(triplet((170, 2, 0)), triplet((170, 2, 0), UPPERCASE)))
How do I convert a hex triplet to an RGB tuple and back?