comparison src/gtkconv.c @ 12345:f6fa5d742c76

[gaim-migrate @ 14649] so we were talking in #gaim the other day, and chat nick colors came up. I mentioned that back when sean changed the color code list, that we'd talked about maybe someday having code to generate the list of colors automatically. And datallah found the w3.org algorithm to check a given color code for visibility, so Tak wrote up some code to use that to generate a list of colors. I like it, and nwalp says to go ahead and commit it then. committer: Tailor Script <tailor@pidgin.im>
author Luke Schierer <lschiere@pidgin.im>
date Mon, 05 Dec 2005 05:08:38 +0000
parents 3726ff9022f3
children f81d458aba18
comparison
equal deleted inserted replaced
12344:719c5b4668b9 12345:f6fa5d742c76
74 /* Undef this to turn off "custom-smiley" debug messages */ 74 /* Undef this to turn off "custom-smiley" debug messages */
75 #define DEBUG_CUSTOM_SMILEY 75 #define DEBUG_CUSTOM_SMILEY
76 76
77 #define LUMINANCE(c) (float)((0.3*(c.red))+(0.59*(c.green))+(0.11*(c.blue))) 77 #define LUMINANCE(c) (float)((0.3*(c.red))+(0.59*(c.green))+(0.11*(c.blue)))
78 78
79 #if 0
79 /* These colors come from the default GNOME palette */ 80 /* These colors come from the default GNOME palette */
80 static GdkColor nick_colors[] = { 81 static GdkColor nick_colors[] = {
81 {0, 47616, 46336, 43776}, /* Basic 3D Medium */ 82 {0, 47616, 46336, 43776}, /* Basic 3D Medium */
82 {0, 32768, 32000, 29696}, /* Basic 3D Dark */ 83 {0, 32768, 32000, 29696}, /* Basic 3D Dark */
83 {0, 22016, 20992, 18432}, /* 3D Shadow */ 84 {0, 22016, 20992, 18432}, /* 3D Shadow */
101 {0, 17920, 40960, 17920}, /* Accent Green */ 102 {0, 17920, 40960, 17920}, /* Accent Green */
102 {0, 9728, 50944, 9728} /* Accent Green Dark */ 103 {0, 9728, 50944, 9728} /* Accent Green Dark */
103 }; 104 };
104 105
105 #define NUM_NICK_COLORS (sizeof(nick_colors) / sizeof(*nick_colors)) 106 #define NUM_NICK_COLORS (sizeof(nick_colors) / sizeof(*nick_colors))
107 #endif
108
109 /* From http://www.w3.org/TR/AERT#color-contrast */
110 #define MIN_BRIGHTNESS_CONTRAST 125
111 #define MIN_COLOR_CONTRAST 500
112
113 #define NUM_NICK_COLORS 220
114 static GdkColor *nick_colors = NULL;
106 115
107 typedef struct { 116 typedef struct {
108 GtkWidget *window; 117 GtkWidget *window;
109 118
110 GtkWidget *entry; 119 GtkWidget *entry;
136 static void update_typing_icon(GaimGtkConversation *gtkconv); 145 static void update_typing_icon(GaimGtkConversation *gtkconv);
137 static char *item_factory_translate_func (const char *path, gpointer func_data); 146 static char *item_factory_translate_func (const char *path, gpointer func_data);
138 gboolean gaim_gtkconv_has_focus(GaimConversation *conv); 147 gboolean gaim_gtkconv_has_focus(GaimConversation *conv);
139 static void gaim_gtkconv_custom_smiley_allocated(GdkPixbufLoader *loader, gpointer user_data); 148 static void gaim_gtkconv_custom_smiley_allocated(GdkPixbufLoader *loader, gpointer user_data);
140 static void gaim_gtkconv_custom_smiley_closed(GdkPixbufLoader *loader, gpointer user_data); 149 static void gaim_gtkconv_custom_smiley_closed(GdkPixbufLoader *loader, gpointer user_data);
150 GdkColor* generate_nick_colors(guint numcolors, GdkColor background);
151 gboolean color_is_visible(GdkColor foreground, GdkColor background);
141 152
142 static GdkColor *get_nick_color(GaimGtkConversation *gtkconv, const char *name) { 153 static GdkColor *get_nick_color(GaimGtkConversation *gtkconv, const char *name) {
143 static GdkColor col; 154 static GdkColor col;
144 GtkStyle *style = gtk_widget_get_style(gtkconv->imhtml); 155 GtkStyle *style = gtk_widget_get_style(gtkconv->imhtml);
145 float scale; 156 float scale;
4058 4069
4059 if (hidden) 4070 if (hidden)
4060 gaim_gtk_conv_window_add_gtkconv(hidden_convwin, gtkconv); 4071 gaim_gtk_conv_window_add_gtkconv(hidden_convwin, gtkconv);
4061 else 4072 else
4062 gaim_gtkconv_placement_place(gtkconv); 4073 gaim_gtkconv_placement_place(gtkconv);
4074
4075 if(NULL == nick_colors)
4076 nick_colors = generate_nick_colors(NUM_NICK_COLORS, gtk_widget_get_style(gtkconv->imhtml)->base[GTK_STATE_NORMAL]);
4063 } 4077 }
4064 4078
4065 static void 4079 static void
4066 gaim_gtkconv_new_hidden(GaimConversation *conv) 4080 gaim_gtkconv_new_hidden(GaimConversation *conv)
4067 { 4081 {
7490 { 7504 {
7491 g_return_val_if_fail(gtkconv != NULL, FALSE); 7505 g_return_val_if_fail(gtkconv != NULL, FALSE);
7492 7506
7493 return (gtkconv->win == hidden_convwin); 7507 return (gtkconv->win == hidden_convwin);
7494 } 7508 }
7509
7510
7511 /* Algorithm from http://www.w3.org/TR/AERT#color-contrast */
7512 gboolean
7513 color_is_visible(GdkColor foreground, GdkColor background)
7514 {
7515 gulong fg_brightness,
7516 bg_brightness,
7517 br_diff,
7518 col_diff;
7519
7520 fg_brightness = (foreground.red * 299 + foreground.green * 587 + foreground.blue * 114) / 1000;
7521 bg_brightness = (background.red * 299 + background.green * 587 + background.blue * 114) / 1000;
7522 br_diff = abs(fg_brightness - bg_brightness);
7523
7524 col_diff = abs(foreground.red - background.red) + abs(foreground.green - background.green) + abs(foreground.blue - background.blue);
7525
7526 return ((col_diff > MIN_COLOR_CONTRAST) && (br_diff > MIN_BRIGHTNESS_CONTRAST));
7527 }
7528
7529
7530 GdkColor*
7531 generate_nick_colors(guint numcolors, GdkColor background)
7532 {
7533 guint i;
7534 GdkColor *colors = (GdkColor*)g_malloc(numcolors * sizeof(GdkColor));
7535
7536 srand(background.red + 1 * background.green + 1 * background.blue + 1);
7537
7538 for(i = 0; i < numcolors;){
7539 GdkColor color = { 0, rand()%65536, rand()%65536, rand()%65536 };
7540 if(color_is_visible(color, background)){
7541 colors[i] = color;
7542 i++;
7543 }
7544 }
7545
7546 return colors;
7547 }