PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 194: How to get color values from gtkcolorbutton?
Written by kksou   
Friday, 09 March 2007
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   
<?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() {
  • Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
  • Registration is free and immediate.
  • Have some doubt about the registration? Please read this forum article.
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

User reviews   Average user ratings:    0.0   (from 2 users)
  1. Johan du Toit from South Africa
    March 08, 2007 5:42am

    I do get the following error:
    "PHP Fatal error: Call to undefined method Gtk::color_selection_palette_to_string() in D:\getcolor.phpw on line 28"

  2. kksou
    March 08, 2007 8:16pm

    Thanks for pointing this out. My version of php-gtk2 does have this method, but I checked some of the other versions, and yes you're right, it is indeed not there.Have changed the example to use GtkColorSelection instead so that we can "dig" inside the GtkColorSelection to get the RGB values direct from the GtkEntry containing the RGB values.

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2008. kksou.com. All Rights Reserved