Skip to content

cmap #

Scientific colormaps for python, without dependencies.

Modules:

  • data

    Where all the data is stored. (No public API).

Classes:

  • Catalog

    Runtime representation of catalog of available colormaps.

  • CatalogItem

    A loaded catalog item.

  • Color

    Class to represent a single color.

  • ColorStop

    A color stop in a color gradient.

  • ColorStops

    A sequence of color stops in a color gradient.

  • Colormap

    A colormap is a mapping from scalar values to RGB(A) colors.

  • HSLA

    Hue, Saturation, Lightness named tuple.

  • HSVA

    Hue, Saturation, Value, Alpha named tuple.

  • RGBA

    RGBA named tuple, all values are floats between 0 and 1.

  • RGBA8

    8 bit RGBA named tuple, where RGB values are from 0 to 255, alpha from 0 to 1.

Functions:

  • to_mpl

    Convienence function to create a matplotlib colormap.

Attributes:

ColorLike module-attribute #

ColorLike: TypeAlias = Union[
    None,
    str,
    RGBTuple,
    RGBATuple,
    np.ndarray,
    list[Union[float, int]],
    "Color",
]

Data types that can be cast to a cmap.Color instance.

RGBTuple: 3-tuple of all ints or all floats, representing an RGB color

RGBATuple: 4-tuple of all floats, or 3 ints and 1 float, representing an RGBA color

ColormapLike module-attribute #

ColormapLike: TypeAlias = Union[
    str,
    Iterable[Union[ColorLike, ColorStopLike]],
    "NDArray",
    "MPLSegmentData",
    dict[float, ColorLike],
    "ColorStops",
    LutCallable,
]

Data types that can be passed to the cmap.Colormap constructor.

Catalog #

Catalog(
    data_root: Path = DATA_ROOT,
    record_pattern: str = RECORD_PATTERN,
)

Runtime representation of catalog of available colormaps.

Parameters:

  • data_root #

    (Path, default: DATA_ROOT ) –

    Path to the root of the data directory, by default uses the cmap.data folder.

  • record_pattern #

    (str, default: RECORD_PATTERN ) –

    Glob pattern to use to find record files, by default "record.json".

Methods:

  • namespaced_keys

    Return a set of available short colormap names, with namespace.

  • resolve

    Return the fully qualified, normalized name of a colormap or alias.

  • short_keys

    Return a set of available short colormap names, without namespace.

  • unique_keys

    Return names that refer to unique colormap data.

namespaced_keys #

namespaced_keys() -> set[str]

Return a set of available short colormap names, with namespace.

resolve #

resolve(name: str) -> str

Return the fully qualified, normalized name of a colormap or alias.

short_keys #

short_keys() -> set[str]

Return a set of available short colormap names, without namespace.

unique_keys #

unique_keys(
    prefer_short_names: bool = True,
    normalized_names: bool = False,
    categories: Container[Category] = (),
    interpolation: Interpolation | None = None,
) -> set[str]

Return names that refer to unique colormap data.

Parameters:

  • prefer_short_names #

    (bool, default: True ) –

    If True (default), short names (without the namespace prefix) will be preferred over fully qualified names. In cases where the same short name is used in multiple namespaces, they will all be referred to by their fully qualified (namespaced) name.

  • normalized_names #

    (bool, default: False ) –

    If True, return the normalized names of the colormaps. If False (default), return the original names of the colormaps (which may include spaces and/or capital letters).

  • categories #

    (Container[Category], default: () ) –

    If provided, only return colormaps in the given categories.

  • interpolation #

    (Interpolation, default: None ) –

    If provided, only return colormaps with the given interpolation method.

Returns:

  • set[str]

    A set of unique colormap names that can be used to access the colormap data.

CatalogItem dataclass #

CatalogItem(
    data: ColormapLike,
    name: str,
    category: Category,
    license: str = "UNKNOWN",
    source: str = "",
    info: str = "",
    namespace: str = "",
    authors: list[str] = list(),
    interpolation: bool
    | Interpolation = "linear",
    tags: list[str] = list(),
    aliases: list[str] = list(),
    under: str | None = None,
    over: str | None = None,
    bad: str | None = None,
)

A loaded catalog item.

Attributes:

  • data (ColormapLike) –

    Any object that can be passed to Colormap to create a colormap. See ColormapLike objects for more information.

  • name (str) –

    The (short) name of the colormap, e.g. "viridis".

  • category (str) –

    The category of the colormap. One of {"sequential", "diverging", "cyclic", "qualitative", "miscellaneous"}.

  • license (str) –

    The license of the colormap.

  • source (str) –

    The source of the colormap (usually a URL).

  • info (str) –

    A description of the colormap. Will be displayed on the colormap page in the documentation.

  • namespace (str) –

    The namespace of the colormap. This is a cmap-specific namespace for organizing colormaps into collections. (e.g. "matplotlib", "cmocean", "colorcet", etc.)

  • authors (list[str]) –

    A list of authors of the colormap.

  • interpolation (bool | Interpolation) –

    The interpolation method to use when sampling the colormap. One of {False, True, "linear", "nearest"}, where False is equivalent to "nearest" and True is equivalent to "linear". If not provided, defaults to "linear".

  • tags (list[str]) –

    A list of tags for the colormap. These are displayed in the documentation.

  • aliases (list[str]) –

    A list of aliases for the colormap. These are alternative names that can be used to access the colormap. Currently, they must be accessed using the fully qualified name (namespace:alias).

  • qualified_name (str) –

    The fully qualified name of the colormap, e.g. "matplotlib:viridis".

  • under (str | None) –

    The color to use for under-range values.

  • over (str | None) –

    The color to use for over-range values.

  • bad (str | None) –

    The color to use for bad values.

Color #

Color(value: ColorLike)

Class to represent a single color.

Instances of this class are immutable and cached (based on the rgba tuple), you can compare them with is.

Parameters:

  • value #

    (str, tuple, list, array, Color, or int) –

    The color to represent. Can be any "ColorLike".

Methods:

  • from_int

    Parse color from bit-shifted integer encoding.

  • to_int

    Convert color to bit-shifted integer encoding.

Attributes:

  • alpha (float) –

    Return the alpha value.

  • hex (str) –

    Return the color as hex.

  • hsl (HSLA) –

    Return the color as Hue, Saturation, Lightness.

  • hsv (HSVA) –

    Return the color as Hue, Saturation, Value.

  • name (str | None) –

    Return the color as name.

  • rgba (RGBA) –

    Return the color as (Red, Green, Blue, Alpha) tuple in 0-1 range.

  • rgba8 (RGBA8) –

    Return the color as (Red, Green, Blue, Alpha) tuple in 0-255 range.

  • rgba_string (str) –

    Return the color as an 'rgba(r, g, b, a)' string; 0-255 range.

alpha property #

alpha: float

Return the alpha value.

hex property #

hex: str

Return the color as hex.

hsl property #

hsl: HSLA

Return the color as Hue, Saturation, Lightness.

hsv property #

hsv: HSVA

Return the color as Hue, Saturation, Value.

name property #

name: str | None

Return the color as name.

rgba property #

rgba: RGBA

Return the color as (Red, Green, Blue, Alpha) tuple in 0-1 range.

rgba8 property #

rgba8: RGBA8

Return the color as (Red, Green, Blue, Alpha) tuple in 0-255 range.

rgba_string property #

rgba_string: str

Return the color as an 'rgba(r, g, b, a)' string; 0-255 range.

from_int classmethod #

from_int(
    value: int,
    format: str,
    bits_per_component: int | Sequence[int] = 8,
) -> Color

Parse color from bit-shifted integer encoding.

Parameters:

  • value #

    (int) –

    The integer value to parse.

  • format #

    (str) –

    The format of the integer value. Must be a string composed only of the characters 'r', 'g', 'b', and 'a'.

  • bits_per_component #

    (int | Sequence[int] | None, default: 8 ) –

    The number of bits used to represent each color component. If a single integer is provided, it is used for all components. If a sequence of integers is provided, the length must match the length of format.

to_int #

to_int(
    format: str,
    bits_per_component: int | Sequence[int] = 8,
) -> int

Convert color to bit-shifted integer encoding.

Parameters:

  • format #

    (str) –

    The format of the integer value. Must be a string composed only of the characters 'r', 'g', 'b', and 'a'.

  • bits_per_component #

    (int | Sequence[int] | None, default: 8 ) –

    The number of bits used to represent each color component. If a single integer is provided, it is used for all components. If a sequence of integers is provided, the length must match the length of format.

ColorStop #

A color stop in a color gradient.

Just a named tuple with a position (float) and a color (cmap.Color).

Attributes:

  • position (float) –

    The position of the color stop in the gradient, between 0 and 1.

  • color (cmap.Color) –

    The color at the position in the gradient. This is a cmap.Color object. The color can be any object that can be cast to a Color, including a string, or 3/4-sequence of RGB(A) values.

ColorStops #

ColorStops(
    stops: np.ndarray
    | Iterable[tuple[float, Color]]
    | None = None,
    *,
    lut_func: LutCallable | None = None,
    interpolation: Interpolation
    | bool = "linear",
)

A sequence of color stops in a color gradient.

Convenience class allowing various operations on a sequence of color stops, including casting to an (N, 5) array (e.g. np.asarray(ColorStops(...)))

This is the main internal representation of a colormap, and is used to construct the LUT used in the Colormap.call method.

Parameters:

  • stops #

    (array - like, default: None ) –

    An array of color stops, by default None (must provide either stops or lut_func) The array must be an (N, 5) array, where the first column is the position (0-1) and the remaining columns are the color (RGBA, 0-1).

  • lut_func #

    (callable, default: None ) –

    A callable that takes a single argument (an (N, 1) array of positions) and returns an (N, 4) array of colors. This will be used to generate the LUT instead of the stops array. If provided, the stops argument will be ignored.

  • interpolation #

    (str, default: 'linear' ) –

    Interpolation mode. Must be one of 'linear' (or True) or 'nearest' (or False). Defaults to 'linear'.

Methods:

  • parse

    Parse any colorstops-like object into a ColorStops object.

  • reversed

    Return a new ColorStops object with reversed colors.

  • shifted

    Return a new ColorStops object with all positions shifted by shift.

  • to_css

    Return a CSS representation of the color stops.

  • to_lut

    Create (N, 4) LUT of RGBA values from 0-1, interpolated between color stops.

Attributes:

color_array property #

color_array: np.ndarray

Return an (N, 4) array of RGBA values.

colors property #

colors: tuple[Color, ...]

Return all colors as Color objects.

stops property #

stops: tuple[np.floating, ...]

Return tuple of color stop positions.

parse classmethod #

parse(colors: ColormapLike) -> ColorStops

Parse any colorstops-like object into a ColorStops object.

Each item in colors can be a color, or a 2-tuple of (position, color), where position (the "stop" along a color gradient) is a float between 0 and 1. Where not provided, color positions will be evenly distributed between neighboring specified positions (if fill_mode is 'neighboring') or will be replaced with index / (len(colors)-1) (if fill_mode is 'fractional').

Colors can be expressed as anything that can be converted to a Color, including a string, or 3/4-sequence of RGB(A) values.

Parameters:

Returns:

reversed #

reversed() -> ColorStops

Return a new ColorStops object with reversed colors.

shifted #

shifted(
    shift: float,
    mode: Literal["wrap", "clip"] = "wrap",
) -> ColorStops

Return a new ColorStops object with all positions shifted by shift.

Parameters:

  • shift #

    (float) –

    The amount to shift the colormap. Positive values shift the colormap towards the end, negative values shift the colormap towards the beginning.

  • mode #

    (('wrap', 'clip'), default: 'wrap' ) –

    The mode to use when shifting the colormap. Must be one of 'wrap' or 'clip'. If 'wrap', the colormap will be shifted and wrapped around the ends. If 'clip', the colormap will be shifted and the colors at the ends will be clipped and/or repeated as necessary.

to_css #

to_css(
    max_stops: int | None = None,
    angle: int = 90,
    radial: bool = False,
    as_hex: bool = False,
) -> str

Return a CSS representation of the color stops.

Parameters:

  • max_stops #

    (int, default: None ) –

    May be used to limit the number of color stops in the css.

  • angle #

    (int, default: 90 ) –

    Angle of the gradient in degrees. by default 90. (ignored for radial)

  • radial #

    (bool, default: False ) –

    If True, return a radial gradient, by default False.

  • as_hex #

    (bool, default: False ) –

    If True, return colors as hex strings, by default use rgba() strings.

to_lut #

to_lut(
    N: int = 256, gamma: float = 1.0
) -> np.ndarray

Create (N, 4) LUT of RGBA values from 0-1, interpolated between color stops.

Parameters:

  • N #

    (int, default: 256 ) –

    Number of colors to return.

  • gamma #

    (float, default: 1.0 ) –

    Gamma correction to apply to the colors.

Colormap #

Colormap(
    value: ColormapLike,
    *,
    name: str | None = None,
    identifier: str | None = None,
    category: str | None = None,
    interpolation: Interpolation
    | bool
    | None = None,
    under: ColorLike | None = None,
    over: ColorLike | None = None,
    bad: ColorLike | None = None,
)

A colormap is a mapping from scalar values to RGB(A) colors.

The primary way to apply a colormap to data is to call the colormap object with scalar values, which will return an array of RGBA colors. See Colormap.__call__ for details.

Parameters:

  • value #

    (ColormapLike) –

    The color data to use for the colormap. Can be a single color, a single color stop, a sequence of colors and/or color stops, or a dictionary mapping scalar values to colors.

    Any of the following are valid:

    • a str containing a recognized string colormap name (e.g. "viridis", "magma"), optionally suffixed with "_r" to reverse the colormap (e.g. "viridis", "magma_r").
    • An iterable of ColorLike values (any object that can be cast to a Color), or "color-stop-like" tuples ( (float, ColorLike) where the first element is a scalar value specifying the position of the color in the gradient. When using color stops, the stop position values should be in the range [0, 1]. If no scalar stop positions are given, they will be linearly interpolated between any neighboring stops (or 0-1 if there are no stops).
    • a dict mapping scalar values to color-like values: e.g. {0.0: "red", 0.5: (0, 1, 0), 1.0: "#0000FF"}.
    • a matplotlib-style segmentdata dict, with keys "red", "green", and "blue", each of which maps to a list of tuples of the form (x, y0, y1), or a callable that takes an array of values in the range [0, 1] and returns an array of values in the range [0, 1]. See the matplotlib docs for more.
    • a Callable that takes an array of N values in the range [0, 1] and returns an (N, 4) array of RGBA values in the range [0, 1].
  • name #

    (str | None, default: None ) –

    A name for the colormap. If None, will be set to the identifier or the string "custom colormap".

  • identifier #

    (str | None, default: None ) –

    The identifier of the colormap. If None, will be set to the name, converted to lowercase and with spaces and dashes replaced by underscores.

  • category #

    (str | None, default: None ) –

    An optional category of the colormap (e.g. "diverging", "sequential"). Not used internally.

  • interpolation #

    (str | bool | None, default: None ) –

    The interpolation mode to use when mapping scalar values to colors. Can be "linear" (default) or "nearest". If True, will use "linear", if False, will use "nearest". Providing this value will override any interpolation from a catalog entry.

  • under #

    (ColorLike | None, default: None ) –

    The color to use for values below the colormap's range.

  • over #

    (ColorLike | None, default: None ) –

    The color to use for values above the colormap's range.

  • bad #

    (ColorLike | None, default: None ) –

    The color to use for bad (NaN, inf) values.

Methods:

  • __call__

    Map scalar values in X to an RGBA array.

  • as_dict

    Return a dictionary representation of the colormap.

  • catalog

    Return the global colormaps catalog.

  • iter_colors

    Return a list of N color objects sampled evenly over the range of the LUT.

  • lut

    Return a lookup table (LUT) for the colormap.

  • reversed

    Return a new Colormap, with reversed colors.

  • shifted

    Return a new Colormap, with colors shifted by a scalar value.

  • to_altair

    Return an altair colorscale with N color samples from the colormap.

  • to_bokeh

    Return a bokeh colorscale, with N color samples from the colormap.

  • to_css

    Return a CSS representation of the colormap as a linear or radial gradient.

  • to_gee

    Return a Google Earth Engine palette with N color samples from the colormap.

  • to_matplotlib

    Return a matplotlib colormap.

  • to_napari

    Return a napari colormap.

  • to_plotly

    Return a plotly colorscale.

  • to_pygfx

    Return a pygfx TextureMap.

  • to_pyqtgraph

    Return a pyqtgraph.ColorMap.

  • to_viscm

    Plot colormap using viscm. (Requires viscm to be installed.).

  • to_vispy

    Return a vispy colormap.

  • with_extremes

    Return a copy of the colormap with new extreme values.

Attributes:

  • bad_color (Color | None) –

    A color to use for missing/bad values when converting scalars to colors.

  • category (str | None) –

    An optional category for the colormap.

  • color_stops (ColorStops) –

    The color stops in the colormap.

  • identifier (str) –

    An identifier for the colormaps.

  • interpolation (Interpolation) –

    The interpolation mode to use when mapping scalar values to colors.

  • name (str) –

    A display name for the colormap.

  • num_colors (int) –

    The number of colors in this colormap.

  • over_color (Color | None) –

    A color to use for values above 1 when converting scalars to colors.

  • under_color (Color | None) –

    A color to use for values below 0 when converting scalars to colors.

bad_color instance-attribute #

bad_color: Color | None = (
    None if bad is None else Color(bad)
)

A color to use for missing/bad values when converting scalars to colors.

If provided, and Colormap.lut is called with with_over_under=True, bad_color will be the last color in the LUT (lut[-1]).

category instance-attribute #

category: str | None = category

An optional category for the colormap.

If not provided, and if the colormap is a catalog colormap, this will be set to the category of the colormap.

color_stops instance-attribute #

color_stops: ColorStops = stops

The color stops in the colormap.

This object is a sequence of color stops: a color (RGBA) and a scalar position (0-1) in the gradient. This is the primary data structure used to represent the colormap. See ColorStops docstring for more information.

identifier instance-attribute #

identifier: str = _make_identifier(
    identifier or name
)

An identifier for the colormaps.

If not provided, it will be generated from name by discarding any characters that are not alphanumeric, spaces, dashes, or underscores, converting to lowercase, replacing spaces and dashes with underscores, and prefacing with "_" if the first character is a digit.

interpolation instance-attribute #

interpolation: Interpolation = _norm_interp(
    interpolation
)

The interpolation mode to use when mapping scalar values to colors.

Either "linear" or "nearest", where "linear" is the default.

name instance-attribute #

name: str = name

A display name for the colormap.

If not provided explicitly, and if the colormap is a catalog colormap, this will be set to "namespace:name". If not a catalog colormap, it will be set to the identifier, or fallback to "custom colormap".

num_colors property #

num_colors: int

The number of colors in this colormap.

over_color instance-attribute #

over_color: Color | None = (
    None if over is None else Color(over)
)

A color to use for values above 1 when converting scalars to colors.

If provided, and Colormap.lut is called with with_over_under=True, over_color will be the second-to-last color in the LUT (lut[-2]).

under_color instance-attribute #

under_color: Color | None = (
    None if under is None else Color(under)
)

A color to use for values below 0 when converting scalars to colors.

If provided, and Colormap.lut is called with with_over_under=True, under_color will be the third-to-last color in the LUT (lut[-3]).

__call__ #

__call__(
    x: NDArray | Iterable[float],
    *,
    N: int = 256,
    gamma: float = 1,
    bytes: bool = False,
) -> NDArray[np.float64]
__call__(
    x: float,
    *,
    N: int = 256,
    gamma: float = 1,
    bytes: bool = False,
) -> Color
__call__(
    x: float | NDArray | Iterable[float],
    *,
    N: int = 256,
    gamma: float = 1,
    bytes: bool = False,
) -> Color | NDArray[np.float64]

Map scalar values in X to an RGBA array.

This is the primary API for "using" a cmap.Colormap to map scalar values to colors.

The dtype of x matters. If x is an integer dtype, then it is interpreted as (fancy) indexing directly into the LUT. If x is a float, then it is assumed to be a normalized value in [0, 1] and will be mapped linearly to the nearest color in the LUT (use a higher N for finer sampling).

Parameters:

  • x #

    (float | array - like) –

    The scalar values to map to colors. If x is a float, a single color object will be returned. If x is an array-like, an array of RGBA colors will be returned with shape x.shape + (4,). See note above about the dtype of x.

  • N #

    (int, default: 256 ) –

    Number of samples in the LUT. This is used to determine the resolution of the mapping (by default, 256). Note that depending on the data being mapped, N can cause slight rounding errors in some cases. N of 256 is the default in matplotlib, so it is here as well, but note that N=255 (odd numbered) will result in an exact color match being returned for a value of 0.5 in a colormap with an odd number of colors.

  • gamma #

    (float, default: 1 ) –

    The gamma value to use when creating the LUT.

  • bytes #

    (bool, default: False ) –

    If False (default), the returned RGBA values will be floats in the interval [0, 1] otherwise they will be numpy.uint8\s in the interval [0, 255].

Returns:

  • color ( Color | NDArray ) –

    If X is a float, a single RGBA color will be returned. If x is an array-like, an array of RGBA colors will be returned with shape x.shape + (4,)

Examples:

>>> from cmap import Colormap
>>> from tifffile import imread
>>> cmap = Colormap("viridis")
>>> data = imread('some_path.tif')
>>> data = data - data.min()  # normalize to 0-1
>>> data = data / data.max()  # normalize to 0-1
>>> colored_img = cmap(data)

as_dict #

as_dict() -> ColormapDict

Return a dictionary representation of the colormap.

The returned dictionary is suitable for serialization, or for passing to the Colormap constructor.

catalog classmethod #

catalog() -> Catalog

Return the global colormaps catalog.

iter_colors #

iter_colors(
    N: Iterable[float] | int | None = None,
) -> Iterator[Color]

Return a list of N color objects sampled evenly over the range of the LUT.

If N is an integer, it will return a list of N colors spanning the full range of the colormap. If N is an iterable, it will return a list of colors at the positions specified by the iterable.

Parameters:

  • N #

    (int | Iterable[float] | None, default: None ) –

    The number of colors to return, or an iterable of positions to sample. If not provided (the default), N will be set to the number of colors in the colormap.

Yields:

  • color ( Color ) –

    Color objects.

lut #

lut(
    N: int = 256,
    gamma: float = 1,
    *,
    with_over_under: bool = False,
) -> np.ndarray

Return a lookup table (LUT) for the colormap.

The returned LUT is a numpy array of RGBA values, with shape (N, 4), where N is the number of requested colors in the LUT. If with_over_under is True the returned shape will be (N + 3, 4), where index N is the under color, index N + 1 is the over color, and index N + 2 is the bad (NaN) color.

The LUT can be used to map scalar values (that have been normalized to 0-1) to colors, using fancy indexing or np.take.

The output of this function is used by the __call__ method, but may also be used directly by users.

LUTs of a particular size and gamma value are cached.

Parameters:

  • N #

    (int, default: 256 ) –

    The number of colors in the LUT.

  • gamma #

    (float, default: 1 ) –

    The gamma value to use for the LUT.

  • with_over_under #

    (bool, default: False ) –

    If True, the LUT will include the under, over, and bad colors as the last three colors in the LUT. If False, the LUT will only include the colors defined by the color_stops.

reversed #

reversed(name: str | None = None) -> Colormap

Return a new Colormap, with reversed colors.

Parameters:

  • name #

    (str | None, default: None ) –

    By default, the name of the new colormap will be the name of the original colormap with "_r" appended. If the original colormap name ends in "_r", the new colormap name will be the original name with "_r" removed. If the name argument is provided, it will be used as the name of the new colormap.

shifted #

shifted(
    shift: float = 0.5,
    name: str | None = None,
    mode: Literal["wrap", "clip"] = "wrap",
) -> Colormap

Return a new Colormap, with colors shifted by a scalar value.

This method shifts the stops in the colormap by a scalar value. It makes most sense for cyclic colormaps, but can be used with any colormap.

Parameters:

  • shift #

    (float, default: 0.5 ) –

    The amount to shift the colormap. Positive values shift the colormap towards the end, negative values shift the colormap towards the beginning.

  • name #

    (str, default: None ) –

    A new name for the colormap. If not provided, the name of the new colormap will be the name of the original colormap with "_shifted{shift}" appended.

  • mode #

    (('wrap', 'clip'), default: 'wrap' ) –

    The mode to use when shifting the colormap. Must be one of 'wrap' or 'clip'. If 'wrap', the colormap will be shifted and wrapped around the ends. If 'clip', the colormap will be shifted and the colors at the ends will be clipped and/or repeated as necessary.

Returns:

  • Colormap

    A new colormap with the colors shifted.

to_altair #

to_altair(N: int = 256) -> list[str]

Return an altair colorscale with N color samples from the colormap.

Suitable for passing to the range parameter of altair.Scale. https://altair-viz.github.io/user_guide/customization.html#color-domain-and-range

to_bokeh #

to_bokeh(
    N: int = 256,
) -> bokeh.models.LinearColorMapper

Return a bokeh colorscale, with N color samples from the colormap.

https://docs.bokeh.org/en/latest/docs/reference/models/mappers.html

Parameters:

  • N #

    (int, default: 256 ) –

    Number of colors to sample from the colormap, by default 256.

to_css #

to_css(
    max_stops: int | None = None,
    angle: int = 90,
    radial: bool = False,
    as_hex: bool = False,
) -> str

Return a CSS representation of the colormap as a linear or radial gradient.

Parameters:

  • max_stops #

    (int, default: None ) –

    May be used to limit the number of color stops in the css.

  • angle #

    (int, default: 90 ) –

    Angle of the gradient in degrees. by default 90. (ignored for radial)

  • radial #

    (bool, default: False ) –

    If True, return a radial gradient, by default False.

  • as_hex #

    (bool, default: False ) –

    If True, return colors as hex strings, by default use rgba() strings.

Examples:

>>> from cmap import Colormap
>>> print(Colormap("brg").to_css())
background: rgb(0, 0, 255);
background: linear-gradient(
    90deg, rgb(0, 0, 255) 0%, rgb(255, 0, 0) 50%, rgb(0, 255, 0) 100%
);

to_gee #

to_gee(N: int = 256) -> list[str]

Return a Google Earth Engine palette with N color samples from the colormap.

Suitable for passing to the palette parameter of Google Earth Engine visualizations. See: https://developers.google.com/earth-engine/guides/image_visualization

to_matplotlib #

to_matplotlib(
    N: int = 256, gamma: float = 1.0
) -> matplotlib.colors.Colormap

Return a matplotlib colormap.

to_napari #

to_napari() -> napari.utils.colormaps.Colormap

Return a napari colormap.

https://napari.org/stable/api/napari.utils.Colormap.html

to_plotly #

to_plotly() -> list[list[float | str]]

Return a plotly colorscale.

to_pygfx #

to_pygfx(
    N: int = 256, *, as_view: bool | None = None
) -> pygfx.TextureMap

Return a pygfx TextureMap.

Note that it is recommended to use at least 256 colors for the LUT. https://github.com/pygfx/pygfx/pull/1025#issuecomment-2706170445

to_pyqtgraph #

to_pyqtgraph() -> pyqtgraph.ColorMap

Return a pyqtgraph.ColorMap.

to_viscm #

to_viscm(
    dpi: int = 100, dest: str | None = None
) -> matplotlib.figure.Figure

Plot colormap using viscm. (Requires viscm to be installed.).

See https://github.com/matplotlib/viscm for details

Parameters:

  • dpi #

    (int, default: 100 ) –

    dpi for saved image. Defaults to 100.

  • dest #

    (str, default: None ) –

    If provided, the image will be saved to this path. Defaults to None.

Returns:

  • fig ( matplotlib.figure.Figure ) –

    The figure object containing the plot.

to_vispy #

to_vispy() -> vispy.color.Colormap

Return a vispy colormap.

with_extremes #

with_extremes(
    *,
    bad: ColorLike | None = None,
    under: ColorLike | None = None,
    over: ColorLike | None = None,
) -> Colormap

Return a copy of the colormap with new extreme values.

HSLA #

Hue, Saturation, Lightness named tuple.

All values are floats between 0 and 1.

Attributes:

  • h (float) –

    Hue

  • s (float) –

    Saturation

  • l (float) –

    Lightness

  • a (float) –

    Alpha value (default is 1)

Methods:

in_degrees #

in_degrees() -> tuple[float, float, float, float]

Convert to degrees.

to_rgba #

to_rgba() -> RGBA

Convert to RGB.

HSVA #

Hue, Saturation, Value, Alpha named tuple.

All values are floats between 0 and 1.

Attributes:

  • h (float) –

    Hue

  • s (float) –

    Saturation

  • v (float) –

    Value

  • a (float) –

    Alpha value (default is 1)

Methods:

to_rgba #

to_rgba() -> RGBA

Convert to RGB.

RGBA #

RGBA named tuple, all values are floats between 0 and 1.

Attributes:

  • r (float) –

    Red

  • g (float) –

    Green

  • b (float) –

    Blue

  • a ((float, optional)) –

    Alpha value (default is 1)

Methods:

  • to_8bit

    Convert to 8-bit integer form.

  • to_hex

    Convert to hex color.

  • to_hsl

    Convert to Hue, Saturation, Lightness.

  • to_hsv

    Convert to Hue, Saturation, Value.

to_8bit #

to_8bit() -> RGBA8

Convert to 8-bit integer form.

to_hex #

to_hex() -> str

Convert to hex color.

to_hsl #

to_hsl() -> HSLA

Convert to Hue, Saturation, Lightness.

to_hsv #

to_hsv() -> HSVA

Convert to Hue, Saturation, Value.

RGBA8 #

8 bit RGBA named tuple, where RGB values are from 0 to 255, alpha from 0 to 1.

Attributes:

  • r (int) –

    Red

  • g (int) –

    Green

  • b (int) –

    Blue

  • a ((float, optional)) –

    Alpha value (default is 1)

Methods:

  • rgba_string

    Return a string representation of the color.

  • to_float

    Convert to float.

  • to_hex

    Convert to hex color.

  • to_hsl

    Convert to Hue, Saturation, Lightness.

  • to_hsv

    Convert to Hue, Saturation, Value.

rgba_string #

rgba_string() -> str

Return a string representation of the color.

to_float #

to_float() -> RGBA

Convert to float.

to_hex #

to_hex() -> str

Convert to hex color.

to_hsl #

to_hsl() -> HSLA

Convert to Hue, Saturation, Lightness.

to_hsv #

to_hsv() -> HSVA

Convert to Hue, Saturation, Value.

to_mpl #

to_mpl(
    value: ColorStopsLike,
    /,
    *,
    name: str | None = None,
    identifier: str | None = None,
    category: str | None = None,
    interpolation: Interpolation
    | bool
    | None = None,
    under: ColorLike | None = None,
    over: ColorLike | None = None,
    bad: ColorLike | None = None,
    N: int = 256,
    gamma: float = 1.0,
) -> matplotlib.colors.Colormap

Convienence function to create a matplotlib colormap.

Arguments are the same as for cmap.Colormap. This simply creates the colormap instance and returns the value of to_matplotlib().