color

class color.Color(*args, **kwargs)

Represents a colour, providing conversions to and from various colour schemes (including RGB, YUV, and HSB). Conversion from these schemes is achieved via the following constructors:

  • from_rgb()

  • from_yuv()

  • from_hsb()

  • from_string()

The main constructor is flexible and permits a variety of styles. The following calls are all equivalent:

>> Color('red')     # colour name
>> Color('#660000') # HTML style
>> Color('#600')    # CSS short-hand
>> Color(102, 0, 0) # R, G, B values
>> Color(g=0, b=0, r=102)     # R, G, B values as keyword args
>> Color(y=42, u=113, v=173)  # Y, U, V values as keyword args
>> Color(h=0.0, s=1.0, b=0.4) # H, S, B values as keyword args

Conversion to these schemes is achieved by querying the following properties:

  • rgb

  • yuv

  • hsb

  • html

Color instances are immutable, but new colours can be easily constructed by using standard mathematical operators on other Color instances:

>>> Color('#112233') + Color('#112233')
Color('#224466')
>>> Color('#112233') * 2
Color('#224466')

Or with instances of RGB, YUV, and HSB:

>>> Color('#112233') + RGB(0x11, 0, 0)
Color('#222233')
>>> Color('#112233') * HSB(1, 1, 2)
Color('#224466')
>>> Color('#112233') - HSB(0, 1, 0)
Color('#333333')
replace(r=None, g=None, b=None, a=None)

Return the colour with the red, green, blue, or alpha component replaced with the specified values. For example:

>>> Color('red').replace(g=0x22)
Color('#662200')
class color.HSB(h, s, b)
b

Alias for field number 2

h

Alias for field number 0

s

Alias for field number 1

class color.RGB(r, g, b)
b

Alias for field number 2

g

Alias for field number 1

r

Alias for field number 0

class color.RGBA(r, g, b, a)
a

Alias for field number 3

b

Alias for field number 2

g

Alias for field number 1

r

Alias for field number 0

class color.YUV(y, u, v)
u

Alias for field number 1

v

Alias for field number 2

y

Alias for field number 0