_css_rgb_or_hex_to_hex

spac.visualization._css_rgb_or_hex_to_hex(col, keep_alpha=False)[source]

Normalise a CSS-style color string to a hexadecimal value or a valid Matplotlib color name.

Parameters:
  • col (str) –

    Accepted formats: * ‘#abc’, ‘#aabbcc’, ‘#rrggbbaa’ * ‘rgb(r,g,b)’ or ‘rgba(r,g,b,a)’, where r, g, b are 0-255 and

    a is 0-1 or 0-255

    • any named Matplotlib color

  • keep_alpha (bool, optional) – If True and the input includes alpha, return an 8-digit hex; otherwise drop the alpha channel. Default is False.

Returns:

  • Lower-case colour name or

  • 6- or 8-digit lower-case hex.

Return type:

str

Raises:

ValueError – If the color cannot be interpreted.

Examples

>>> _css_rgb_or_hex_to_hex('gold')
'gold'
>>> _css_rgb_or_hex_to_hex('rgb(255,0,0)')
'#ff0000'
>>> _css_rgb_or_hex_to_hex('rgba(255,0,0,0.5)', keep_alpha=True)
'#ff000080'