color.py

Sub-module providing color functions.

References,

rgb_to_xyz(red: int, green: int, blue: int) Tuple[float, float, float][source]

Convert standard RGB color to XYZ color.

D65/2° standard illuminant.

Parameters:
  • red (int) – RGB value of Red.

  • green (int) – RGB value of Green.

  • blue (int) – RGB value of Blue.

Returns:

Tuple (X, Y, Z) representing XYZ color

Return type:

tuple

xyz_to_lab(x_val: float, y_val: float, z_val: float) Tuple[float, float, float][source]

Convert XYZ color to CIE-Lab color.

Parameters:
  • x_val (float) – XYZ value of X.

  • y_val (float) – XYZ value of Y.

  • z_val (float) – XYZ value of Z.

Returns:

Tuple (L, a, b) representing CIE-Lab color

Return type:

tuple D65/2° standard illuminant

rgb_to_lab(red: int, green: int, blue: int) Tuple[float, float, float][source]

Convert RGB color to CIE-Lab color.

Parameters:
  • red (int) – RGB value of Red.

  • green (int) – RGB value of Green.

  • blue (int) – RGB value of Blue.

Returns:

Tuple (L, a, b) representing CIE-Lab color

Return type:

tuple D65/2° standard illuminant

dist_rgb(rgb1: Tuple[int, int, int], rgb2: Tuple[int, int, int]) float[source]

Determine distance between two rgb colors.

Parameters:
  • rgb1 (tuple) – RGB color definition

  • rgb2 (tuple) – RGB color definition

Returns:

Square of the distance between provided colors

Return type:

float

This works by treating RGB colors as coordinates in three dimensional space and finding the closest point within the configured color range using the formula:

d^2 = (r2 - r1)^2 + (g2 - g1)^2 + (b2 - b1)^2

For efficiency, the square of the distance is returned which is sufficient for comparisons

dist_rgb_weighted(rgb1: Tuple[int, int, int], rgb2: Tuple[int, int, int]) float[source]

Determine the weighted distance between two rgb colors.

Parameters:
  • rgb1 (tuple) – RGB color definition

  • rgb2 (tuple) – RGB color definition

Returns:

Square of the distance between provided colors

Return type:

float Similar to a standard distance formula, the values are weighted to approximate human perception of color differences For efficiency, the square of the distance is returned which is sufficient for comparisons

dist_cie76(rgb1: Tuple[int, int, int], rgb2: Tuple[int, int, int]) float[source]

Determine distance between two rgb colors using the CIE76 algorithm.

Parameters:
  • rgb1 (tuple) – RGB color definition

  • rgb2 (tuple) – RGB color definition

Returns:

Square of the distance between provided colors

Return type:

float For efficiency, the square of the distance is returned which is sufficient for comparisons

dist_cie94(rgb1: Tuple[int, int, int], rgb2: Tuple[int, int, int]) float[source]

Determine distance between two rgb colors using the CIE94 algorithm.

Parameters:
  • rgb1 (tuple) – RGB color definition

  • rgb2 (tuple) – RGB color definition

Returns:

Square of the distance between provided colors

Return type:

float For efficiency, the square of the distance is returned which is sufficient for comparisons

dist_cie2000(rgb1: Tuple[int, int, int], rgb2: Tuple[int, int, int]) float[source]

Determine distance between two rgb colors using the CIE2000 algorithm.

Parameters:
  • rgb1 (tuple) – RGB color definition

  • rgb2 (tuple) – RGB color definition

Returns:

Square of the distance between provided colors

Return type:

float For efficiency, the square of the distance is returned which is sufficient for comparisons

xterm256color_from_rgb(red: int, green: int, blue: int) Tuple[int, Tuple[int, int, int]][source]

Convert RGB values to xterm 256-color cube index and RGB approximation.

Uses the 6x6x6 color cube (indices 16-231) with levels [0,95,135,175,215,255].

Parameters:
  • red (int) – RGB value of Red (0-255).

  • green (int) – RGB value of Green (0-255).

  • blue (int) – RGB value of Blue (0-255).

Returns:

Tuple (cube_index, (r, g, b)) representing the xterm cube index and RGB approximation

Return type:

tuple

xterm256gray_from_rgb(red: int, green: int, blue: int) Tuple[int, Tuple[int, int, int]][source]

Convert RGB values to xterm 256-color grayscale index and RGB approximation.

Uses the 24 grayscale entries (indices 232-255) with values 8+10*i.

Parameters:
  • red (int) – RGB value of Red (0-255).

  • green (int) – RGB value of Green (0-255).

  • blue (int) – RGB value of Blue (0-255).

Returns:

Tuple (gray_index, (r, g, b)) representing the xterm gray index and RGB approximation

Return type:

tuple