Skip to content

cellects.display.param

cellects.display.param

This script contains color, font, and size parameters for displaying images, videos, and plots.

generate_color_gradient(colors, n_steps)

Generate a linear color gradient interpolating between a sequence of RGB colors.

Parameters:

Name Type Description Default
colors list

List of RGB tuples defining the colors to interpolate through.

required
n_steps int

Total number of color tuples to generate to fill the gradient between the provided colors.

required

Returns:

Type Description
list

A list of RGB tuples forming a gradient between the provided colors.

Examples:

>>> colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]
>>> gradient = generate_color_gradient(colors, 5)
>>> print(gradient)
[(255.0, 0.0, 0.0), (127.5, 127.5, 0.0), (0.0, 255.0, 0.0),
 (0.0, 127.5, 127.5), (0.0, 0.0, 255.0)]
Source code in src/cellects/display/param.py
def generate_color_gradient(colors: list, n_steps: int) -> list:
    """
    Generate a linear color gradient interpolating between a sequence of RGB colors.

    Parameters
    ----------
    colors : list
        List of RGB tuples defining the colors to interpolate through.
    n_steps : int
        Total number of color tuples to generate to fill the gradient between the provided colors.

    Returns
    -------
    list
        A list of RGB tuples forming a gradient between the provided colors.

    Examples
    --------
    >>> colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]
    >>> gradient = generate_color_gradient(colors, 5)
    >>> print(gradient)
    [(255.0, 0.0, 0.0), (127.5, 127.5, 0.0), (0.0, 255.0, 0.0),
     (0.0, 127.5, 127.5), (0.0, 0.0, 255.0)]
    """
    gradient = []
    n_segments = len(colors) - 1
    steps_per_segment = n_steps // n_segments

    for i in range(n_segments):
        start_color = colors[i]
        end_color = colors[i + 1]
        for step in range(steps_per_segment):
            t = step / steps_per_segment
            interpolated_color = tuple(
                start_color[j] + t * (end_color[j] - start_color[j])
                for j in range(3)
            )
            gradient.append(interpolated_color)

    gradient.append(colors[-1])
    return gradient[:n_steps]

get_mpl_colormap(cmap_name)

Returns a linear color range array for the given matplotlib colormap.

Parameters:

Name Type Description Default
cmap_name str

The name of the colormap to get.

required

Returns:

Type Description
ndarray

A 256x1x3 array of bytes representing the linear color range.

Examples:

>>> result = get_mpl_colormap('viridis')
>>> print(result.shape)
(256, 1, 3)
Source code in src/cellects/display/param.py
def get_mpl_colormap(cmap_name: str):
    """
    Returns a linear color range array for the given matplotlib colormap.

    Parameters
    ----------
    cmap_name : str
        The name of the colormap to get.

    Returns
    -------
    numpy.ndarray
        A 256x1x3 array of bytes representing the linear color range.

    Examples
    --------
    >>> result = get_mpl_colormap('viridis')
    >>> print(result.shape)
    (256, 1, 3)

    """
    cmap = plt.get_cmap(cmap_name)

    # Initialize the matplotlib color map
    sm = plt.cm.ScalarMappable(cmap=cmap)

    # Obtain linear color range
    color_range = sm.to_rgba(np.linspace(0, 1, 256), bytes=True)[:, 2::-1]

    return color_range.reshape(256, 1, 3)

random_pastel_colors(color_nb, hexadecimal=False)

Generate random pastel colors.

This function generates a specified number of random pastel colors by manipulating the HSV color space to ensure a soft, pastel-like appearance. The generated colors can be returned either in hexadecimal format or as RGB tuples.

Parameters:

Name Type Description Default
color_nb int

Number of pastel colors to generate.

required
hexadecimal bool

If True, the colors are returned in hexadecimal format. If False, the colors are returned as RGB tuples with each value ranging from 0 to 255.

False

Returns:

Type Description
List[Union[tuple, str]]

A list of generated pastel colors. Each color is represented as an RGB tuple or hexadecimal string, depending on the value of hexadecimal.

Raises:

Type Description
IndexError

If the colony_colors list is accessed using an incorrect index.

Examples:

>>> colors = random_pastel_colors(3, hexadecimal=True)
>>> print(colors)
['#d9d8ff', '#ffdabd', '#ffe5d3']
>>> colors = random_pastel_colors(3, hexadecimal=False)
>>> print(colors)
[(221, 187, 255), (255, 218, 187), (255, 227, 211)]
Source code in src/cellects/display/param.py
def random_pastel_colors(color_nb, hexadecimal: bool=False):
    """
    Generate random pastel colors.

    This function generates a specified number of random pastel colors by manipulating the HSV color
    space to ensure a soft, pastel-like appearance. The generated colors can be returned either in
    hexadecimal format or as RGB tuples.

    Parameters
    ----------
    color_nb : int
        Number of pastel colors to generate.
    hexadecimal : bool, default=False
        If `True`, the colors are returned in hexadecimal format.
        If `False`, the colors are returned as RGB tuples with each value ranging from 0 to 255.

    Returns
    -------
    List[Union[tuple, str]]
        A list of generated pastel colors. Each color is represented as an RGB tuple or hexadecimal
        string, depending on the value of `hexadecimal`.

    Raises
    ------
    IndexError
        If the `colony_colors` list is accessed using an incorrect index.

    Examples
    --------
    >>> colors = random_pastel_colors(3, hexadecimal=True)
    >>> print(colors)
    ['#d9d8ff', '#ffdabd', '#ffe5d3']

    >>> colors = random_pastel_colors(3, hexadecimal=False)
    >>> print(colors)
    [(221, 187, 255), (255, 218, 187), (255, 227, 211)]
    """
    colony_colors = []
    for i in range(color_nb):
        color_code = np.array(colorsys.hsv_to_rgb((0.6 + i / color_nb) % 1, 0.3, 0.85))[::-1]
        if hexadecimal:
            colony_colors.append(to_hex(color_code))
        else:
            colony_colors.append(tuple((color_code * 255).astype(int)))
    return colony_colors