194. How to get color values from gtkcolorbutton?

Problem

You have set up a GtkColorButton/GtkColorSelection to allow user to select colors from a color palette. You would like to retrieve the selected color in RGB values as shown below:

How to get color values from gtkcolorbutton?


Solution

  • Set up a dialog with a GtkColorSelection in it.
  • Use the signal color-changed to know when the user has selected a color.
  • Get the selected color with GtkColorselection::get_current_color(). However, note that the value returned is a GdkColor.
  • While gtk+ does have a method Gtk::color_selection_palette_to_string()to convert GdkColor to RGB values, it doesn't seem to exist in php-gtk2. (Thanks to Johan du Toit for pointing this out. Please see reader's comments below.)
  • If you want the selected color in RGB values, we can "dig" inside GtkColorSelection widget to get the RGB values direct from the GtkEntry.

Sample Code

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
29   
30   
31   
32   
33   
34   
35   
36   
37   
38   
39   
40   
41   
42   
43   
44   
45   
46   
47   
48   
49   
50   
51   
52   
53   
54   
56   
57   
58   
59   
60   
61   
62   
63   
64   
65   
66   
69   
70   
71   
72   
73   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 120);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Get Color Values from GtkColorButton");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 40);
$vbox->pack_start($title, 0, 0);
$vbox->pack_start(new GtkLabel(), 0, 0);

$hbox = new GtkHBox();
$vbox->pack_start($hbox, 0, 0);
$hbox->pack_start($button = new GtkButton('Select Color'), 0, 0);
$hbox->pack_start(new GtkLabel(' : '), 0);
$hbox->pack_start($color_button = new GtkButton(), 0, 0);
$color_button->set_size_request(40,20);
$hbox->pack_start(new GtkLabel(' '), 0);
$selected_color_entry = new GtkEntry();
$hbox->pack_start($selected_color_entry, 0);
$button->connect('clicked', 'on_click', $color_button, $selected_color_entry);
$color_button->connect('clicked', 'on_click', $color_button, $selected_color_entry);

$window->show_all();
Gtk::main();

function on_click($button, $color_button, $selected_color_entry) {
    $color = select_color();
    echo "color = $color\n";
    if ($color=='') return;
    $selected_color_entry->set_text($color); // note 4
    $color_button->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse($color));
    $color_button->modify_bg(Gtk::STATE_PRELIGHT, GdkColor::parse($color));
    $color_button->modify_bg(Gtk::STATE_ACTIVE, GdkColor::parse($color));
}

function select_color() {
    $colorbutton = new ColorButton();
    return $colorbutton->selected_color;
}

class ColorButton{

    var $selected_color='';

    function ColorButton() {
        $dialog = new GtkDialog('Alert', null, Gtk::DIALOG_MODAL);
        $this->dialog = $dialog;
        $color_selection = new GtkColorSelection(); // note 1
        $color_selection->connect('color-changed',
            array(&$this, 'on_color_change')); // note 2
        $dialog->vbox->pack_start($color_selection);
        $dialog->add_button(Gtk::STOCK_OK, Gtk::RESPONSE_OK);
        $dialog->show_all();
        $dialog->run();
        $dialog->destroy();
    }

    function on_color_change($widget) {
        $this->selected_color = current(current(next(current( 
            $widget->get_children())->get_children()) 
            ->get_children())->get_children())->get_text(); // note 3
    }

}

?>

Output

As shown above.

 

Explanation

  1. Create the GtkColorSelection.
  2. Register the signal color-changed
  3. Get the selected color direct from the GtkColorSelection widget.
  4. Echo the selected color.

Related Links

Add comment


Security code
Refresh