pystacia.color

Color factories

These functions exist for easy creation of Color objects from different input formats.

pystacia.color.from_string(value, factory=None)

Create Color from string.

Parameters:
  • value (str) – CSS color specification
  • factory – alternative Color subclass to use
Return type:

Color or factory instance

Creates new instance from a valid color specification string as in CSS 2.1. Supported formats include rgb, rgba, hsl, hsla, color identifiers, hexadecimal values, integer and percent values where applicable. When factory is specified this type is used instead of default Color type.

>>> from_string('red')
<Color(r=1,g=0,b=0,a=1) object at 0x10320e400L>
>>> from_string('rgb(1,1,0)')
<Color(r=1,g=1,b=0,a=1) object at 0x103270600L>
>>> from_string('#fff')
<Color(r=1,g=1,b=1,a=1) object at 0x103251800L>
>>> from_string('hsla(50%, 100%, 100%, 0.5)')
<Color(r=0,g=1,b=1,a=0.5) object at 0x103252a00L>
pystacia.color.from_rgb(r, g, b, factory=None)

Create opaque Color from red, green and blue components.

Parameters:
  • r – red component
  • g – green component
  • b – blue component
  • factory – alternative Color subclass to use
Return type:

Color or factory instance

Red, green and blue components should be numbers between 0 and 1 inclusive. Resulting color is opaque (alpha channel equal to 1). When factory is specified this type is used instead of default Color type.

>>> from_rgb(0.5, 1, 0.5)
<Color(r=0.5,g=1,b=0.5,a=1) object at 0x103266200L>
pystacia.color.from_rgba(r, g, b, a, factory=None)

Create Color from red, green, blue and alpha components.

Parameters:
  • r – red component
  • g – green component
  • b – blue component
  • a – alpha component
  • factory – alternative Color subclass to use
Return type:

Color or factory instance

Red, green, blue and alpha components should be numbers between 0 and 1 inclusive.

>>> from_rgba(1, 1, 0, 0.5)
<Color(r=1,g=1,b=0,a=0.5) object at 0x103222600L>

The Color class

class pystacia.color.Color(resource=None)

Object representing color information.

a

Convenience synonym for alpha.

alpha

Set or get alpha channel information.

The value ought to be a float between 0 and 1.

Return type:float or int
b

Convenience synonym for blue.

blue

Set or get blue channel information.

The value ought to be a float between 0 and 1.

Return type:float or int
g

Convenience synonym for green.

get_hsl()

Return hue, saturation and lightness components.

Return type:tuple
get_int24()

Return RGB triplet as single 24 bit integer.

Returns an integer representing this color. Highest 8 bit represent red channel information.

get_rgb()

Return red, green and blue components.

Return type:tuple

Returns tuple containing red, green and blue channel information as numbers between 0 and 1.

get_rgb8()

Return red, gren and blue components as 8bit integers

Return type:tuple

Returns tuple containing red, green and blue in this order as 8bit integers randing from 0 to 255

get_rgba()

Return red, green, blue and alpha components.

Return type:tuple

Returns tuple containing red, green, blue and alpha channel information as numbers between 0 and 1.

get_string()

Return string representation of color.

Return type:str

Returns standard CSS string representation of color either rgb(r, g, b) or rgba(r, g, b, a) when color is not fully opaque.

green

Set or get green channel information.

The value ought to be a float between 0 and 1.

Return type:float or int
opaque

Check if color is fully opaque.

Return type:bool

Returns True if alpha component is exactly equal to 1, otherwise False.

>>> from_string('red').opaque
True
>>> from_string('transparent').opaque
False
r

Convenience synonym for red.

red

Set or get red channel information.

The value ought to be a float between 0 and 1.

Return type:float or int
set_rgb(r, g, b)

Set red, green and blue components all at once.

Parameters:
  • r – red component
  • g – green component
  • b – blue component

Components should be numbers between 0 and 1. Alpha component remains unchanged.

set_rgba(r, g, b, a)

Set red, green, blue and alpha components all at once.

Parameters:
  • r – red component
  • g – green component
  • b – blue component
  • a – alpha component

Components should be numbers between 0 and 1.

set_string(value)

Resets color value of this instance from string.

Usage and parameters identical to factory function from_string().

transparent

Check if color is fully transparent.

Return type:bool

Returns True if alpha component is exactly equal to 0, otherwise False.

>>> from_string('blue').transparent
False
>>> from_string('transparent').transparent
True

Table Of Contents

Related Topics

This Page