topologicpy.Color module

class topologicpy.Color.Color

Bases: object

Methods

AddHex(*colors[, silent])

Adds the input hexadecimal color codes channel-wise, clipping each channel to a max of 255.

AnyToHex(color[, silent, colorModel])

Converts a color to a hexadecimal color string.

Average(*colors[, silent])

Averages the input list of colors after conversion to HEX.

ByCSSNamedColor(color[, alpha, silent])

Creates a Color from a CSS named color string.

ByHEX(hex[, alpha, silent])

Converts a hexadecimal color string to RGB color values.

ByValueInRange([value, minValue, maxValue, ...])

Returns the r, g, b, and optionally a, values corresponding to a value in a range.

CMYKToHex(cmyk[, silent])

Converts a CMYK color to its hexadecimal representation.

CSSNamedColor(color[, silent])

Returns the CSS named color that most closely matches the input RGB color.

CSSNamedColors()

Returns a list of all CSS named colors.

ColorScale([colorScale, silent])

Return a Plotly-compatible scale list from a name or custom list.

PlotlyColor(color[, alpha, useAlpha, silent])

Returns a Plotly color string based on an RGB or RGBA list.

RGBToHex(rgb[, silent])

Converts RGB color values to a hexadecimal color string.

static AddHex(*colors, silent: bool = False)

Adds the input hexadecimal color codes channel-wise, clipping each channel to a max of 255.

Parameters
colors*list or str

The input list of hexadecimal colors.

silentbool , optional

If set to True, error and warning messages are suppressed. Default is False.

Returns
str

The resulting hex color after addition (e.g. ‘#FF88FF’).

static AnyToHex(color, silent: bool = False, colorModel: str = 'auto')

Converts a color to a hexadecimal color string.

Parameters
colorlist, tuple, or str

The input color. Supported formats include:

  • RGB lists/tuples: [255, 0, 0], [1.0, 0.0, 0.0]

  • RGBA lists/tuples: [255, 0, 0, 0.5], [1.0, 0.0, 0.0, 0.5]

  • CMYK lists/tuples: [0.0, 1.0, 1.0, 0.0]

  • HEX strings: “#RGB”, “#RGBA”, “#RRGGBB”, “#RRGGBBAA”

  • CSS rgb()/rgba(): “rgb(255,0,0)”, “rgba(0,0,0,0.5)”

  • CSS hsl()/hsla(): “hsl(0,100%,50%)”, “hsla(0,100%,50%,0.5)”

  • CSS named colors: “red”, “transparent”

  • A list of color strings, in which case their average color is returned.

Alpha values are validated but ignored because the return format is ‘#RRGGBB’.

silentbool , optional

If set to True, error and warning messages are suppressed. Default is False.

colorModelstr , optional

The color model used for numeric list or tuple inputs. Valid values are “auto”, “rgb”, “rgba”, and “cmyk”. Default is “auto”.

In “auto” mode, three-channel numeric inputs are interpreted as RGB. Four-channel numeric inputs are interpreted as RGBA only when at least one RGB channel is greater than 1 and the fourth channel is between 0 and 1. A four-channel input whose values are all between 0 and 1 is ambiguous and requires an explicit “rgba” or “cmyk” colorModel.

Returns
str

A hexadecimal color string in the format ‘#RRGGBB’, or None if the input cannot be interpreted unambiguously.

static Average(*colors, silent: bool = False)

Averages the input list of colors after conversion to HEX.

Parameters
colors*list or str

The input color parameter which can be any of RGB, CMYK, CSS named color, or HEX.

silentbool , optional

If set to True, error and warning messages are suppressed. Default is False.

Returns
str

A hexadecimal color string in the format ‘#RRGGBB’.

static ByCSSNamedColor(color, alpha: float = None, silent: bool = False)

Creates a Color from a CSS named color string. See https://developer.mozilla.org/en-US/docs/Web/CSS/named-color

Parameters
colorstr

A CSS named color.

alphafloat , optional

The desired alpha (transparency value). Default is None which means no alpha value will be included in the returned list.

silentbool , optional

If set to True, error and warning messages are suppressed. Default is False.

Returns
list

The color expressed as an [r, g, b] or an [r, g, b, a] list.

static ByHEX(hex: str, alpha: float = None, silent: bool = False)

Converts a hexadecimal color string to RGB color values.

Parameters
hexstr

A hexadecimal color string in the format ‘#RRGGBB’.

alphafloat , optional

The transparency value. 0.0 means the color is fully transparent, 1.0 means the color is fully opaque. Default is None.

silentbool , optional

If set to True, error and warning messages are suppressed. Default is False.

Returns
list

The color expressed as an [r, g, b] or an [r, g, b, a] list.

static ByValueInRange(value: float = 0.5, minValue: float = 0.0, maxValue: float = 1.0, alpha: float = None, colorScale='viridis', silent: bool = False)

Returns the r, g, b, and optionally a, values corresponding to a value in a range.

Parameters
valuefloat , optional

The input value. Default is 0.5.

minValuefloat , optional

The input minimum value. Default is 0.0.

maxValuefloat , optional

The input maximum value. Default is 1.0.

alphafloat , optional

The alpha value. Default is None.

colorScalestr , optional

A Plotly color scale name or list, ‘syntax’ (Depthmap Classic), ‘syntax_r’ (reversed), or ‘default’. Default is ‘viridis’.

silentbool , optional

If set to True, error and warning messages are suppressed. Default is False.

Returns
list

The color expressed as an [r, g, b] or an [r, g, b, a] list.

static CMYKToHex(cmyk, silent: bool = False)

Converts a CMYK color to its hexadecimal representation.

Parameters
cmyklist or tuple

CMYK values as [C, M, Y, K], each in the range 0 to 1.

silentbool , optional

If set to True, error and warning messages are suppressed. Default is False.

Returns
str

The hexadecimal color string for Plotly (e.g., ‘#FFFFFF’).

static CSSNamedColor(color, silent: bool = False)

Returns the CSS named color that most closely matches the input RGB color.

Parameters
colorlist

The input color in the format [r, g, b].

silentbool , optional

If set to True, error and warning messages are suppressed. Default is False.

Returns
str

The CSS named color that most closely matches the input color.

static CSSNamedColors()

Returns a list of all CSS named colors. See https://developer.mozilla.org/en-US/docs/Web/CSS/named-color

Returns
list

The list of all CSS named colors.

static ColorScale(colorScale='viridis', silent: bool = False)

Return a Plotly-compatible scale list from a name or custom list.

syntax reproduces Depthmap Classic with blue=0 and red=1, including its unequal ranges and discrete HTML-safe RGB channels. syntax_r reverses it. Names are case-insensitive. Other inputs follow Plotly’s colorscale validation. Returned lists are independent.

static PlotlyColor(color, alpha=1.0, useAlpha=False, silent: bool = False)

Returns a Plotly color string based on an RGB or RGBA list.

Parameters
colorlist

The input color list in the format [r, g, b] or [r, g, b, a].

alphafloat , optional

The transparency value. Default is 1.0.

useAlphabool , optional

If set to True, the returned string uses rgba(…). Default is False.

silentbool , optional

If set to True, error and warning messages are suppressed. Default is False.

Returns
str

The Plotly color string.

static RGBToHex(rgb, silent: bool = False)

Converts RGB color values to a hexadecimal color string.

Parameters
rgblist or tuple

Three integer-like RGB values in the range 0 to 255.

silentbool , optional

If set to True, error and warning messages are suppressed. Default is False.

Returns
str

A hexadecimal color string in the format ‘#RRGGBB’.