1 /**********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 ***********************************************************************/
15 #include <fc_config.h>
31 /*************************************************************
32 Get display color type of default visual
33 *************************************************************/
34 enum Display_color_type
get_visual(void)
38 gtk_widget_push_colormap (gdk_rgb_get_colormap());
40 visual
= gtk_widget_get_default_visual();
42 if (visual
->type
== GDK_VISUAL_STATIC_GRAY
) {
43 /* StaticGray, use black and white */
44 log_verbose("found B/W display.");
48 if(visual
->type
< GDK_VISUAL_STATIC_COLOR
) {
49 /* No color visual available at default depth */
50 log_verbose("found grayscale(?) display.");
51 return GRAYSCALE_DISPLAY
;
54 log_verbose("color system booted ok.");
59 /****************************************************************************
60 Allocate a color (adjusting it for our colormap if necessary on paletted
61 systems) and return a pointer to it.
62 ****************************************************************************/
63 struct color
*color_alloc(int r
, int g
, int b
)
65 struct color
*color
= fc_malloc(sizeof(*color
));
66 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
68 color
->color
.red
= (r
<< 8) + r
;
69 color
->color
.green
= (g
<< 8) + g
;
70 color
->color
.blue
= (b
<< 8) + b
;
71 gdk_rgb_find_color(cmap
, &color
->color
);
76 /****************************************************************************
77 Free a previously allocated color. See color_alloc.
78 ****************************************************************************/
79 void color_free(struct color
*color
)
84 /****************************************************************************
85 Return a number indicating the perceptual brightness of this color
86 relative to others (larger is brighter).
87 ****************************************************************************/
88 int color_brightness_score(struct color
*pcolor
)
90 struct rgbcolor
*prgb
= rgbcolor_new(pcolor
->color
.red
>> 8,
91 pcolor
->color
.green
>> 8,
92 pcolor
->color
.blue
>> 8);
93 int score
= rgbcolor_brightness_score(prgb
);
95 rgbcolor_destroy(prgb
);
99 /****************************************************************************
100 Fill the string with the color in "#rrggbb" mode. Use it instead of
101 gdk_color() which have been included in gtk2.12 version only.
102 ****************************************************************************/
103 size_t color_to_string(GdkColor
*color
, char *string
, size_t length
)
105 fc_assert_ret_val(NULL
!= string
, 0);
106 fc_assert_ret_val(0 < length
, 0);
112 return fc_snprintf(string
, length
, "#%02x%02x%02x",
113 color
->red
>> 8, color
->green
>> 8, color
->blue
>> 8);