10.12. Color Selection

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 Gtk::ColorSelection#color=.

Lets have a look at what the color selection widget has to offer us. The widget comes in two flavors: Gtk::ColorSelection and Gtk::ColorSelectionDialog.

Gtk::ColorSelection.new

You'll probably not be using this version directly. It creates an orphan ColorSelection widget which you'll have to parent yourself. The ColorSelection widget inherits from the VBox widget.

Gtk::ColorSelectionDialog.new( title )

This is the most common use of creating a color selection. It creates a ColorSelectionDialog. It consists of a Frame containing a ColorSelection widget, an HSeparator and an HBox with three buttons, "Ok", "Cancel" and "Help". You can reach these buttons by accessing the "ok_button", "cancel_button" and "help_button" widgets in the ColorSelectionDialog structure, (i.e., GTK::ColorSelectionDialog#ok_button).

Gtk::ColorSelection#set_has_opacity_control( has_opacity )
Gtk::ColorSelection#has_opacity_control=( has_opacity )

The color selection widget supports adjusting the opacity of a color (also known as the alpha channel). This is disabled by default. Calling this method with has_opacity set to true enables opacity. Likewise, has_opacity set to false will disable opacity.

Gtk::ColorSelection#set_current_color( color )
Gtk::ColorSelection#current_color=( color )

Gtk::ColorSelection#set_current_alpha( alpha )
Gtk::ColorSelection#current_alpha=( alpha )

You can set the current color explicitly by calling Gtk::ColorSelection#set_current_color() with a Gdk::Color Setting the opacity (alpha channel) is done with Gtk::ColorSelection#set_current_alpha(). The alpha value should be between 0 (fully transparent) and 65636 (fully opaque).

Gtk::ColorSelection#current_color

Gtk::ColorSelection#current_alpha

When you need to query the current color, typically when you've received a "color_changed" signal, you use these methods.

Here's a simple example demonstrating the use of the ColorSelectionDialog. The program displays a color selection dialog, and then prints the color you selected.

COLORSELECTION DIALOG SCREENSHOT

ON HOLD TILL I FIGURE OUT THE Gdk::Color* STUFF.

Prev Next