darsia.ColorPath#
- class ColorPath(colors=None, base_color=None, relative_colors=None, mode='rgb', name='ColorPath')[source]#
Bases:
objectPiecewise 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:
- colors
list[np.ndarray] |None Absolute colors in RGB space, defining the color path. Defaults to a simple grayscale path from black to white.
- base_color
np.ndarray|None Base color in RGB space, used as the first color in the path.
- relative_colors
list[np.ndarray] |None Relative colors in RGB space wrt. the base color, defining the color path. If provided, colors must be None.
- mode
Literal[‘rgb’, ‘lab’, ‘hcl’] Color space to use for interpolation in between colors. Defaults to “rgb”.
- name
str Name of the color path.
- colors
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.
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
Absolute colors in RGB space.
Relative colors in RGB space.
Relative distances between the colors in the path.
Equidistant distance between 0 and 1 for each color in the path.
Number of segments in the color path.
Color space to use for interpolation in between colors.
Name of the color path.
- classmethod from_dict(data)[source]#
Create a ColorPath instance from a dictionary representation.
- Parameters:
- data
dict Dictionary representation of the color path.
- data
- classmethod load(path)[source]#
Load the color path from a file.
- Parameters:
- path
Path The path to the file from which the color path should be loaded.
- path
- 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:
- colors
np.ndarray Colors to be interpreted.
- colors
- Returns:
np.ndarrayParametrization 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.
- interpret(parameters, color_mode, mode='relative')[source]#
Interpret parameters in terms of the color path.
- Parameters:
- parameters
np.ndarray Parameters to be interpreted.
- color_mode
darsia.ColorMode Color mode to use for interpretation.
- mode
Literal[‘equidistant’, ‘relative’] Mode to use for interpretation.
- parameters
- Returns:
np.ndarrayInterpreted 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:
- Returns:
ColorPathRefined 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_colors
int Number of quantization levels in the colormap.
- n_colors
- Returns:
list[np.ndarray]Sampled absolute color path.
- save(path)[source]#
Save the color path to a file.
- Parameters:
- path
Path The path to the file where the color path should be saved.
- path
- 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:
dictDictionary 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.