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)