(color, palette=None)
return tuple(clamp(val, 0.0, 1.0) for val in components)
Converts a color given in one of the supported color formats to
    R-G-B-A values.

    Examples:

      >>> color_name_to_rgba("red")
      (1.0, 0.0, 0.0, 1.0)
      >>> color_name_to_rgba("#ff8000") == (1.0, 128/255.0, 0.0, 1.0)
      True
      >>> color_name_to_rgba("#ff800080") == (1.0, 128/255.0, 0.0, 128/255.0)
      True
      >>> color_name_to_rgba("#08f") == (0.0, 136/255.0, 1.0, 1.0)
      True
      >>> color_name_to_rgba("rgb(100%, 50%, 0%)")
      (1.0, 0.5, 0.0, 1.0)
      >>> color_name_to_rgba("rgba(100%, 50%, 0%, 25%)")
      (1.0, 0.5, 0.0, 0.25)
      >>> color_name_to_rgba("hsla(120, 100%, 50%, 0.5)")
      (0.0, 1.0, 0.0, 0.5)
      >>> color_name_to_rgba("hsl(60, 100%, 50%)")
      (1.0, 1.0, 0.0, 1.0)
      >>> color_name_to_rgba("hsv(60, 100%, 100%)")
      (1.0, 1.0, 0.0, 1.0)

    @param color: the color to be converted in one of the following formats:
      - B{CSS3 color specification}: C{#rrggbb}, C{#rgb}, C{#rrggbbaa}, C{#rgba},
        C{rgb(red, green, blue)}, C{rgba(red, green, blue, alpha)},
        C{hsl(hue, saturation, lightness)}, C{hsla(hue, saturation, lightness, alpha)},
        C{hsv(hue, saturation, value)} and C{hsva(hue, saturation, value, alpha)}
        where the components are given as hexadecimal numbers in the first four
        cases and as decimals or percentages (0%-100%) in the remaining cases.
        Red, green and blue components are between 0 and 255; hue is between 0
        and 360; saturation, lightness and value is between 0 and 100; alpha is
        between 0 and 1.
      - B{Valid HTML color names}, i.e. those that are present in the HTML 4.0
        specification
      - B{Valid X11 color names}, see U{http://en.wikipedia.org/wiki/X11_color_names}
      - B{Red-green-blue components} given separately in either a comma-, slash- or
        whitespace-separated string or a list or a tuple, in the range of 0-255.
        An alpha value of 255 (maximal opacity) will be assumed.
      - B{Red-green-blue-alpha components} given separately in either a comma-, slash-
        or whitespace-separated string or a list or a tuple, in the range of 0-255
      - B{A single palette index} given either as a string or a number. Uses
        the palette given in the C{palette} parameter of the method call.
    @param palette: the palette to be used if a single number is passed to
      the method. Must be an instance of L{colors.Palette}.

    @return: the RGBA values corresponding to the given color in a 4-tuple.
      Since these colors are primarily used by Cairo routines, the tuples
      contain floats in the range 0.0-1.0
Converts a color given in one of the supported color formats to
    R-G-B-A values.
Converts a color given in one of the supported color formats to R - G - B - A values .
def color_name_to_rgba(color, palette=None):
    """Converts a color given in one of the supported color formats to
    R-G-B-A values.

    Examples:

      >>> color_name_to_rgba("red")
      (1.0, 0.0, 0.0, 1.0)
      >>> color_name_to_rgba("#ff8000") == (1.0, 128/255.0, 0.0, 1.0)
      True
      >>> color_name_to_rgba("#ff800080") == (1.0, 128/255.0, 0.0, 128/255.0)
      True
      >>> color_name_to_rgba("#08f") == (0.0, 136/255.0, 1.0, 1.0)
      True
      >>> color_name_to_rgba("rgb(100%, 50%, 0%)")
      (1.0, 0.5, 0.0, 1.0)
      >>> color_name_to_rgba("rgba(100%, 50%, 0%, 25%)")
      (1.0, 0.5, 0.0, 0.25)
      >>> color_name_to_rgba("hsla(120, 100%, 50%, 0.5)")
      (0.0, 1.0, 0.0, 0.5)
      >>> color_name_to_rgba("hsl(60, 100%, 50%)")
      (1.0, 1.0, 0.0, 1.0)
      >>> color_name_to_rgba("hsv(60, 100%, 100%)")
      (1.0, 1.0, 0.0, 1.0)

    @param color: the color to be converted in one of the following formats:
      - B{CSS3 color specification}: C{#rrggbb}, C{#rgb}, C{#rrggbbaa}, C{#rgba},
        C{rgb(red, green, blue)}, C{rgba(red, green, blue, alpha)},
        C{hsl(hue, saturation, lightness)}, C{hsla(hue, saturation, lightness, alpha)},
        C{hsv(hue, saturation, value)} and C{hsva(hue, saturation, value, alpha)}
        where the components are given as hexadecimal numbers in the first four
        cases and as decimals or percentages (0%-100%) in the remaining cases.
        Red, green and blue components are between 0 and 255; hue is between 0
        and 360; saturation, lightness and value is between 0 and 100; alpha is
        between 0 and 1.
      - B{Valid HTML color names}, i.e. those that are present in the HTML 4.0
        specification
      - B{Valid X11 color names}, see U{http://en.wikipedia.org/wiki/X11_color_names}
      - B{Red-green-blue components} given separately in either a comma-, slash- or
        whitespace-separated string or a list or a tuple, in the range of 0-255.
        An alpha value of 255 (maximal opacity) will be assumed.
      - B{Red-green-blue-alpha components} given separately in either a comma-, slash-
        or whitespace-separated string or a list or a tuple, in the range of 0-255
      - B{A single palette index} given either as a string or a number. Uses
        the palette given in the C{palette} parameter of the method call.
    @param palette: the palette to be used if a single number is passed to
      the method. Must be an instance of L{colors.Palette}.

    @return: the RGBA values corresponding to the given color in a 4-tuple.
      Since these colors are primarily used by Cairo routines, the tuples
      contain floats in the range 0.0-1.0
    """
    if not isinstance(color, str):
        if hasattr(color, "__iter__"):
            components = list(color)
        else:
            # A single index is given as a number
            try:
                components = palette.get(color)
            except AttributeError:
                raise ValueError("palette index used when no palette was given")
        if len(components) < 4:
            components += [1.0] * (4 - len(components))
    else:
        if color[0] == "#":
            color = color[1:]
            if len(color) == 3:
                components = [int(i, 16) * 17.0 / 255.0 for i in color]
                components.append(1.0)
            elif len(color) == 4:
                components = [int(i, 16) * 17.0 / 255.0 for i in color]
            elif len(color) == 6:
                components = [int(color[i : i + 2], 16) / 255.0 for i in (0, 2, 4)]
                components.append(1.0)
            elif len(color) == 8:
                components = [int(color[i : i + 2], 16) / 255.0 for i in (0, 2, 4, 6)]
        elif color.lower() in known_colors:
            components = known_colors[color.lower()]
        else:
            color_mode = "rgba"
            maximums = (255.0, 255.0, 255.0, 1.0)
            for mode in ["rgb(", "rgba(", "hsv(", "hsva(", "hsl(", "hsla("]:
                if color.startswith(mode) and color[-1] == ")":
                    color = color[len(mode) : -1]
                    color_mode = mode[:-1]
                    if mode[0] == "h":
                        maximums = (360.0, 100.0, 100.0, 1.0)
                    break

            if " " in color or "/" in color or "," in color:
                color = color.replace(",", " ").replace("/", " ")
                components = color.split()
                for idx, comp in enumerate(components):
                    if comp[-1] == "%":
                        components[idx] = float(comp[:-1]) / 100.0
                    else:
                        components[idx] = float(comp) / maximums[idx]
                if len(components) < 4:
                    components += [1.0] * (4 - len(components))
                if color_mode[:3] == "hsv":
                    components = hsva_to_rgba(*components)
                elif color_mode[:3] == "hsl":
                    components = hsla_to_rgba(*components)
            else:
                components = palette.get(int(color))

    # At this point, the components are floats
    return tuple(clamp(val, 0.0, 1.0) for val in components)
def color_name_to_rgba ( color , palette = None ) : if not isinstance ( color , str ) : if hasattr ( color , "__iter__" ) : components = list ( color ) else : # A single index is given as a number try : components = palette . get ( color ) except AttributeError : raise ValueError ( "palette index used when no palette was given" ) if len ( components ) < 4 : components += [ 1.0 ] * ( 4 - len ( components ) ) else : if color [ 0 ] == "#" : color = color [ 1 : ] if len ( color ) == 3 : components = [ int ( i , 16 ) * 17.0 / 255.0 for i in color ] components . append ( 1.0 ) elif len ( color ) == 4 : components = [ int ( i , 16 ) * 17.0 / 255.0 for i in color ] elif len ( color ) == 6 : components = [ int ( color [ i : i + 2 ] , 16 ) / 255.0 for i in ( 0 , 2 , 4 ) ] components . append ( 1.0 ) elif len ( color ) == 8 : components = [ int ( color [ i : i + 2 ] , 16 ) / 255.0 for i in ( 0 , 2 , 4 , 6 ) ] elif color . lower ( ) in known_colors : components = known_colors [ color . lower ( ) ] else : color_mode = "rgba" maximums = ( 255.0 , 255.0 , 255.0 , 1.0 ) for mode in [ "rgb(" , "rgba(" , "hsv(" , "hsva(" , "hsl(" , "hsla(" ] : if color . startswith ( mode ) and color [ - 1 ] == ")" : color = color [ len ( mode ) : - 1 ] color_mode = mode [ : - 1 ] if mode [ 0 ] == "h" : maximums = ( 360.0 , 100.0 , 100.0 , 1.0 ) break if " " in color or "/" in color or "," in color : color = color . replace ( "," , " " ) . replace ( "/" , " " ) components = color . split ( ) for idx , comp in enumerate ( components ) : if comp [ - 1 ] == "%" : components [ idx ] = float ( comp [ : - 1 ] ) / 100.0 else : components [ idx ] = float ( comp ) / maximums [ idx ] if len ( components ) < 4 : components += [ 1.0 ] * ( 4 - len ( components ) ) if color_mode [ : 3 ] == "hsv" : components = hsva_to_rgba ( * components ) elif color_mode [ : 3 ] == "hsl" : components = hsla_to_rgba ( * components ) else : components = palette . get ( int ( color ) ) # At this point, the components are floats return tuple ( clamp ( val , 0.0 , 1.0 ) for val in components )