Colors API

The base class for colors is the following:

class thcolor.colors.Color(alpha=1.0)

Class representing a color within thcolor.

Parameters

alpha (float) – Value for alpha.

property alpha: float

The alpha component value.

Represented as a float varying between 0.0 (invisible) and 1.0 (opaque).

Return type

float

ascmyk()

Get a CMYKColor out of the current object.

Return type

CMYKColor

ashsl()

Get an HSLColor out of the current object.

Return type

HSLColor

ashsv()

Get an HSVColor out of the current object.

Return type

HSVColor

ashwb()

Get an HWBColor out of the current object.

Return type

HWBColor

aslab()

Get a LABColor out of the current object.

Return type

LABColor

aslch()

Get a LCHColor out of the current object.

Return type

LCHColor

assrgb()

Get an SRGBColor out of the current object.

Return type

SRGBColor

asxyz()

Get an XYZColor out of the current object.

Return type

XYZColor

asyiq()

Get an YIQColor out of the current object.

Return type

YIQColor

asyuv()

Get an YUVColor out of the current object.

Return type

YUVColor

css()

Get the CSS color descriptions.

Includes older CSS specifications compatibility, as a sequence of strings.

For example:

>>> SRGBColor.frombytes(18, 52, 86, 0.82).css()
... ("#123456", "rgba(18, 52, 86, 82%)")
Return type

Sequence[str]

darker(by=0.1)

Get a darker version of the given color.

Parameters

by (float) – Percentage by which the color should be darker.

Return type

Color

desaturate(by=0.1)

Get a less saturated version of the given color.

Parameters

by (float) – Percentage by which the color should be desaturated.

Return type

Color

classmethod fromtext(expr, decoder=None)

Create a color from a string.

Parameters

expr (str) – The expression to decode.

Return type

Color

lighter(by=0.1)

Get a lighter version of the given color.

Parameters

by (float) – Percentage by which the color should be lighter.

Return type

Color

replace(**properties)

Get the color with the given properties replaced.

For changing the alpha on an RGB color:

>>> SRGBColor(.1, .2, .3).replace(alpha=.5)
... SRGBColor(red=0.1, green=0.2, blue=0.3, alpha=0.5)

For changing the lightness on an HSL color:

>>> HSLColor(DegreesAngle(270), .5, 1).replace(lightness=.2)
... HSLColor(hue=DegreesAngle(degrees=270.0), saturation=0.5,
...          lightness=0.2, alpha=1.0)
Parameters

properties – Properties to change from the original color.

Return type

Color

saturate(by=0.1)

Get a more saturated version of the given color.

Parameters

by (float) – Percentage by which the color should be saturated.

Return type

Color

Subclasses are the following:

class thcolor.colors.CMYKColor(cyan, magenta, yellow, black, alpha=1.0)

A color expressed using its CMYK channels’ intensities.

Parameters
  • cyan (float) – Value for cyan.

  • magenta (float) – Value for magenta.

  • yellow (float) – Value for yellow.

  • black (float) – Value for black.

  • alpha (float) – Value for alpha.

property alpha: float

The alpha component value.

Represented as a float varying between 0.0 (invisible) and 1.0 (opaque).

Return type

float

property black

Black channel intensity between 0.0 and 1.0.

property cyan

Cyan channel intensity between 0.0 and 1.0.

property magenta

Magenta channel intensity between 0.0 and 1.0.

property yellow

Yellow channel intensity between 0.0 and 1.0.

class thcolor.colors.HSLColor(hue, saturation, lightness, alpha=1.0)

A color expressed using its hue, saturation and lightness components.

Parameters
property alpha: float

The alpha component value.

Represented as a float varying between 0.0 (invisible) and 1.0 (opaque).

Return type

float

property hue: thcolor.angles.Angle

The hue, as an angle.

Return type

Angle

property lightness: float

The lightness, between 0.0 and 1.0.

Return type

float

property saturation: float

The saturation, between 0.0 and 1.0.

Return type

float

class thcolor.colors.HSVColor(hue, saturation, value, alpha=1.0)

A color expressed using its hue, saturation and value components.

Parameters
  • hue (Angle) – Value for hue.

  • saturation (float) – Value for saturation.

  • value (float) – Value for value.

  • alpha (float) – Value for alpha.

property alpha: float

The alpha component value.

Represented as a float varying between 0.0 (invisible) and 1.0 (opaque).

Return type

float

property hue: thcolor.angles.Angle

The hue, as an angle.

Return type

Angle

property saturation: float

The saturation, between 0.0 and 1.0.

Return type

float

property value: float

The value, between 0.0 and 1.0.

Return type

float

class thcolor.colors.HWBColor(hue, whiteness=0.0, blackness=0.0, alpha=1.0)

A color expressed using its hue, whiteness and blackness components.

Parameters
property alpha: float

The alpha component value.

Represented as a float varying between 0.0 (invisible) and 1.0 (opaque).

Return type

float

property blackness: float

The blackness, as a value between 0.0 and 1.0.

Return type

float

property hue: thcolor.angles.Angle

The hue, as an angle.

Return type

Angle

property whiteness: float

The whiteness, as a value between 0.0 and 1.0.

Return type

float

class thcolor.colors.LABColor(lightness, a, b, alpha=1.0)

A color expressed using its CIELAB color space cartesian coordinates.

Parameters
  • lightness (float) – Value for lightness.

  • a (float) – Value for a.

  • b (float) – Value for b.

  • alpha (float) – Value for alpha.

property a: float

The A axis value in the Lab colorspace.

Return type

float

property alpha: float

The alpha component value.

Represented as a float varying between 0.0 (invisible) and 1.0 (opaque).

Return type

float

property b: float

The B axis value in the Lab colorspace.

Return type

float

property lightness: float

The CIE lightness.

Similar to the lightness in the HSL representation. Represented as a float between 0.0 and 1.0.

Return type

float

class thcolor.colors.LCHColor(lightness, chroma, hue, alpha=1.0)

A color expressed using its CIELAB color space polar coordinates.

Parameters
  • lightness (float) – Value for lightness.

  • chroma (float) – Value for chroma.

  • hue (Angle) – Value for hue.

  • alpha (float) – Value for alpha.

property alpha: float

The alpha component value.

Represented as a float varying between 0.0 (invisible) and 1.0 (opaque).

Return type

float

property chroma: float

The chroma.

Represented as a positive number theoretically unbounded.

Return type

float

property hue: thcolor.angles.Angle

The hue, as an angle.

Return type

Angle

property lightness: float

The CIE lightness.

Similar to the lightness in the HSL representation. Represented as a float between 0.0 and 1.0.

Return type

float

class thcolor.colors.SRGBColor(red, green, blue, alpha=1.0)

A color expressed using its channel intensities in the sRGB profile.

Parameters
  • red (float) – Value for red.

  • green (float) – Value for green.

  • blue (float) – Value for blue.

  • alpha (float) – Value for alpha.

property alpha: float

The alpha component value.

Represented as a float varying between 0.0 (invisible) and 1.0 (opaque).

Return type

float

asbytes()

Get the red, blue and green bytes.

Return type

Tuple[int, int, int]

property blue: float

The intensity of the blue channel.

Represented as a float between 0.0 (dark) and 1.0 (light).

Return type

float

classmethod frombytes(red, green, blue, alpha=1.0)

Get an sRGB color from colors using values between 0 and 255.

Return type

SRGBColor

classmethod fromnetscapecolorname(name)

Get an sRGB color from a Netscape color name.

Return type

SRGBColor

property green: float

The intensity of the green channel.

Represented as a float between 0.0 (dark) and 1.0 (light).

Return type

float

property red: float

The intensity of the red channel.

Represented as a float between 0.0 (dark) and 1.0 (light).

Return type

float

class thcolor.colors.XYZColor(x, y, z, alpha=1.0)

A color expressed using its CIEXYZ color space coordinates.

Parameters
  • x (float) – Value for x.

  • y (float) – Value for y.

  • z (float) – Value for z.

  • alpha (float) – Value for alpha.

property alpha: float

The alpha component value.

Represented as a float varying between 0.0 (invisible) and 1.0 (opaque).

Return type

float

property x: float

The CIE X component, between 0.0 and 1.0.

Return type

float

property y: float

The CIE Y component, between 0.0 and 1.0.

Return type

float

property z: float

The CIE Z component, between 0.0 and 1.0.

Return type

float

class thcolor.colors.YIQColor(y, i, q, alpha=1.0)

A color expressed using its YIQ components.

Parameters
  • y (float) – Value for y.

  • i (float) – Value for i.

  • q (float) – Value for q.

  • alpha (float) – Value for alpha.

property alpha: float

The alpha component value.

Represented as a float varying between 0.0 (invisible) and 1.0 (opaque).

Return type

float

property i: float

The orange-blue range value.

Return type

float

property q: float

The purple-green range value.

Return type

float

property y: float

The luma.

Return type

float

class thcolor.colors.YUVColor(y, u, v, alpha=1.0)

A color expressed using its YUV components.

Parameters
  • y (float) – Value for y.

  • u (float) – Value for u.

  • v (float) – Value for v.

  • alpha (float) – Value for alpha.

property alpha: float

The alpha component value.

Represented as a float varying between 0.0 (invisible) and 1.0 (opaque).

Return type

float

property u: float

The U chrominance.

Return type

float

property v: float

The V chrominance.

Return type

float

property y: float

The luma.

Return type

float