Cross-Backend Abstraction: Colors

All backends work with variations of RGB, RGBA or RGBa color spaces. Some use pre-multiplied alpha values, some do not. Some accept RGB values as floats from 0.0 to 1.0, some accept RGB values as integers from 0 to 255, some expect hexadecimal notations as strings. The bewegung.Color class tries to provide a common base for working with RGB(A) colors in different notations.

The Color API

class bewegung.Color(r, g, b, a=255)

Common infrastructure for working with RGBA colors in different formats.

Color objects are immutable.

Parameters
  • r (int) – red channel 0…255 (uint8)

  • g (int) – green channel 0…255 (uint8)

  • b (int) – blue channel 0…255 (uint8)

  • a (int) – alpha channel 0…255 (uint8), opaque by default

property a: int

alpha channel

Return type

int

as_hex(alpha=True)

Exports color as hexadecimal string

Parameters

alpha (bool) – Allows to disable alpha channel on export

Return type

str

as_opaque()

Exports color as a new, fully opaque version of itself

Return type

ColorABC

as_rgba_float()

Exports color a tuple of floats 0.0…1.0

Return type

Tuple[float, float, float, float]

as_rgba_int()

Exports color a tuple of ints 0…255 (uint8)

Return type

Tuple[int, int, int, int]

as_transparent()

Exports color as a new, fully transparent version of itself

Return type

ColorABC

property b: int

blue channel

Return type

int

classmethod from_hex(cls, raw)

Imports color from a hexadecimal string

Parameters

raw (str) – Accepts strings both with and without alpha channel. Opaque by default.

Return type

ColorABC

classmethod from_hsv(cls, h, s, v)

Imports color from HSV

Parameters
  • h (float) – hue 0.0…360.0

  • s (float) – saturation 0.0…1.0

  • v (float) – value (brightness) 0.0…1.0

classmethod from_rgba_float(cls, r, g, b, a=1.0)

Imports color from floats

Parameters
  • r (float) – red channel 0.0…1.0

  • g (float) – green channel 0.0…1.0

  • b (float) – blue channel 0.0…1.0

  • a (float) – alpha channel 0.0…1.0, opaque by default

Return type

ColorABC

property g: int

green channel

Return type

int

property r: int

red channel

Return type

int