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
rgborhslclass. - 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
¶
toga.colors.rgb
¶
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:
|
g
|
The color's green value, as an integer from 0 to 255. Higher or lower values will be clipped.
TYPE:
|
b
|
The color's blue value, as an integer from 0 to 255. Higher or lower values will be clipped.
TYPE:
|
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:
|
toga.colors.hsl
¶
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:
|
s
|
The color's saturation, as a float from 0 to 1. Higher or lower values will be clipped.
TYPE:
|
l
|
The color's lightness, as a float from 0 to 1. Higher or lower values will be clipped.
TYPE:
|
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:
|