The colorlib

The colorlib module is handling the transformation between different color spaces. A set of different color spaces is available including colorlib.CIELUV, colorlib.CIELAB, colorlib.CIEXYZ, colorlib.polarLUV/colorlib.HCL, colorlib.polarLAB, colorlib.HSV, colorlib.HLS, colorlib.RGB, colorlib.sRGB, and colorlib.hexcols for HEX colors.

../_images/img_colorlib.jpeg
class colorlib.colorlib[source]

The colorlib class is a collection of methods used to convert or transform colors between different color spaces.

DEG2RAD(x)[source]

ParameterConver degrees into radiant.

Parameters:x (float or array of floats) – values in degrees
Returns:Returns input x in radiant.
Return type:float or float array
DEVRGB_to_RGB(R, G, B, gamma = 2.4)[source]

Device dependent sRGB to device independent RGB.

Parameters:
  • R (numpy.ndarray) – indensities for red ([0.,1.])
  • G (numpy.ndarray) – indensities for green ([0.,1.])
  • B (numpy.ndarray) – indensities for blue ([0.,1.])
  • gamma (float) – gamma adjustment.
Returns:

Returns a list of numpy.ndarrays with adjusted R, G, and B values.

Return type:

list

HLAB_to_XYZ(L, A, B, XN = None, YN = None, ZN = None)[source]

Hunter LAB to CIE-XYZ.

Note

Note that the Hunter LAB is no longer part of the public API, but the code is still here in case needed.

Parameters:
  • L (numpy.ndarray) – values for the L dimension
  • A (numpy.ndarray) – values for the A dimension
  • B (numpy.ndarray) – values for the B dimension
  • YN, ZN (XN,) – chromaticity of the white point. If of length 1 the white point specification will be recycled if length of R/G/B is larger than one. If not specified (all three NA) default values will be used
Returns:

Returns corresponding CIE-XYZ chromaticies, a list of numpy.ndarray’s of the same length as the inputs ([X, Y, Z]).

Return type:

list

HLS_to_RGB(h, l, s)[source]

Convert RLS to HLS.

All r/g/b values in [0.,1.], h in [[0., 360.], l and s in [0., 1.]. From: http://wiki.beyondunreal.com/wiki/RGB_To_HLS_Conversion.

Parameters:
  • h (nympy.ndarray) – hue values
  • l (numpy.ndarray) – lightness
  • s (numpy.ndarray) – saturation
Returns:

Returns a numpy.ndarray with the corresponding coordinates in the RGB color space ([r, g, b]). Same length as the inputs.

Return type:

list

HSV_to_RGB(r, g, b)[source]

Convert RGB to HSV.

Parameters:
  • h (nympy.ndarray) – hue values
  • s (numpy.ndarray) – saturation
  • v (numpy.ndarray) – value (the value-dimension of HSV)
Returns:

Returns a numpy.ndarray with the corresponding coordinates in the RGB color space ([r, g, b]). Same length as the inputs.

Return type:

list

LAB_to_XYZ(L, A, B, XN = None, YN = None, ZN = None)[source]

CIELAB to CIEXYZ.

Parameters:
  • L (numpy.ndarray) – values for the L dimension
  • A (numpy.ndarray) – values for the A dimension
  • B (numpy.ndarray) – values for the B dimension
  • YN, ZN (XN,) – chromaticity of the white point. If of length 1 the white point specification will be recycled if length of R/G/B is larger than one. If not specified (all three NA) default values will be used
Returns:

Returns corresponding X/Y/Z coordinates of CIE chromaticies, a list of numpy.ndarray’s of the same length as the inputs ([X, Y, Z]).

Return type:

list

LAB_to_polarLAB(L, A, B)[source]

Convert from CIELAB to the polar representation polarLAB.

Parameters:
  • L (numpy.ndarray) – values for the L dimension of the CIELAB color space
  • A (numpy.ndarray) – values for the A dimension of the CIELAB color space
  • B (numpy.ndarray) – values for the B dimension of the CIELAB color space
Returns:

Returns corresponding polar LAB chromaticies, a list of numpy.ndarray’s of the same length as the inputs ([L, A, B]).

Return type:

list

LUV_to_XYZ(L, U, V, XN = None, YN = None, ZN = None)[source]

CIE-LUV to CIE-XYZ.

Parameters:
  • L (numpy.ndarray) – values for the L dimension
  • U (numpy.ndarray) – values for the U dimension
  • V (numpy.ndarray) – values for the V dimension
  • YN, ZN (XN,) – chromaticity of the white point. If of length 1 the white point specification will be recycled if length of R/G/B is larger than one. If not specified (all three NA) default values will be used
Returns:

Returns a list of CIE-XYZ coordinates ([X, Y, Z]) with the same length as the input arrays.

Return type:

list

LUV_to_polarLUV(L, U, V)[source]

LUV to polarLUV (HCL).

Parameters:
  • L (numpy.ndarray) – values for the X dimension
  • U (numpy.ndarray) – values for the Y dimension
  • V (numpy.ndarray) – values for the Z dimension
Returns:

Returns a list of polarLUV or HCL coordinates ([L, C, H]) with the same length as the input arrays. The HCL color space is simply the polar representation of the CIE-LUV color space.

Return type:

list

RAD2DEG(x)[source]

ParameterConver radiant to degrees.

Parameters:x (float or array of floats) – values in radiant
Returns:Returns input x in degrees.
Return type:float or array of floats
RGB_to_DEVRGB(R, G, B, gamma=2.4)[source]

DEVRGB_to_RGB(R, G, B, gamma = 2.4)

Device independent RGB to device dependent sRGB.

Parameters:
  • R (numpy.ndarray) – indensities for red ([0.,1.]).
  • G (numpy.ndarray) – indensities for green ([0.,1.]).
  • B (numpy.ndarray) – indensities for blue ([0.,1.]).
  • gamma (float) – gamma adjustment.
Returns:

Returns a list of numpy.ndarrays with adjusted R, G, and B values.

Return type:

list

RGB_to_HLS(r, g, b)[source]

Convert RGB to HLS.

All r/g/b values in [0.,1.], h in [[0., 360.], l and s in [0., 1.]. From: http://wiki.beyondunreal.com/wiki/RGB_To_HLS_Conversion.

Parameters:
  • r (numpy.ndarray) – intensities for red ([0.,1.])
  • g (numpy.ndarray) – intensities for green ([0.,1.])
  • b (numpy.ndarray) – intensities for blue ([0.,1.])
Returns:

Returns a numpy.ndarray with the corresponding coordinates in the HLS color space ([h, l, s]). Same length as the inputs.

Return type:

list

RGB_to_HSV(r, g, b)[source]

Convert RGB to HSV.

Parameters:
  • r (numpy.ndarray) – intensities for red ([0.,1.])
  • g (numpy.ndarray) – intensities for green ([0.,1.])
  • b (numpy.ndarray) – intensities for blue ([0.,1.])
Returns:

Returns a numpy.ndarray with the corresponding coordinates in the HSV color space ([h, s, v]). Same length as the inputs.

Return type:

list

RGB_to_XYZ(R, G, B, XN = None, YN = None, ZN = None)[source]

Device independent RGB to XYZ.

R, G, and B give the levels of red, green and blue as values in the interval [0.,1.]. X, Y and Z give the CIE chromaticies.

Parameters:
  • R (numpy.ndarray) – indensities for red ([0.,1.])
  • G (numpy.ndarray) – indensities for green ([0.,1.])
  • B (numpy.ndarray) – indensities for blue ([0.,1.])
  • YN, ZN (XN,) – chromaticity of the white point. If of length 1 the white point specification will be recycled if length of R/G/B is larger than one. If not specified (all three None) default values will be used
Returns:

Returns corresponding X/Y/Z coordinates of CIE chromaticies, a list of numpy.ndarray’s of the same length as the inputs ([X, Y, Z]).

Return type:

list

XYZ_to_HLAB(X, Y, Z, XN = None, YN = None, ZN = None)[source]

CIE-XYZ to Hunter LAB.

Note

Note that the Hunter LAB is no longer part of the public API, but the code is still here in case needed.

Parameters:
  • X (numpy.ndarray) – values for the Z dimension
  • Y (numpy.ndarray) – values for the Z dimension
  • Z (numpy.ndarray) – values for the Z dimension
  • YN, ZN (XN,) – chromaticity of the white point. If of length 1 the white point specification will be recycled if length of R/G/B is larger than one. If not specified (all three None) default values will be used
Returns:

Returns corresponding Hunter LAB chromaticies, a list of numpy.ndarray’s of the same length as the inputs ([L, A, B]).

Return type:

list

XYZ_to_LAB(X, Y, Z, XN = None, YN = None, ZN = None)[source]

CIEXYZ to CIELAB.

Parameters:
  • X (numpy.ndarray) – values for the X dimension
  • Y (numpy.ndarray) – values for the Y dimension
  • Z (numpy.ndarray) – values for the Z dimension
  • YN, ZN (XN,) – chromaticity of the white point. If of length 1 the white point specification will be recycled if length of R/G/B is larger than one. If not specified (all three NA) default values will be used
Returns:

Returns corresponding L/A/B coordinates, a list of numpy.ndarray’s of the same length as the inputs ([L, A, B]).

Return type:

list

XYZ_to_LUV(X, Y, Z, XN = None, YN = None, ZN = None)[source]

CIE-XYZ to CIE-LUV.

Parameters:
  • X (numpy.ndarray) – values for the X dimension
  • Y (numpy.ndarray) – values for the Y dimension
  • Z (numpy.ndarray) – values for the Z dimension
  • YN, ZN (XN,) – chromaticity of the white point. If of length 1 the white point specification will be recycled if length of R/G/B is larger than one. If not specified (all three NA) default values will be used.
Returns:

Returns a list of CIELUV coordinates ([L, U, V]) with the same length as the input arrays.

Return type:

list

XYZ_to_RGB(X, Y, Z, XN = None, YN = None, ZN = None)[source]

CIEXYZ to device independent RGB.

R, G, and B give the levels of red, green and blue as values in the interval [0.,1.]. X, Y and Z give the CIE chromaticies.

Parameters:
  • X (numpy.ndarray) – values for the Z dimension
  • Y (numpy.ndarray) – values for the Z dimension
  • Z (numpy.ndarray) – values for the Z dimension
  • YN, ZN (XN,) – chromaticity of the white point. If of length 1 the white point specification will be recycled if length of R/G/B is larger than one. If not specified (all three None) default values will be used
Returns:

Returns corresponding X/Y/Z coordinates of CIE chromaticies, a list of numpy.ndarray’s of the same length as the inputs ([R, G, B]).

Return type:

list

XYZ_to_sRGB(X, Y, Z, XN = None, YN = None, ZN = None)[source]

CIEXYZ to sRGB.

R, G, and B give the levels of red, green and blue as values in the interval [0.,1.]. X, Y and Z give the CIE chromaticies.

Parameters:
  • X (numpy.ndarray) – values for the Z dimension
  • Y (numpy.ndarray) – values for the Z dimension
  • Z (numpy.ndarray) – values for the Z dimension
  • YN, ZN (XN,) – chromaticity of the white point. If of length 1 the white point specification will be recycled if length of R/G/B is larger than one. If not specified (all three NA) default values will be used
Returns:

Returns corresponding X/Y/Z coordinates of CIE chromaticies, a list of numpy.ndarray’s of the same length as the inputs ([R, G, B]).

Return type:

list

XYZ_to_uv(X, Y, Z)[source]

CIE-XYZ to u and v.

Parameters:
  • X (numpy.ndarray) – values for the Z dimension.
  • Y (numpy.ndarray) – values for the Y dimension.
  • Z (numpy.ndarray) – values for the Z dimension.
Returns:

Returns a list of numpy.ndarrays containing u and v ([u, v]).

Return type:

list

ftrans(u, gamma)[source]

Gamma Correction.

Function gtrans() and ftrans() provide gamma correction between the RGB (device independent) and sRGB (device dependent) color space.

The standard value of gamma for sRGB displays is approximately 2.2, but more accurately is a combination of a linear transform and a power transform with exponent 2.4. gtrans() maps linearized sRGB to sRGB, ftrans() provides the inverse map.

Parameters:
  • u (numpy.ndarray) – float array of length N
  • gamma (float or numpy.ndarray) – gamma value. If float or numpy.ndarray of length one gamma will be recycled (if length u > 1)
Returns:

Same length as input u.

Return type:

numpy.ndarray

gtrans(u, gamma)[source]

Gamma Correction.

Function gtrans and ftrans provide gamma correction which can be used to switch between sRGB and linearized sRGB (RGB).

The standard value of gamma for sRGB displays is approximately 2.2, but more accurately is a combination of a linear transform and a power transform with exponent 2.4 gtrans maps linearized sRGB to sRGB, ftrans provides the inverse map.

Parameters:
  • u (numpy.ndarray) – float array of length N
  • gamma (float or numpy.ndarray) – gamma value. If float or numpy.ndarray of length one gamma will be recycled (if length u > 1)
Returns:

Same length as input u.

Return type:

numpy.ndarray

hex_to_sRGB(hex_, gamma = 2.4)[source]

Convert hex colors to sRGB.

Parameters:
  • hex (str, list of str) – hex strings.
  • gamma (float) – gamma correction factor.
Returns:

Returns a list of numpy.ndarrays with the corresponding red, green, and blue intensities ([0.,1.]).

Return type:

list

polarLAB_to_LAB(L, A, B)[source]

Convert form polarLAB to onvert CIELAB.

Parameters:
  • L (numpy.ndarray) – values for the L dimension of the polar LAB color space
  • A (numpy.ndarray) – values for the A dimension of the polar LAB color space
  • B (numpy.ndarray) – values for the B dimension of the polar LAB color space
Returns:

Returns corresponding CIELAB chromaticies, a list of numpy.ndarray’s of the same length as the inputs ([L, A, B]).

Return type:

list

polarLUV_to_LUV(L, C, H)[source]

polarLUV (HCL) to LUV.

Parameters:
  • L (numpy.ndarray) – values for the L or luminance dimension
  • C (numpy.ndarray) – values for the C or chroma dimension
  • H (numpy.ndarray) – values for the H or hue dimension
Returns:

Returns a list of polarLUV or HCL coordinates ([L, U, V]) with the same length as the input arrays.

Return type:

list

sRGB_to_XYZ(R, G, B, XN = None, YN = None, ZN = None)[source]

sRGB to CIEXYZ.

R, G, and B give the levels of red, green and blue as values in the interval [0.,1.]. X, Y and Z give the CIE chromaticies.

Parameters:
  • R (numpy.ndarray) – indensities for red ([0.,1.])
  • G (numpy.ndarray) – indensities for green ([0.,1.])
  • B (numpy.ndarray) – indensities for blue ([0.,1.])
  • YN, ZN (XN,) – chromaticity of the white point. If of length 1 the white point specification will be recycled if length of R/G/B is larger than one. If not specified (all three None) default values will be used
Returns:

Returns corresponding X/Y/Z coordinates of CIE chromaticies, a list of numpy.ndarray’s of the same length as the inputs ([X, Y, Z]).

Return type:

list

sRGB_to_hex(r, g, fixup = True)[source]

sRGB colors to hex colors.

Parameters:
  • r (numpy.ndarray) – intensities for red ([0.,1.,])
  • g (numpy.ndarray) – intensities for green ([0.,1.,])
  • b (numpy.ndarray) – intensities for blue ([0.,1.,])
  • fixup (bool) – whether or not the rgb values should be corrected if they lie outside the defined RGB space (outside [0.,1.,])
Returns:

A list with hex colors as strings.

Return type:

list