Colors

Colors can have one of the following types:

class thcolor.Color.Type

Class representing the type of a color, or how it is expressed. The following types are available:

INVALID

An invalid color, for internal processing.

RGB

A color expressed through its sRGB components: red, green and blue.

HSL

A color expressed through its HSL components: hue, saturation and lightness.

HWB

A color expressed through its HWB components: hue, whiteness and blackness.

CMYK

A color expressed through its CMYK components: cyan, magenta, yellow and black.

An alpha component can be added to every single one of these types, so it is not included in the type names.

Colors are represented in thcolor as instances of the following class:

class thcolor.Color(*args, **kwargs)

Class representing a color within thcolor. Its constructor depends on the first given argument, which represents the color type as one of the Color.Type constants.

Color(Color.Type.RGB, red, green, blue, alpha = 1.0)

Create a color using its red, green and blue components. Each is expressed as a byte value, from 0 (dark) to 255 (light).

An alpha value going from 0.0 (invisible) to 1.0 (opaque) can be appended to the base components.

Color(Color.Type.HSL, hue, saturation, lightness, alpha = 1.0)

Create a color using its hue, saturation and lightness components. The hue is represented by an Angle object, and the saturation and lightness are values going from 0.0 to 1.0.

An alpha value going from 0.0 (invisible) to 1.0 (opaque) can be appended to the base components.

Color(Color.Type.HWB, hue, whiteness, blackness, alpha = 1.0)

Create a color using its hue, whiteness and blackness components. The hue is represented by an Angle object, and the whiteness and lightness are values going from 0.0 to 1.0.

An alpha value going from 0.0 (invisible) to 1.0 (opaque) can be appended to the base components.

Color(Color.Type.CMYK, cyan, magenta, yellow, black, alpha = 1.0)

Create a color using its cyan, magenta, yellow and blackness components, which are all values going from 0.0 to 1.0.

An alpha value going from 0.0 (invisible) to 1.0 (opaque) can be appended to the base components.

cmyk()

Get the CMYK (cyan, magenta, yellow, black) components of the color. For example:

>>> Color.from_text("cmyk(.1 .2 .3 .4)").cmyk()
... (0.1, 0.2, 0.3, 0.4)

If the color is not represented as CMYK internally, it will be converted naively.

cmyka()

Get the CMYK (cyan, magenta, yellow, black) and alpha components of the color. For example:

>>> Color.from_text("cmyk(.1 .2 .3 .4 / 10%)").cmyka()
... (0.1, 0.2, 0.3, 0.4, 0.1)

If the color is not represented as CMYK internally, it will be converted naively.

css()

Get the CSS color descriptions, with older CSS specifications compatibility, as a list of strings.

For example:

>>> Color(Color.Type.RGB, 18, 52, 86, 0.82).css()
... ["#123456", "rgba(18, 52, 86, 82%)"] 
hls()

Alias for hsl() but reverses the lightness and saturation for commodity.

hlsa()

Alias for hsla() but reverses the lightness and saturation for commodity.

hwb()

Get the HWB (hue, whiteness, blackness) components of the color. For example:

>>> Color.from_text("hwb(.7 turn / 5% 10%)").hwb()
... (Angle(type = Angle.Type.TURN, value = 0.7), 0.05, 0.1)

If the color is not represented as HSL internally, it will be converted.

hwba()

Get the HWB (hue, whiteness, blackness) and alpha components of the color. For example:

>>> Color.from_text("hwb(.7 turn / 5% 10% .2)").hwba()
... (Angle(type = Angle.Type.TURN, value = 0.7), 0.05, 0.1, 0.2)

If the color is not represented as HSL internally, it will be converted.

rgb()

Get the sRGB (red, green, blue) components of the color. For example:

>>> Color.from_text("#876543").rgb()
... (135, 101, 67)

If the color is not represented as sRGB internally, it will be converted.

rgba()

Get the sRGB (red, green, blue) and alpha components of the color. For example:

>>> Color.from_text("#87654321").rgb()
... (135, 101, 67, 0.1294)

If the color is not represented as sRGB internally, it will be converted.

type

The read-only angle type as one of the Color.Type constants.