<--previous | contents | next-->

Colours

Both PyGDChart and GDChart itself store and manipulate colours as RGB integers. Users will be familiar with RGB colour definitions from other contexts - for instance, colours in HTML are often specified in the form "#RRGGBB", where each component is a hex number between 0 and 256. Since the naked RGB integer is cumbersome to work with, the PyGDChart library provides a number of simple facilities to make the programmer's life easier.

The RGB Class

The first of the colour manipulation facilities provided by PyGDChart is the RGB class. This class is simply a way to collect and manipulate the components of a normal RGB colour definition. It is instantiated as follows:

x = gdchart.RGB(r, g, b)

After instantiation, an RGB object exposes the attributes r, g, and b, which can be inspected and set independently. RGB objects have an __int__ method that converts the RGB colour definition to the correct numerical value. As such, an RGB object can be used wherever a colour definition is required.

The rgbFactory() function

The rgbFactory() function is an easy way to manufacture RGB objects corresponding to common colours:

x = gdchart.rgbFactory("blue")

At the moment only entries for "blue", "red", "green", "orange", "white", "black" and "yellow" are provided, but this list will grow in future.

Colours and PyGDChart

From the preceding sections, we already know of two major ways to specify a colour to PyGDChart. Firstly, as a plain integer:

x.bg_color = 0x3232CC

And secondly, as an RGB object:

x.bg_color = gdchart.rgbFactory("blue")

From the latter example, however, an even more convenient way of specifying a colour leaps to mind. Since options in PyGDChart are "smart", we can check wether an option has been passed a string, and call rgbFactory automatically to generate an RGB object. This leads us to the third way of specifying colours in PyGDChart - simply by using a descriptive string:

x.bg_color = "blue"

<--previous | contents | next--> (12/31/03)
PyGDChart User's Manual