Color Classes

Module containing color classes that allow for easy conversion between color spaces.

This module contains different classes that represent a few of the most common color spaces.

Although these classes may be used individually, they were originally designed to be used within a ColorDict object.

class colordict.color.sRGBColor(r, g, b, a=None, norm=255, include_a=False, round_to=- 1)

Bases: colordict.color.ColorTupleBase

Represents a color in the RGB color space.

Parameters
  • r (float) – Red component of the color.

  • g (float) – Green component of the color.

  • b (float) – Blue component of the color.

  • a (float) – Opacity component of the color. Defaults to None, which means it will be the same as the norm parameter.

  • norm – What the r, g, b, and a components are normalized to (aka their highest possible value). Some common values for this parameter would be 255 or 1.

  • include_a – Whether to include the opacity parameter a in the contructed tuple. Setting it to True may result in an object such as sRGBColor(255, 255, 0, 255) instead of sRGBColor(255, 255, 0), for exemple.

  • round_to – Rounds the value of each color component to this many decimal places. Setting this parameter to 0 ensures that the components will be of type int. The default, -1, means that the components won’t be rounded at all.

class colordict.color.HSLColor(h, s, l, a=None, h_norm=360, sla_norm=1, include_a=False, round_to=- 1)

Bases: colordict.color.ColorTupleBase

Represents a color in the HSL color space.

Parameters
  • h (float) – HUE component of the color.

  • s (float) – Saturation component of the color.

  • l (float) – Lightness component of the color.

  • a (float) – Opacity component of the color. Defaults to None, which means it will be the same as the sla_norm parameter.

  • h_norm – What the h component is normalized to (aka its highest possible value). Some common values for this parameter would be 360 or 1.

  • sla_norm – What the s, l and a components are normalized to (aka their highest possible value). Some common values for this parameter would be 1 or 100.

  • include_a – Whether to include the opacity parameter a in the contructed tuple. Setting it to True may result in an object such as HSLColor(360, 1, 0, 1) instead of HSLColor(360, 1, 0), for exemple.

  • round_to – Rounds the value of each color component to this many decimal places. Setting this parameter to 0 ensures that the components will be of type int. The default, -1, means that the components won’t be rounded at all.

class colordict.color.HSVColor(h, s, v, a=None, h_norm=360, sva_norm=1, include_a=False, round_to=- 1)

Bases: colordict.color.ColorTupleBase

Represents a color in the HSV color space.

Parameters
  • h (float) – HUE component of the color.

  • s (float) – Saturation component of the color.

  • v (float) – Value component of the color.

  • a (float) – Opacity component of the color. Defaults to None, which means it will be the same as the sva_norm parameter.

  • h_norm – What the h component is normalized to (aka its highest possible value). Some common values for this parameter would be 360 or 1.

  • sva_norm – What the s, v and a components are normalized to (aka their highest possible value). Some common values for this parameter would be 1 or 100.

  • include_a – Whether to include the opacity parameter a in the contructed tuple. Setting it to True may result in an object such as HSVColor(360, 1, 0, 1) instead of HSVColor(360, 1, 0), for exemple.

  • round_to – Rounds the value of each color component to this many decimal places. Setting this parameter to 0 ensures that the components will be of type int. The default, -1, means that the components won’t be rounded at all.

class colordict.color.CMYKColor(c, m, y, k, a=None, norm=1, include_a=False, round_to=- 1)

Bases: colordict.color.ColorTupleBase

Represents a color in the CMYK color space.

Parameters
  • c (float) – Cyan component of the color.

  • m (float) – Magenta component of the color.

  • y (float) – Yellow component of the color.

  • k (float) – Key (black) component of the color.

  • a (float) – Opacity component of the color. Defaults to None, which means it will be the same as the norm parameter.

  • norm – What the c, m, y, k, and a components are normalized to (aka their highest possible value). Some common values for this parameter would be 1 or 100.

  • include_a – Whether to include the opacity parameter a in the contructed tuple. Setting it to True may result in an object such as CMYKColor(1, 1, 0, 1) instead of CMYKColor(1, 1, 0), for exemple.

  • round_to – Rounds the value of each color component to this many decimal places. Setting this parameter to 0 ensures that the components will be of type int. The default, -1, means that the components won’t be rounded at all.

class colordict.color.CMYColor(c, m, y, a=None, norm=1, include_a=False, round_to=- 1)

Bases: colordict.color.ColorTupleBase

Represents a color in the CMY color space.

Parameters
  • c (float) – Cyan component of the color.

  • m (float) – Magenta component of the color.

  • y (float) – Yellow component of the color.

  • a (float) – Opacity component of the color. Defaults to None, which means it will be the same as the norm parameter.

  • norm – What the c, m, y and a components are normalized to (aka their highest possible value). Some common values for this parameter would be 1 or 100.

  • include_a – Whether to include the opacity parameter a in the contructed tuple. Setting it to True may result in an object such as CMYColor(1, 1, 0, 1) instead of CMYColor(1, 1, 0), for exemple.

  • round_to – Rounds the value of each color component to this many decimal places. Setting this parameter to 0 ensures that the components will be of type int. The default, -1, means that the components won’t be rounded at all.

class colordict.color.HexColor(hex_str, include_a=False)

Bases: colordict.color.ColorBase, str

Represents a color in the RGB color space as a hexadecimal string.

Is mostly used for representing colors in web applications.

Parameters
  • hex_str (str) – Hexadecimal string from which the HexColor instance will be built. May or may not include a “#” character in its beggining.

  • include_a – Whether to include the opacity parameter a in the contructed tuple. Setting it to True may result in an object such as HexColor('#ffffff00') instead of HexColor('#ffff00'), for exemple.

class colordict.color.ColorBase(rgba, **kwargs)

Bases: object

Base class from which all color classes inherit.

Note

This class is abstract and should not be instanciated.

hex(**kwargs)

Converts the current color to a hexadecimal representation.

Parameters

**kwargs – Keyword arguments wrapped in this function will be passed on to the HexColor constructor.

rgb(**kwargs)

Converts the current color to an sRGB representation.

Parameters

**kwargs – Keyword arguments wrapped in this function will be passed on to the sRGBColor constructor.

hsl(**kwargs)

Converts the current color to an HSL representation.

Parameters

**kwargs – Keyword arguments wrapped in this function will be passed on to the HSLColor constructor.

hsv(**kwargs)

Converts the current color to an HSV representation.

Parameters

**kwargs – Keyword arguments wrapped in this function will be passed on to the HSVColor constructor.

cmyk(**kwargs)

Converts the current color to a CMYK representation.

Parameters

**kwargs – Keyword arguments wrapped in this function will be passed on to the CMYKColor constructor.

cmy(**kwargs)

Converts the current color to a CMY representation.

Parameters

**kwargs – Keyword arguments wrapped in this function will be passed on to the CMYColor constructor.

class colordict.color.ColorTupleBase(iterable, rgba, include_a=False, round_to=- 1)

Bases: colordict.color.ColorBase, tuple

Base class from which all color classes that are represented by tuples inherit.

Note

This class is abstract and should not be instanciated.