Skip to content

Color

A representation of a color.

Usage

When specifying colors for style properties such as color and background-color, or on widget APIs that use colors explicitly (such as any color arguments for the Canvas API), Toga will accept values in a range of possible formats.

Reference

toga.colors.ColorT module-attribute

ColorT: TypeAlias

Toga's color APIs accept:

  • An instance of Toga's rgb or hsl class.
  • The name of a CSS named color. Strings of all available color names are available as constants. These constants are named the same as their value, except in all caps, as is the convention for Python constants. In other words, toga.colors.ORANGE == "orange".
  • A string representing the color as hexadecimal RGB or RGBA.

The following color definitions would all be equivalent:

import toga
from toga.colors import hsl, rgb, REBECCAPURPLE

toga.Box(color=rgb(102, 51, 153))
toga.Box(color=hsl(270, 0.5, 0.4))
toga.Box(color="rebeccapurple")
toga.Box(color=REBECCAPURPLE)
toga.Box(color="#639")
toga.Box(color="#663399")

None of these specify alpha (transparency), so the color defaults to fully opaque. rgb, hsl, and the hex string format allow alpha to be specified as well. Thus, the following, which explicitly set the alpha to opaque, are also equivalent:

toga.Box(color=rgb(102, 51, 153, 1.0))
toga.Box(color=hsl(270, 0.5, 0.4, 1.0))
toga.Box(color="#639F")
toga.Box(color="#663399FF")

As is the case in CSS, rgba and hsla are available as aliases for rgb and hsl, respectively. rgb and hsl are the preferred forms, but either will work identically.

toga.colors.Color

Bases: ABC

A base class for all colorspace representations.

Not meant to be used directly.

rgb abstractmethod property

rgb

This color in RGB format. If it's already RGB, returns the object itself.

Also accessible via the alias rgba.

hsl abstractmethod property

hsl

This color in HSL format. If it's already HSL, returns the object itself.

Also accessible via the alias rgba.

toga.colors.rgb

rgb(r: int, g: int, b: int, a: float = 1.0)

Bases: Color

A color specified via red, green, and blue channels, plus transparency.

Also accessible via the alias rgba.

PARAMETER DESCRIPTION
r

The color's red value, as an integer from 0 to 255. Higher or lower values will be clipped.

TYPE: int

g

The color's green value, as an integer from 0 to 255. Higher or lower values will be clipped.

TYPE: int

b

The color's blue value, as an integer from 0 to 255. Higher or lower values will be clipped.

TYPE: int

a

The color's alpha, or transparency, as a float from 0 to 1. Higher or lower values will be clipped. Defaults to fully opaque.

TYPE: float DEFAULT: 1.0

r property

r: int

The color's red value, as an integer from 0 to 255.

g property

g: int

The color's green value, as an integer from 0 to 255.

b property

b: int

The color's blue value, as an integer from 0 to 255.

a property

a: float

The color's alpha, or transparency, as a float from 0 to 1.

toga.colors.hsl

hsl(h: int, s: float, l: float, a: float = 1.0)

Bases: Color

A color specified via hue, saturation, and lightness, plus transparency.

Also accessible via the alias hsla.

PARAMETER DESCRIPTION
h

The color's hue, as an integer from 0 to 360. Higher or lower values will wrap around.

TYPE: int

s

The color's saturation, as a float from 0 to 1. Higher or lower values will be clipped.

TYPE: float

l

The color's lightness, as a float from 0 to 1. Higher or lower values will be clipped.

TYPE: float

a

The color's alpha, or transparency, as a float from 0 to 1. Higher or lower values will be clipped. Defaults to fully opaque.

TYPE: float DEFAULT: 1.0

h property

h: int

The color's hue, as an integer from 0 to 360.

s property

s: float

The color's saturation, as a float from 0 to 1.

l property

l: float

The color's lightness, as a float from 0 to 1.

a property

a: float

The color's alpha, or transparency, as a float from 0 to 1.