pyface.color module

Color classes and corresponding trait types for Pyface.

The base Color class holds red, green, blue and alpha channel values as a tuple of normalized values from 0.0 to 1.0. Various property traits pull out the individual channel values and supply values for the HSV and HSL colour spaces (with and without alpha).

The from_toolkit and to_toolkit methods allow conversion to and from native toolkit color objects.

pyface.color.Channel = Range(0.0, 1.0, value=1.0, channel=True)

A trait holding a single channel value.

pyface.color.ChannelTuple = Tuple(Channel, Channel, Channel)

A trait holding three channel values.

pyface.color.AlphaChannelTuple = Tuple(Channel, Channel, Channel, Channel)

A trait holding four channel values.

class pyface.color.Color

Bases: traits.has_traits.HasStrictTraits

A mutable specification of a color with alpha.

This is a class designed to be used by user interface elements which need to color some or all of the interface element. Each color has a number of different representations as channel tuples, each channel holding a value between 0.0 and 1.0, inclusive. The standard red, green, blue and alpha channels are also provided as convenience properties.

Methods are provided to convert to and from toolkit-specific color objects.

Colors implement equality testing, but are not hashable as they are mutable, and so are not suitable for use as dictionary keys. If you need a dictionary key, use an appropriate channel tuple from the object.

rgba = AlphaChannelTuple()

A tuple holding the red, green, blue, and alpha channels.

rgb = Property(ChannelTuple(), depends_on='rgba')

A tuple holding the red, green, and blue channels.

red = Property(Channel, depends_on='rgba')

The red channel.

green = Property(Channel, depends_on='rgba')

The green channel.

blue = Property(Channel, depends_on='rgba')

The blue channel.

alpha = Property(Channel, depends_on='rgba')

The alpha channel.

hsva = Property(AlphaChannelTuple, depends_on='rgba')

A tuple holding the hue, saturation, value, and alpha channels.

hsv = Property(ChannelTuple, depends_on='rgb')

A tuple holding the hue, saturation, and value channels.

hlsa = Property(AlphaChannelTuple, depends_on='rgba')

A tuple holding the hue, lightness, saturation, and alpha channels.

hls = Property(ChannelTuple, depends_on='rgb')

A tuple holding the hue, lightness, and saturation channels.

is_dark = Property(Bool, depends_on='rgba')

Whether the color is dark for contrast purposes.

classmethod from_str(text, **traits)

Create a new Color object from a string.

Parameters
  • text (str) –

    A string holding the representation of the color. This can be:

    • a color name, including all CSS color names, plus any additional names found in pyface.color.color_table. The names are normalized to lower case and stripped of whitespace, hyphens and underscores.

    • a hex representation of the color in the form ‘#RGB’, ‘#RGBA’, ‘#RRGGBB’, ‘#RRGGBBAA’, ‘#RRRRGGGGBBBB’, or ‘#RRRRGGGGBBBBAAAA’.

  • **traits – Any additional trait values to be passed as keyword arguments.

Raises

ColorParseError – If the string cannot be converted to a valid color.

classmethod from_toolkit(toolkit_color, **traits)

Create a new Color object from a toolkit color object.

Parameters
  • toolkit_color (toolkit object) – A toolkit color object, such as a Qt QColor or a Wx wx.Colour.

  • **traits – Any additional trait values to be passed as keyword arguments.

to_toolkit()

Create a new toolkit color object from a Color object.

Returns

toolkit_color – A toolkit color object, such as a Qt QColor or a Wx wx.Colour.

Return type

toolkit object

hex()

Provide a hex representation of the Color object.

Note that because the hex value is restricted to 0-255 integer values for each channel, the representation is not exact.

Returns

hex – A hex string in standard #RRGGBBAA format that represents the color.

Return type

str