Chapter 31. Color Selection

Inheritance Hierarchy

Object
   +--- Widget
         +--- Container
               +--- Box
                     +--- VBox
                           +--- ColorSelection
         

The color selection widget is, not surprisingly, a widget for interactive selection of colors. This composite widget lets the user select a color by manipulating RGB (Red, Green, Blue) and HSV (Hue, Saturation, Value) triples. This is done either by adjusting single values with sliders or entries, or by picking the desired color from a hue-saturation wheel/value bar. Optionally, the opacity of the color can also be set.

The color selection widget currently emits only one signal, 'color_changed' which is emitted whenever the current color in the widget changes, either when the user changes it or if it's set explicitly through selection_set_color().

Lets have a look at what the color selection widget has to offer us. The widget comes in two flavours: Gtk::ColorSelection (described here) and Gtk::ColorSelectionDialog (described later .)

To create a ColorSelection widget, use:

$color = new Gtk::ColorSelection();

The update policy can be set using:

$color->set_update_policy( $policy );

The default policy is 'continuous' which means that the current color is updated continuously when the user drags the sliders or presses the mouse and drags in the hue-saturation wheel or value bar. If you experience performance problems, you may want to set the policy to 'discontinuous' or 'delayed'.

$color->set_opacity( $use_opacity );

The color selection widget supports adjusting the opacity of a color (also known as translucency or the alpha channel). This is disabled by default. Calling this function with $use_opacity set to a true value enables opacity. Likewise, if $use_opacity is set to a false value it will disable opacity.

$color->set_color( @colors );

You can set the current color explicitly by calling this function with a list of colors. The length of the array depends on whether opacity is enabled or not. Position 0 contains the red component, 1 is green, 2 is blue and opacity is at position 3 (only if opacity is enabled, see set_opacity()). All values are between 0.0 and 1.0.

@color = $color->get_color();

When you need to query the current color, typically when you've received a 'color_changed' signal, you use this function. Returned is a list of colors to fill in. See the set_color() function for the description of this list.