color Module#

Classes:

Color

A color.

class term_image.color.Color(r, g, b, a=255)[source]#

Bases: _DummyColor

A color.

Parameters:
  • r (int) – The red channel.

  • g (int) – The green channel.

  • b (int) – The blue channel.

  • a (int) – The alpha channel (opacity).

Raises:

ValueError – The value of a channel is not within the valid range.

Note

The valid value range for all channels is 0 to 255, both inclusive.

Tip

This class is a NamedTuple of four fields.

Warning

In the case of multiple inheritance i.e if subclassing this class along with other classes, this class should appear last (i.e to the far right) in the base class list.

Attributes:

r

The red channel

g

The green channel

b

The blue channel

a

The alpha channel (opacity)

Instance Properties:

hex

Converts the color to its RGBA hexadecimal representation.

rgb

Extracts the R, G and B channels of the color.

rgb_hex

Converts the color to its RGB hexadecimal representation.

Class Methods:

from_hex

Creates a new instance from a hexadecimal color string.

r: int#

The red channel

g: int#

The green channel

b: int#

The blue channel

a: int#

The alpha channel (opacity)

property hex: str#

Converts the color to its RGBA hexadecimal representation.

Returns:

The RGBA hex color string, starting with the # character i.e #rrggbbaa.

Each channel is represented by two lowercase hex digits ranging from 00 to ff.

property rgb: tuple[int, int, int]#

Extracts the R, G and B channels of the color.

Returns:

A 3-tuple containing the red, green and blue channel values.

property rgb_hex: str#

Converts the color to its RGB hexadecimal representation.

Returns:

The RGB hex color string, starting with the # character i.e #rrggbb.

Each channel is represented by two lowercase hex digits ranging from 00 to ff.

classmethod from_hex(color)[source]#

Creates a new instance from a hexadecimal color string.

Parameters:

color (str) – A case-insensitive RGB or RGBA hex color string, optionally starting with the # (pound) character i.e [#]rrggbb[aa].

Returns:

A new instance representing the given hex color string.

Raises:

ValueError – Invalid hex color string.

Return type:

Self

Note

For an RGB hex color string, the value of A (the alpha channel) is taken to be 255.