colors

colorschemes

Colorschemes defined by different points of it used to interpolate the continious colorscheme.

rnets.colors.colorschemes.Colorscheme

Type synonym to define a colorscheme.

Type:

type

rnets.colors.colorschemes.VIRIDIS

Viridis colorscheme.

Type:

Colorscheme

rnets.colors.colorschemes.INFERNO

Inferno colorscheme.

Type:

Colorscheme

rnets.colors.colorschemes.MAGMA

Magma colorscheme.

Type:

Colorscheme

rnets.colors.colorschemes.PLASMA

Plasma colorscheme.

Type:

Colorscheme

rnets.colors.colorschemes.CIVIDIS

Cividis colorscheme.

Type:

Colorscheme

rnets.colors.colorschemes.VIRIDIS_R

Viridis colorscheme reversed.

Type:

Colorscheme

rnets.colors.colorschemes.INFERNO_R

Inferno colorscheme reversed.

Type:

Colorscheme

rnets.colors.colorschemes.MAGMA_R

Magma colorscheme reversed.

Type:

Colorscheme

rnets.colors.colorschemes.PLASMA_R

Plasma colorscheme reversed.

Type:

Colorscheme

rnets.colors.colorschemes.CIVIDIS_R

Cividis colorscheme reversed.

Type:

Colorscheme

palettes

Color palettes. In the form of dict of Color

css (dict of str as keys and obj

Color as values): CSS4 colorscheme. More information in https://drafts.csswg.org/css-color/.

utils

Utils for Color parsing, writing and interpolation.

rnets.colors.utils.Color

Type synonym to define a color.

Type:

type

Note

This library only works with 3-D colorspaces. Its extension N-D colorspaces should be straightforward, however, it has been decided not to include it in benefit of a strict definition of Color.

rnets.colors.utils.adjust_range(x: float) float[source]

Given an float, adjust it to the [0., 1.] interval, making underflow values 0 and overflow values 1.

Parameters:

x (float) -- Value to adjust.

Returns:

Adjusted float value.

rnets.colors.utils.apply_gamma(x: float, g: float | None = None, A: float = 1.) float[source]

Apply gamma to color component.

Parameters:
  • x (float) -- Component to which the Gamma will be applied.

  • g (float, optional) -- Gamma value to apply. If None, assume sRGB gamma. Defaults to None.

  • A (float, optional) -- A value for the gamma function. In most cases 1. Defaults to 1.

Returns:

Component with the new Gamma value.

Note

The input and the output value are assumed to be a float within the [0, 1] interval.

rnets.colors.utils.calc_relative_luminance(c: Color) float[source]

Calculate the relative luminance of a Color in the RGB space.

Parameters:

c (Color) -- Color in the RGB colorspace to compute its luminance.

Returns:

Computed luminance.

Return type:

float

References

https://www.w3.org/WAI/GL/wiki/Relative_luminance

rnets.colors.utils.color_sel_lum(c1: Color, c2: Color, dc: Color, threshold: float = 0.5) Color[source]

Given a pair of colors, select a color from it based on the relative luminance of a third one.

Parameters:
  • c1 (Color) -- Color to select if the luminance is below the threshold.

  • c2 (Color) -- Color to select if the luminance is above the threshold.

  • dc (Color) -- Color to use to compute the relative luminance.

  • threshold (float, optional) -- Threshold to make the decision. Ideally, a value between 0. and 1. Defaults to 0.5.

Returns:

c1 or c2.

Return type:

Color

rnets.colors.utils.cyclic_conditions(x: float, y: float) tuple[float, float][source]

Assuming that the range [0, 1] represent a periodic one-dimensional space, find the representation of the values that minimizes the distance between them.

Parameters:
  • x (float) -- First value.

  • y (float) -- Second value.

Returns:

A tuple of the form (x, y) containing the new representation of the values.

rnets.colors.utils.ensure_color(c: Color) bool[source]

Tests wether a Color is valid or not.

Arg:

c (Color): Color to be tested.

Returns:

wether the color is valid or not.

Return type:

bool

Note

Color follows the colorsys representation, meaning that it

consists of 3 float values ranging from [0, 1].

rnets.colors.utils.hexstr_to_rgb(s: str) Color | None[source]

Parses a color string of the form #RRGGBB into a Color.

Parameters:

s (str) -- Color string to be parsed.

Returns:

With the values of the parsed string None: If the conversion fails.

Return type:

Color

Note

Note that Color follows the colorsys representation,

meaning that it consists of 3 float values ranging from [0, 1].

rnets.colors.utils.interp_c_seq(x: float, cs: Sequence[Color], cyclic: tuple[bool, bool, bool] = (False, False, False)) Color[source]

Given a value float x in the range [0., 1.] and a sequence of colors, interpolate a color being represented by x, being 0 the first color in the list and 1. the last.

Parameters:
  • x (float) -- Floating point number in the range of [0., 1.] representing the position in the color sequence.

  • cs (Sequence of Color) -- Sequence of colors to be interpolated.

  • cyclic (bool, optional) -- Index of the components of the color that are cyclic. In a cyclic component, the 0 and 1 values are connected, and thus the algorithm will consider the shortest path.

Returns:

Interpolation using the :obj:colorsys implementation (3

float values in [0. ,1.])

Return type:

Color

Note

Note that this function will perform the interpolation withing the

color space provided. i. e. if RGB is used, the interpolation will be done within the RGB colorspace. If x overflows on the roof, the last color of the sequence is returned while if x overflows on the floor, the first color of the sequence is returned.

rnets.colors.utils.interp_cs(cs: Sequence[Color], interp: ColorSpace = 'lab') Callable[[float], Color][source]

Given a sequence of Color in an arbitrary colorspace, convert them to another space and create an interpolation function. The value returned by the interpolation function will be converted again to the original colorspace.

Parameters:
  • cs (Sequence of Color) -- Sequence of colors in the RGB to be converted HSL and interpolated.

  • (Literal (interp) -- rgb, lab or hsl, optional): The colorspace in which the interpolation will be perform.

Returns:

obj:Callable[[float], Color]: A function that takes a float in

the range [0., 1.] as an argument and returns the interpolated color from the given Sequence of Color using the specified colorspaces.

Note

Wrapper for interp_fn_cspace.

rnets.colors.utils.interp_fn(cs: Sequence) Callable[[float], Color][source]

Given a sequence of Color, returns an interpolated function based on it.

Parameters:

cs (Sequence of Color) -- Sequence of colors to be interpolated.

Returns:

obj:Callable[[float], Color]: A function that takes a float in

the range [0., 1.] as an argument and returns the interpolated color from the given Sequence of Color.

Note

This function acts as a wrapper of interp.

rnets.colors.utils.interp_fn_cspace(cs: Sequence[Color], target: Callable[[float, float, float], Color] = lambda a, b, c: ..., origin: Callable[[float, float, float], Color] = lambda a, b, c: ..., cyclic: tuple[bool, bool, bool] = (False, False, False)) Callable[[float], Color][source]

Given a sequence of Color in an arbitrary colorspace, convert them to another space and create an interpolation function. The value returned by the interpolation function will be converted again to the original colorspace.

Parameters:
  • cs (Sequence of Color) -- Sequence of colors in the RGB to be converted HSL and interpolated.

  • a (origin (function of taking three floats as input and returning) -- Color, optional): Function that projects the given color components to the colorspace in which the interpolation will be performed. Defaults to the identity function.

  • a -- Color, optional): Function that returns the interpolated color to the original colorspace. Defaults to the identity function.

  • cyclic (tuple of three bools, optional) -- During the interpolation, treat the dimension marked as True as cyclic. Defaults to (False, False, False)

Returns:

obj:Callable[[float], Color]: A function that takes a float in

the range [0., 1.] as an argument and returns the interpolated color from the given Sequence of Color using the specified colorspaces.

Note

The origin and target take 3 floats as input for to ease the integration with the colorsys library.

rnets.colors.utils.interp_fn_rgb_hls(cs: Sequence[Color]) Callable[[float], Color][source]

Given a sequence of Color representing the RGB colorspace, convert them to the CIEL*ab space and create an interpolation function. The value returned by the interpolation function will be converted again to RGB.

Parameters:

cs (Sequence of Color) -- Sequence of colors in the RGB to be converted CIEL*ab and interpolated.

Returns:

obj:Callable[[float], :obj:Color]: A function that takes a float in

the range [0., 1.] as an argument and returns the CIEL*ab interpolated color from the given Sequence of RGB Color.

rnets.colors.utils.interp_fn_rgb_lab(cs: Sequence[Color]) Callable[[float], Color][source]

Given a sequence of Color representing the RGB colorspace, convert them to the HSL space and create an interpolation function. The value returned by the interpolation function will be converted again to rgb.

Parameters:

cs (Sequence of Color) -- Sequence of colors in the RGB to be converted HSL and interpolated.

Returns:

obj:Callable[[float], :obj:Color]: A function that takes a float in

the range [0., 1.] as an argument and returns the HSL interpolated color from the given Sequence of RGB Color.

Note

D65 illuminant. Gamma correction assuming sRGB. Sometimes the obtained color components are negative numbers near to 0. To assure that the colors are within the [0., 1.] interval, adjust_range() is applied to the output values.

References

CIE Colorimetry 15 (Third ed.). CIE. 2004. ISBN 3-901-906-33-9.

rnets.colors.utils.lab_to_xyz(l: float, a: float, b: float) Color[source]

Projects the given L*a*b Color value to the XYZ colorspace.

Parameters:
  • l (float) -- Floating point number in the interval [0, 1] representing the L, a, b components respectively.

  • a (float) -- Floating point number in the interval [0, 1] representing the L, a, b components respectively.

  • b (float) -- Floating point number in the interval [0, 1] representing the L, a, b components respectively.

Returns:

Projected Color.

References

Smith, Thomas; Guild, John (1931–32). "The C.I.E. colorimetric standards and their use". Transactions of the Optical Society. 33 (3): 73–134. DOI 10.1088/1475-4878/33/3/301

CIE Colorimetry 15 (Third ed.). CIE. 2004. ISBN 3-901-906-33-9.

Note

Adapted from OpenCV.

rnets.colors.utils.rgb_achromatize(c: Color) Color[source]

Achromatize the given RGB color.

Parameters:

c (Color) -- Color that will be achromatized.

Returns:

Color Achromatized color.

rnets.colors.utils.rgb_to_hexstr(c: Color, inc_hash: bool = True) str[source]

Returns the given color as an hexadecimal string of the form #RRGGBB where R, G and B are represented from a number from 0 to 9 and a letter from a (10) to f (15).

Parameters:
  • c (Color) -- Color to transform into string.

  • inc_hash (bool, optional) -- If True, include the hash character at the beggining of the color string. Defaults to True.

Returns:

Color string with the RRGGBB form.

Return type:

str

Note

Note that Color follows the colorsys representation,

meaning that it consists of 3 float values ranging from [0, 1].

rnets.colors.utils.rgb_to_xyz(r: float, g: float, b: float) Color[source]

Projects the given RGB Color value to the L*a*b* colorspace.

Parameters:
  • r (float) -- Floating point number in the interval [0, 1] representing the red, green and blue components respectively.

  • g (float) -- Floating point number in the interval [0, 1] representing the red, green and blue components respectively.

  • b (float) -- Floating point number in the interval [0, 1] representing the red, green and blue components respectively.

Returns:

Projected Color.

Note

Obeserver. = 2, Illuminant = D65

References

Smith, Thomas; Guild, John (1931–32). "The C.I.E. colorimetric standards and their use". Transactions of the Optical Society. 33 (3): 73–134. DOI 10.1088/1475-4878/33/3/301

rnets.colors.utils.unapply_gamma(x: float, g: float | None = None, A: float = 1.) float[source]

Unapply gamma to color component.

Parameters:
  • x (float) -- Component to which the Gamma will be unaapplied.

  • g (float, optional) -- Gamma value to apply. If None, assume sRGB gamma. Defaults to None.

  • A (float, optional) -- A value for the gamma function. In most cases 1. Defaults to 1.

Returns:

Ungamma component.

Note

The input and the output value are assumed to be a float within the [0, 1] interval.

rnets.colors.utils.xyz_to_lab(x: float, y: float, z: float) Color[source]

Projects the given XYZ Color value to the L*a*b* colorspace.

Parameters:
  • x (float) -- Floating point number in the interval [0, 1] representing the X, Y, Z components respectively.

  • y (float) -- Floating point number in the interval [0, 1] representing the X, Y, Z components respectively.

  • z (float) -- Floating point number in the interval [0, 1] representing the X, Y, Z components respectively.

Returns:

Projected Color.

References

Smith, Thomas; Guild, John (1931–32). "The C.I.E. colorimetric standards and their use". Transactions of the Optical Society. 33 (3): 73–134. DOI 10.1088/1475-4878/33/3/301

CIE Colorimetry 15 (Third ed.). CIE. 2004. ISBN 3-901-906-33-9.

Note

Adapted from OpenCV.

rnets.colors.utils.xyz_to_rgb(x: float, y: float, z: float) Color[source]

Projects the given xyz Color value to the rgb colorspace.

Parameters:
  • x (float) -- Floating point number in the interval [0, 1] representing the X, Y and Z components respectively.

  • y (float) -- Floating point number in the interval [0, 1] representing the X, Y and Z components respectively.

  • z (float) -- Floating point number in the interval [0, 1] representing the X, Y and Z components respectively.

Returns:

Projected Color.

Note

Obeserver. = 2, Illuminant = D65

References

Smith, Thomas; Guild, John (1931–32). "The C.I.E. colorimetric standards and their use". Transactions of the Optical Society. 33 (3): 73–134. DOI 10.1088/1475-4878/33/3/301