ll.color – RGB colors and color model conversion

ll.color provides classes and functions for handling RGBA colors.

class ll.color.Color[source]

Bases: tuple

A Color object represents a color with 8-bit red, green and blue components and opacity.

static __new__(cls, r: int = 0, g: int = 0, b: int = 0, a: int = 255)[source]

Create a Color with the 8 bit red, green, blue and alpha components r, g, b and a. Values will be clipped to the range [0; 255].

classmethod fromcss(s: str) Color[source]

Create a Color object from the CSS color string s. All formats from CSS2 are supported (i.e. '#xxx', '#xxxxxx', rgb(r, g, b), rgb(r%, g%, b%), rgba(r, g, b, a), rgba(r%, g%, b%, a) and color names like 'red').

classmethod fromrgb(r: int | float, g: int | float, b: int | float, a: int | float = 1.0) Color[source]

Create a Color object from the red, green, blue and alpha values r, g, b and a. All values will be clipped to the range [0; 1].

classmethod fromhsv(h: int | float, s: int | float, v: int | float, a: int | float = 1.0) Color[source]

Create a Color object from the hue, saturation and value values h, s and v and the alpha value a. The hue value will be used modulo 1.0, saturation, value and alpha will be clipped to the range [0; 1].

classmethod fromhls(h: int | float, l: int | float, s: int | float, a: int | float = 1.0) Color[source]

Create a Color object from the hue, luminance and saturation values h, l and s and the alpha value a. The hue value will be used modulo 1.0, luminance, saturation and alpha will be clipped to the range [0; 1].

__str__() str[source]

self formatted as a CSS color string.

r() int[source]

The red value as an int between 0 and 255.

g() int[source]

The green value as an int between 0 and 255.

b() int[source]

The blue value as an int between 0 and 255.

a() int[source]

The alpha value as an int between 0 and 255.

rgb() Tuple[float, float, float][source]

The red, green and blue value as a float tuple with values between 0.0 and 1.0.

rgba() Tuple[float, float, float, float][source]

The red, green, blue and alpha value as a float tuple with values between 0.0 and 1.0.

hsv() Tuple[float, float, float][source]

self as a HSV tuple (“hue”, “saturation”, “value”). All three values are between 0.0 and 1.0.

hsva() Tuple[float, float, float, float][source]

self as a HSV+alpha tuple (“hue”, “saturation”, “value”, “alpha”). All four values are between 0.0 and 1.0.

hls() Tuple[float, float, float][source]

self as a HLS tuple (“hue, luminance, saturation”). All three values are between 0.0 and 1.0.

hlsa() Tuple[float, float, float, float][source]

self as a HLS+alpha tuple (“hue, luminance, saturation, alpha”). All four values are between 0.0 and 1.0.

hue() float[source]

The hue value from hls().

light() float[source]

The lightness value from hls().

sat() float[source]

The saturation value from hls().

lum() float[source]

Luminance according to sRGB:

(0.2126*r + 0.7152*g + 0.0722*b)/255
withhue(hue: int | float) Color[source]

Return a new color with the HLS hue replaced by hue.

withlight(light: int | float) Color[source]

Return a new color with the HLS lightness replaced by light

withsat(sat: int | float) Color[source]

Return a new color with the HLS saturation replaced by sat

withlum(lum: int | float) Color[source]

Return a copy of self where the luminance has been replace with lum.

witha(a: int) Color[source]

Return a copy of self with the alpha value replaced with a.

abslight(f: int | float) Color[source]

Return a copy of self with f added to the HLS lightness.

rellight(f: int | float) Color[source]

Return a copy of self where the lightness has been modified: If f is positive the lightness will be increased, with f==1 giving a lightness of 1. If f is negative, the lightness will be decreased with f==-1 giving a lightness of 0. f==0 will leave the lightness unchanged.

abslum(f: int | float) Color[source]

Return a copy of self where f has been added to the lum value.

rellum(f: int | float) Color[source]

Return a copy of self where the luminance has been modified: If f is positive the luminance will be increased, with f==1 giving a luminance of 1. If f is negative, the luminance will be decreased with f==-1 giving a luminance of 0. f==0 will leave the luminance unchanged. All other values return a linear interpolation.

combine(r: int | float = None, g: int | float = None, b: int | float = None, a: int | float = None) Color[source]

Return a copy of self with some of its components replaced by the arguments. If a component is nont passed (or the value None is given) the component will be unchanged in the resulting color.

invert(f: int | float = 1.0) Color[source]

Return an inverted version of self, i.e. for each color c the following prints True three times:

<?print c.invert().r() == 255 - c.r()?>
<?print c.invert().g() == 255 - c.g()?>
<?print c.invert().b() == 255 - c.b()?>

f specifies the amount of inversion, with 1 returning a complete inversion, and 0 returning the original color. Values between 0 and 1 return an interpolation of both extreme values. (And 0.5 always returns medium grey).

__mod__(other: Color) Color[source]

Blends self with the background color other according to the CSS specification

ll.color.css(value: str, default: str | None = <object object>, /) Color[source]

Create a Color object from the CSS color string value via Color.fromcss().

If value is no valid CSS color string and default is given, return default instead.

ll.color.dist(c1: Color, c2: Color, /) float[source]

Return the distance between two colors.

ll.color.multiply(c1: Color, c2: Color, /) Color[source]

Multiplies the colors c1 and c2.

ll.color.screen(c1: Color, c2: Color, /) Color[source]

Does a negative multiplication of the colors c1 and c2.

ll.color.mix(*args) Color[source]

Calculates a weighted mix of the colors from args. Items in args are either colors or weights. The following example mixes two parts black with one part white:

>>> from ll import color
>>> color.mix(2, color.black, 1, color.white)
Color(0x55, 0x55, 0x55)