Module colors

Functions for converting colors from one representation to another.

Overview

color_names

Return an iterator over all HTML/CSS color names.

floats2rgb

Convert a RGB-triple with floats to one with ints.

hex2rgb

Convert from hexadecimal to RGB.

is_valid_name

Return whether name is a valid color name.

name2hex

Return the hexadecimal string for name.

name2rgb

Return the RGB values for name.

rgb2floats

Convert a RGB-triple with ints to one with floats.

rgb2hex

Convert from RGB to hexadecimal.

salmagundi.colors.color_names()[source]

Return an iterator over all HTML/CSS color names.

salmagundi.colors.floats2rgb(rgb)[source]

Convert a RGB-triple with floats to one with ints.

Useful when using functions that take or return RGB values as floats from 0 to 1, e.g. the functions in module colorsys.

Parameters

rgb (tuple(float, float, float)) – RGB-triple with values ∈ [0, 1]

Returns

RGB-triple with values ∈ [0..255]

Return type

tuple(int, int, int)

New in version 0.2.0.

salmagundi.colors.hex2rgb(hexstr)[source]

Convert from hexadecimal to RGB.

Parameters

hexstr (str) – string with three or six hex digits, optionally prefixed with #

Returns

RGB-triple

Return type

tuple(int, int, int)

Raises

ValueError – if one of the arguments is not in [0..255]

salmagundi.colors.is_valid_name(name)[source]

Return whether name is a valid color name.

Parameters

name (str) – color name

Returns

True if the name is valid

Return type

bool

salmagundi.colors.name2hex(name)[source]

Return the hexadecimal string for name.

Parameters

name (str) – color name

Returns

hexadecimal string (# + 6 hex digits)

Return type

str

Raises

KeyError – if name is not valid

salmagundi.colors.name2rgb(name)[source]

Return the RGB values for name.

Parameters

name (str) – color name

Returns

RGB-triple

Return type

tuple(int, int, int)

Raises

KeyError – if name is not valid

salmagundi.colors.rgb2floats(rgb)[source]

Convert a RGB-triple with ints to one with floats.

Useful when using functions that take or return RGB values as floats from 0 to 1, e.g. the functions in module colorsys.

Parameters

rgb (tuple(int, int, int)) – RGB-triple with values ∈ [0..255]

Returns

RGB-triple with values ∈ [0, 1]

Return type

tuple(float, float, float)

New in version 0.2.0.

salmagundi.colors.rgb2hex(r, g, b)[source]

Convert from RGB to hexadecimal.

Parameters
  • r (int) – red value ∈ [0..255]

  • g (int) – green value ∈ [0..255]

  • b (int) – blue value ∈ [0..255]

Returns

hexadecimal string (# + 6 hex digits)

Return type

str

Raises

ValueError – if one of the arguments is not in [0..255]