darsia.ColorPath#

class ColorPath(colors=None, base_color=None, relative_colors=None, mode='rgb', name='ColorPath')[source]#

Bases: object

Piecewise linear color path in RGB space.

This class allows to define a color path through a list of absolute colors in RGB space. In addition, it supports parametrization of given images in terms of the color path. Both absolute and relative colors can be used to define the path.

If using the “rgb” mode, there are no technical restrictions or assumptions on the validity of the colors. The class could be used for parametrization of any vector-valued image of any dimension. The main purpose though is to provide a convenient way to define a color path in RGB space, which can be used for visualization purposes. For this, a color map can be created which can be used in matplotlib or other libraries.

Color path.

Parameters:
colorslist[np.ndarray] | None

Absolute colors in RGB space, defining the color path. Defaults to a simple grayscale path from black to white.

base_colornp.ndarray | None

Base color in RGB space, used as the first color in the path.

relative_colorslist[np.ndarray] | None

Relative colors in RGB space wrt. the base color, defining the color path. If provided, colors must be None.

modeLiteral[‘rgb’, ‘lab’, ‘hcl’]

Color space to use for interpolation in between colors. Defaults to “rgb”.

namestr

Name of the color path.

Methods

fit(colors, color_mode[, mode])

Parametrize colors in terms of the color path.

from_dict(data)

Create a ColorPath instance from a dictionary representation.

get_color_map([n_colors, name])

Create a colormap from the color path, ready for matplotlib.

interpret(parameters, color_mode[, mode])

Interpret parameters in terms of the color path.

load(path)

Load the color path from a file.

refine(num_segments[, distance_to_left, ...])

Redefine the color path with a given number of segments.

sample_absolute_color_path([n_colors])

Sample the absolute color path through interpolation.

save(path)

Save the color path to a file.

show_cmap()

Visualize the color path as a colormap.

show_path([name, directory, delay])

Visualize the color path in RGB space.

to_dict()

Convert the color path to a dictionary representation.

Attributes

colors

Absolute colors in RGB space.

relative_colors

Relative colors in RGB space.

relative_distances

Relative distances between the colors in the path.

equidistant_distances

Equidistant distance between 0 and 1 for each color in the path.

num_segments

Number of segments in the color path.

mode

Color space to use for interpolation in between colors.

name

Name of the color path.

classmethod from_dict(data)[source]#

Create a ColorPath instance from a dictionary representation.

Parameters:
datadict

Dictionary representation of the color path.

classmethod load(path)[source]#

Load the color path from a file.

Parameters:
pathPath

The path to the file from which the color path should be loaded.

fit(colors, color_mode, mode='relative')[source]#

Parametrize colors in terms of the color path.

Apply brute-force minimization to find the closest color representation on the path for each pixel in the image.

Parameters:
colorsnp.ndarray

Colors to be interpreted.

Returns:
np.ndarray

Parametrization of the input image in terms of the color path.

get_color_map(n_colors=256, name='custom_cmap')[source]#

Create a colormap from the color path, ready for matplotlib.

Parameters:
n_colorsint

Number of quantization levels in the colormap.

namestr

Name of the colormap.

Returns:
LinearSegmentedColormap

Colormap created from the color path.

interpret(parameters, color_mode, mode='relative')[source]#

Interpret parameters in terms of the color path.

Parameters:
parametersnp.ndarray

Parameters to be interpreted.

color_modedarsia.ColorMode

Color mode to use for interpretation.

modeLiteral[‘equidistant’, ‘relative’]

Mode to use for interpretation.

Returns:
np.ndarray

Interpreted colors.

refine(num_segments, distance_to_left=None, distance_to_right=None, mode='relative')[source]#

Redefine the color path with a given number of segments.

Parameters:
num_segmentsint

Number of segments for the refined color path.

distance_to_leftfloat | None

Value to extend the color path to the left (inter).

distance_to_rightfloat | None

Value to extend the color path to the right.

Returns:
ColorPath

Refined color path with the specified number of segments.

sample_absolute_color_path(n_colors=256)[source]#

Sample the absolute color path through interpolation.

The interpolation mode depends on the mode parameter of the constructor.

Parameters:
n_colorsint

Number of quantization levels in the colormap.

Returns:
list[np.ndarray]

Sampled absolute color path.

save(path)[source]#

Save the color path to a file.

Parameters:
pathPath

The path to the file where the color path should be saved.

show_cmap()[source]#

Visualize the color path as a colormap.

show_path(name='', directory=None, delay=False, **kwargs)[source]#

Visualize the color path in RGB space.

to_dict()[source]#

Convert the color path to a dictionary representation.

Returns:
dict

Dictionary representation of the color path.

colors#

Absolute colors in RGB space.

equidistant_distances#

Equidistant distance between 0 and 1 for each color in the path.

mode#

Color space to use for interpolation in between colors.

name#

Name of the color path.

num_segments#

Number of segments in the color path.

relative_colors#

Relative colors in RGB space.

relative_distances#

Relative distances between the colors in the path.