import re


def hex_str_to_rgb_tuple(hexstr):
    """
    Return RGBA or RGBA tuple (all values in [0,1]) correspdonding to supplied
    hex color string.

    Parameters:
        hexstr - 'rrggbb' or 'rrggbbaa' hex color string
    Return value:
        (r,g,b) or (r,g,b,a) tuple (all values in [0,1])
        correspdonding to hex color string.
    Raises exceptions:
        ValueError if invalid hexstring format.
    """
    rgbstring_re = re.compile(r'[0-9A-Fa-f]{6}')
    rgbastring_re = re.compile(r'[0-9A-Fa-f]{8}')

    if rgbstring_re.match(hexstr):     # RRGGBB
        r = int(hexstr[0:2], 16) / 255.0
        g = int(hexstr[2:4], 16) / 255.0
        b = int(hexstr[4:6], 16) / 255.0
        return (r,g,b)
    elif rgbastring_re.match(hexstr):  # RRGGBBAA
        r = int(hexstr[0:2], 16) / 255.0
        g = int(hexstr[2:4], 16) / 255.0
        b = int(hexstr[4:6], 16) / 255.0
        a = int(hexstr[6:8], 16) / 255.0
        return (r,g,b,a)
    else:
        raise(ValueError("Invalid hex color string '" + hexstr + "'"))