gtk.gdk.Color — an object holding color information
Floating-point and HSV attributes are available in PyGTK 2.16 and above.
For details on how assignment to
*_float attributes work,
see constructor documentation.
contains the values of a color that may or may not be allocated. The red,
green and blue attributes are specified by an unsigned integer in the range
0-65535. The pixel value is an index into the colormap that has allocated
Typically a color is allocated by using the
method. Unallocated colors can be used to specify the color attributes of
objects since these colors will be allocated when an attempt is made to use
Starting with PyGTK 2.14
gtk.gdk.Color objects are properly
comparable. By Python rules, colors (being mutable) are now unhashable. If you
need to use them as dictionary keys, use string representation instead. You can
convert string representation to
gtk.gdk.Color objects using
Also beginning with PyGTK 2.14
gtk.gdk.Color objects have
custom support for
functions. For any color it holds that:
color == eval(repr(color))
PyGTK 2.16 introduces several ways of using floating-point numbers for
objects or setting their individual components. In all cases it was decided to
silently clamp input values to valid range, rather than being strict and e.g. raise
an exception. The rationale is that floating-point arithmetics are imprecise, so
you could end with a computed value slightly larger than maximum or a little smaller
than minimum valid value. To simplify using new features, such "slightly off"
values are just clamped without any warning.
|The red color component in the range 0-65535|
|The green color component in the range 0-65535|
|The blue color component in the range 0-65535|
|The index of the color when allocated in its colormap|
|String containing color specification|
|a new |
Second form of the constructor is available in PyGTK 2.14 and above.
blue can be floating-point numbers since PyGTK
Creates a new
with the color component values specified by
blue (all default to
0) and using the pixel value specified by
pixel will be overwritten when the color is
Second form of the constructor is analogous to
Starting with PyGTK 2.16,
blue parameters can also be floating-point numbers in the
range 0.0--1.0. Either all specified values must be integers or floats -- mixing is
not allowed as this would be too error-prone. Values outside the valid range
0.0--1.0 are clamped, so e.g. 3.14 is the same as 1.0.
Note that internally values are still stored as integers, so values of
*_float attribute will not necessarily be the same
as the value used as argument for the constructor. They will be as close as
permitted by 16-bit color component storage used by
All of the following expressions create a bright green color:
gtk.gdk.Color(0, 65535, 0) gtk.gdk.Color(green=1.0) gtk.gdk.Color('#0f0')
This method is available in PyGTK 2.12 and above.
to_string() method returns a textual
specification of color in the hexadecimal form #rrrrggggbbbb,
where r, g and b are hex digits representing the red,
green and blue components respectively.
Starting with PyGTK 2.14 you can also
str(color) code. However, that can return a
shorter (3, 6 or 12 hexadecimal digits) string if shorter version means
the same for the constructor.
|a string containing a color specification|
gtk.gdk.color_parse() method returns
spec. The format of
spec is a string containing the specification of the
color either as a name (e.g. "navajowhite") as specified in the X11
rgb.txt file or as a hexadecimal string (e.g.
"#FF0078"). The hexadecimal string must start with '#' and must contain 3
sets of hexadecimal digits of the same length (i.e. 1, 2 ,3 or 4 digits).
For example the following specify the same color value: "#F0A", "#FF00AA",
"#FFF000AAA" and "#FFFF0000AAAA". The
This function raise the ValueError (TypeError prior to PyGTK 2.4) exception if unable to parse the color specification
|Hue of the desired color as a float in range 0.0--1.0|
|Saturation of the desired color as a float in range 0.0--1.0|
|Value of the desired color as a float in range 0.0--1.0|
This function is available in PyGTK 2.16 and above.
gtk.gdk.color_from_hsv() method returns
specified by the HSV parameters. All three parameters are mandatory and should be
floats from 0.0 to 1.0. The range requirement, however, is not strict, see
As hue goes from 0 to 1 color goes roughly as red → yellow → green → cyan → blue → magenta → red. Because of the "circular" nature, this parameter wraps around, so only fractional part matters. E.g. -4.2 or 1.8 are the same as 0.8. Saturation determines how intense a color is, with 0.0 meaning pure gray and 1.0 -- fully intense color. Value determines how light a color is, with 0.0 standing for fully black and 1.0 for completely white color. Both saturation and value are clamped to valid range (see rationale at the top of the page).
Note that internal storage is still integers, so values of
value attributes of the returned object will not
necessarily be equal to the value used as argument for this functions. They will
be as close as permitted by 16-bit color component storage used
For more details read about HSV colorspace.