Hexadecimal To RGB

This algorithm converts hexadecimal color code to RGB color model.



									#*****Please import following Modules*****
# math
#*****************************************

class RGB:
    def __init__(self, r, g, b):
        self.R = bytes([int(r)])
        self.G = bytes([int(g)])
        self.B = bytes([int(b)])

def HexadecimalToRGB(hex):
    if hex.startswith("#"):
        hex = hex[1:]
    r = HexadecimalToDecimal(hex[0:2])
    g = HexadecimalToDecimal(hex[2:4])
    b = HexadecimalToDecimal(hex[4:6])
    return RGB(r, g, b)

def HexadecimalToDecimal(hex):
    hex = hex.upper()
    hexLength = len(hex)
    dec = 0
    i = 0
    while i < hexLength:
        b = ord(hex[i])
        if b >= 48 and b <= 57:
            b -= 48
        elif b >= 65 and b <= 70:
            b -= 55
        dec += b * math.pow(16, ((hexLength - i) - 1))
        i += 1
    return dec
								


Example

									data = "#520057"
value = HexadecimalToRGB(data)
								


Output

									R: [82]
G: [0]
B: [87]