In the following code, I want the background colour of the main GTK_WINDOW_TOPLEVEL to be 0xc0deed. But when I run it is appearing black. I even tried gtk_drawing_area_new and adding it to the main window. But still it is appearing black although I could get other colours like red, blue, white etc
#include <gtk/gtk.h>
int main( int argc, char *argv[])
{
GtkWidget *p_s_window = NULL;
GdkColor color;
color.red = 0x00C0;
color.green = 0x00DE;
color.blue = 0x00ED;
gtk_init(&argc, &argv);
p_s_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(p_s_window), GTK_WIN_POS_CENTER);
gtk_window_set_title(GTK_WINDOW(p_s_window), "hello");
gtk_widget_modify_bg(p_s_window, GTK_STATE_NORMAL, &color);
g_signal_connect_swapped(G_OBJECT(p_s_window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(p_s_window);
gtk_main();
return 0;
}
The GdkColor components are 16-bit, thus having a range of 0 through 65535. Multiply your values with 65535/255 and you'll be alright.
For example yellow would be:
color.red = 0xffff;
color.green = 0xffff;
color.blue = 0;
Although the question is fairly old, I would like to provide another answer that doesn't require calculation.
You can use gdk_color_parse()
to parse the string representation of your color. As mentioned in the documentation, this works on various formats:
The string can either [sic!] one of a large set of standard names (taken from the X11 rgb.txt file), or it can be a hexadecimal value in the form “#rgb” “#rrggbb”, “#rrrgggbbb” or “#rrrrggggbbbb” where “r”, “g” and “b” are hex digits of the red, green, and blue components of the color, respectively.
So in your case this would simply be:
GdkColor color;
if (gdk_color_parse("#c0deed", &color)) {
gtk_widget_modify_bg(p_s_window, GTK_STATE_NORMAL, &color);
} else {
// set default color
}
Please also note that as of Gtk 3.0, gtk_widget_modify_bg()
is deprecated. Use gtk_widget_override_background_color()
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With