def rgb_tuple_to_hex_str(rgb, a=None):
    """
    Return a hex string representation of the supplied RGB tuple
    (as used in SVG etc.) with alpha, rescaling all values from [0,1]
    into [0,255] (ie hex x00 to xff).

    Parameters:
       rgb - (r,g,b) tuple (each of r,g,b is in [0,1])
       a - (default None) alpha channel (transparancy) value or None

    Return value:
       string 'rrggbb' where rr, gg, bb are hex representations of r,g,b resp.
       or 'rrggbbaa' if a!=None where aa is hex repr. of alpha value a.
    """
    xrgb = tuple([int(round(x*255)) for x in rgb])
    if a == None:
        return '%02x%02x%02x' % xrgb
    else:
        a = int(round(a*255))
        return '%02x%02x%02x%02x' % (xrgb[0],xrgb[1],xrgb[2],a)