improved calculation of text color based on the splash image (bug
2006-06-28 Sven Neumann <sven@gimp.org> * app/gui/splash.c: improved calculation of text color based on the splash image (bug #165760).
This commit is contained in:

committed by
Sven Neumann

parent
6c42c4fcc8
commit
59056d8d9a
@ -1,3 +1,8 @@
|
|||||||
|
2006-06-28 Sven Neumann <sven@gimp.org>
|
||||||
|
|
||||||
|
* app/gui/splash.c: improved calculation of text color based on
|
||||||
|
the splash image (bug #165760).
|
||||||
|
|
||||||
2006-06-28 Sven Neumann <sven@gimp.org>
|
2006-06-28 Sven Neumann <sven@gimp.org>
|
||||||
|
|
||||||
* app/menus/plug-in-menus.c (plug_in_menus_setup): generate unique
|
* app/menus/plug-in-menus.c (plug_in_menus_setup): generate unique
|
||||||
|
198
app/gui/splash.c
198
app/gui/splash.c
@ -59,6 +59,10 @@ static GimpSplash *splash = NULL;
|
|||||||
|
|
||||||
|
|
||||||
static void splash_map (void);
|
static void splash_map (void);
|
||||||
|
static void splash_position_layouts (GimpSplash *splash,
|
||||||
|
const gchar *text1,
|
||||||
|
const gchar *text2,
|
||||||
|
GdkRectangle *area);
|
||||||
static gboolean splash_area_expose (GtkWidget *widget,
|
static gboolean splash_area_expose (GtkWidget *widget,
|
||||||
GdkEventExpose *event,
|
GdkEventExpose *event,
|
||||||
GimpSplash *splash);
|
GimpSplash *splash);
|
||||||
@ -66,7 +70,7 @@ static void splash_rectangle_union (GdkRectangle *dest,
|
|||||||
PangoRectangle *pango_rect,
|
PangoRectangle *pango_rect,
|
||||||
gint offset_x,
|
gint offset_x,
|
||||||
gint offset_y);
|
gint offset_y);
|
||||||
static gboolean splash_average_bottom (GtkWidget *widget,
|
static gboolean splash_average_text_area (GimpSplash *splash,
|
||||||
GdkPixbuf *pixbuf,
|
GdkPixbuf *pixbuf,
|
||||||
GdkColor *color);
|
GdkColor *color);
|
||||||
|
|
||||||
@ -155,11 +159,27 @@ splash_create (void)
|
|||||||
|
|
||||||
gtk_widget_set_size_request (splash->area, splash->width, splash->height);
|
gtk_widget_set_size_request (splash->area, splash->width, splash->height);
|
||||||
|
|
||||||
gtk_widget_realize (splash->area);
|
/* create the pango layouts */
|
||||||
|
splash->upper = gtk_widget_create_pango_layout (splash->area, "");
|
||||||
|
splash->lower = gtk_widget_create_pango_layout (splash->area, "");
|
||||||
|
|
||||||
splash_average_bottom (splash->area,
|
attrs = pango_attr_list_new ();
|
||||||
gdk_pixbuf_animation_get_static_image (pixbuf),
|
attr = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
|
||||||
&values.foreground);
|
attr->start_index = 0;
|
||||||
|
attr->end_index = -1;
|
||||||
|
pango_attr_list_insert (attrs, attr);
|
||||||
|
|
||||||
|
pango_layout_set_attributes (splash->upper, attrs);
|
||||||
|
pango_attr_list_unref (attrs);
|
||||||
|
|
||||||
|
/* this sets the initial layout positions */
|
||||||
|
splash_position_layouts (splash, "", "", NULL);
|
||||||
|
|
||||||
|
splash_average_text_area (splash,
|
||||||
|
gdk_pixbuf_animation_get_static_image (pixbuf),
|
||||||
|
&values.foreground);
|
||||||
|
|
||||||
|
gtk_widget_realize (splash->area);
|
||||||
splash->gc = gdk_gc_new_with_values (splash->area->window, &values,
|
splash->gc = gdk_gc_new_with_values (splash->area->window, &values,
|
||||||
GDK_GC_FOREGROUND);
|
GDK_GC_FOREGROUND);
|
||||||
|
|
||||||
@ -182,19 +202,6 @@ splash_create (void)
|
|||||||
G_CALLBACK (splash_area_expose),
|
G_CALLBACK (splash_area_expose),
|
||||||
splash);
|
splash);
|
||||||
|
|
||||||
/* create the pango layouts */
|
|
||||||
splash->upper = gtk_widget_create_pango_layout (splash->area, "");
|
|
||||||
splash->lower = gtk_widget_create_pango_layout (splash->area, "");
|
|
||||||
|
|
||||||
attrs = pango_attr_list_new ();
|
|
||||||
attr = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
|
|
||||||
attr->start_index = 0;
|
|
||||||
attr->end_index = -1;
|
|
||||||
pango_attr_list_insert (attrs, attr);
|
|
||||||
|
|
||||||
pango_layout_set_attributes (splash->upper, attrs);
|
|
||||||
pango_attr_list_unref (attrs);
|
|
||||||
|
|
||||||
/* add a progress bar */
|
/* add a progress bar */
|
||||||
splash->progress = gtk_progress_bar_new ();
|
splash->progress = gtk_progress_bar_new ();
|
||||||
gtk_box_pack_end (GTK_BOX (vbox), splash->progress, FALSE, FALSE, 0);
|
gtk_box_pack_end (GTK_BOX (vbox), splash->progress, FALSE, FALSE, 0);
|
||||||
@ -234,11 +241,7 @@ splash_update (const gchar *text1,
|
|||||||
const gchar *text2,
|
const gchar *text2,
|
||||||
gdouble percentage)
|
gdouble percentage)
|
||||||
{
|
{
|
||||||
GdkRectangle expose = { 0, 0, 0, 0 };
|
GdkRectangle expose = { 0, 0, 0, 0 };
|
||||||
PangoRectangle ink;
|
|
||||||
PangoRectangle logical;
|
|
||||||
gint width;
|
|
||||||
gint height;
|
|
||||||
|
|
||||||
g_return_if_fail (percentage >= 0.0 && percentage <= 1.0);
|
g_return_if_fail (percentage >= 0.0 && percentage <= 1.0);
|
||||||
|
|
||||||
@ -249,36 +252,7 @@ splash_update (const gchar *text1,
|
|||||||
splash_timer_elapsed (text1, text2, percentage);
|
splash_timer_elapsed (text1, text2, percentage);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
width = splash->area->allocation.width;
|
splash_position_layouts (splash, text1, text2, &expose);
|
||||||
height = splash->area->allocation.height;
|
|
||||||
|
|
||||||
if (text1)
|
|
||||||
{
|
|
||||||
pango_layout_get_pixel_extents (splash->upper, &ink, NULL);
|
|
||||||
splash_rectangle_union (&expose, &ink, splash->upper_x, splash->upper_y);
|
|
||||||
|
|
||||||
pango_layout_set_text (splash->upper, text1, -1);
|
|
||||||
pango_layout_get_pixel_extents (splash->upper, &ink, &logical);
|
|
||||||
|
|
||||||
splash->upper_x = (width - logical.width) / 2;
|
|
||||||
splash->upper_y = height - 2 * (logical.height + 6);
|
|
||||||
|
|
||||||
splash_rectangle_union (&expose, &ink, splash->upper_x, splash->upper_y);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (text2)
|
|
||||||
{
|
|
||||||
pango_layout_get_pixel_extents (splash->lower, &ink, NULL);
|
|
||||||
splash_rectangle_union (&expose, &ink, splash->lower_x, splash->lower_y);
|
|
||||||
|
|
||||||
pango_layout_set_text (splash->lower, text2, -1);
|
|
||||||
pango_layout_get_pixel_extents (splash->lower, &ink, &logical);
|
|
||||||
|
|
||||||
splash->lower_x = (width - logical.width) / 2;
|
|
||||||
splash->lower_y = height - (logical.height + 6);
|
|
||||||
|
|
||||||
splash_rectangle_union (&expose, &ink, splash->lower_x, splash->lower_y);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (expose.width > 0 && expose.height > 0)
|
if (expose.width > 0 && expose.height > 0)
|
||||||
gtk_widget_queue_draw_area (splash->area,
|
gtk_widget_queue_draw_area (splash->area,
|
||||||
@ -320,6 +294,51 @@ splash_map (void)
|
|||||||
gtk_window_set_auto_startup_notification (TRUE);
|
gtk_window_set_auto_startup_notification (TRUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* area returns the union of the previous and new ink rectangles */
|
||||||
|
static void
|
||||||
|
splash_position_layouts (GimpSplash *splash,
|
||||||
|
const gchar *text1,
|
||||||
|
const gchar *text2,
|
||||||
|
GdkRectangle *area)
|
||||||
|
{
|
||||||
|
PangoRectangle ink;
|
||||||
|
PangoRectangle logical;
|
||||||
|
|
||||||
|
if (text1)
|
||||||
|
{
|
||||||
|
pango_layout_get_pixel_extents (splash->upper, &ink, NULL);
|
||||||
|
|
||||||
|
if (area)
|
||||||
|
splash_rectangle_union (area, &ink, splash->upper_x, splash->upper_y);
|
||||||
|
|
||||||
|
pango_layout_set_text (splash->upper, text1, -1);
|
||||||
|
pango_layout_get_pixel_extents (splash->upper, &ink, &logical);
|
||||||
|
|
||||||
|
splash->upper_x = (splash->width - logical.width) / 2;
|
||||||
|
splash->upper_y = splash->height - 2 * (logical.height + 6);
|
||||||
|
|
||||||
|
if (area)
|
||||||
|
splash_rectangle_union (area, &ink, splash->upper_x, splash->upper_y);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (text2)
|
||||||
|
{
|
||||||
|
pango_layout_get_pixel_extents (splash->lower, &ink, NULL);
|
||||||
|
|
||||||
|
if (area)
|
||||||
|
splash_rectangle_union (area, &ink, splash->lower_x, splash->lower_y);
|
||||||
|
|
||||||
|
pango_layout_set_text (splash->lower, text2, -1);
|
||||||
|
pango_layout_get_pixel_extents (splash->lower, &ink, &logical);
|
||||||
|
|
||||||
|
splash->lower_x = (splash->width - logical.width) / 2;
|
||||||
|
splash->lower_y = splash->height - (logical.height + 6);
|
||||||
|
|
||||||
|
if (area)
|
||||||
|
splash_rectangle_union (area, &ink, splash->lower_x, splash->lower_y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
splash_rectangle_union (GdkRectangle *dest,
|
splash_rectangle_union (GdkRectangle *dest,
|
||||||
PangoRectangle *pango_rect,
|
PangoRectangle *pango_rect,
|
||||||
@ -340,61 +359,68 @@ splash_rectangle_union (GdkRectangle *dest,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* This function chooses a gray value for the text color, based on
|
/* This function chooses a gray value for the text color, based on
|
||||||
* the average intensity of the lower 60 rows of the splash image.
|
* the average luminance of the text area of the splash image.
|
||||||
*/
|
*/
|
||||||
static gboolean
|
static gboolean
|
||||||
splash_average_bottom (GtkWidget *widget,
|
splash_average_text_area (GimpSplash *splash,
|
||||||
GdkPixbuf *pixbuf,
|
GdkPixbuf *pixbuf,
|
||||||
GdkColor *color)
|
GdkColor *color)
|
||||||
{
|
{
|
||||||
const guchar *pixels;
|
const guchar *pixels;
|
||||||
gint x, y;
|
|
||||||
gint width, height;
|
|
||||||
gint rowstride;
|
gint rowstride;
|
||||||
gint channels;
|
gint channels;
|
||||||
gint luminance;
|
gint luminance = 0;
|
||||||
gint count;
|
guint sum[3] = { 0, 0, 0 };
|
||||||
guint sum[3] = { 0, 0, 0 };
|
GdkRectangle image = { 0, 0, 0, 0 };
|
||||||
|
GdkRectangle area = { 0, 0, 0, 0 };
|
||||||
|
|
||||||
g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), FALSE);
|
g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), FALSE);
|
||||||
g_return_val_if_fail (gdk_pixbuf_get_bits_per_sample (pixbuf) == 8, FALSE);
|
g_return_val_if_fail (gdk_pixbuf_get_bits_per_sample (pixbuf) == 8, FALSE);
|
||||||
|
|
||||||
width = gdk_pixbuf_get_width (pixbuf);
|
image.width = gdk_pixbuf_get_width (pixbuf);
|
||||||
height = gdk_pixbuf_get_height (pixbuf);
|
image.height = gdk_pixbuf_get_height (pixbuf);
|
||||||
rowstride = gdk_pixbuf_get_rowstride (pixbuf);
|
rowstride = gdk_pixbuf_get_rowstride (pixbuf);
|
||||||
channels = gdk_pixbuf_get_n_channels (pixbuf);
|
channels = gdk_pixbuf_get_n_channels (pixbuf);
|
||||||
pixels = gdk_pixbuf_get_pixels (pixbuf);
|
pixels = gdk_pixbuf_get_pixels (pixbuf);
|
||||||
|
|
||||||
y = MAX (0, height - 60);
|
splash_position_layouts (splash, "Short text", "Somewhat longer text", &area);
|
||||||
count = width * (height - y);
|
|
||||||
|
|
||||||
pixels += y * rowstride;
|
if (gdk_rectangle_intersect (&image, &area, &area))
|
||||||
|
|
||||||
for (; y < height; y++)
|
|
||||||
{
|
{
|
||||||
const guchar *src = pixels;
|
const gint count = area.width * area.height;
|
||||||
|
gint x, y;
|
||||||
|
|
||||||
for (x = 0; x < width; x++)
|
pixels += area.x * channels;
|
||||||
|
pixels += area.y * rowstride;
|
||||||
|
|
||||||
|
for (y = 0; y < area.height; y++)
|
||||||
{
|
{
|
||||||
sum[0] += src[0];
|
const guchar *src = pixels;
|
||||||
sum[1] += src[1];
|
|
||||||
sum[2] += src[2];
|
|
||||||
|
|
||||||
src += channels;
|
for (x = 0; x < area.width; x++)
|
||||||
|
{
|
||||||
|
sum[0] += src[0];
|
||||||
|
sum[1] += src[1];
|
||||||
|
sum[2] += src[2];
|
||||||
|
|
||||||
|
src += channels;
|
||||||
|
}
|
||||||
|
|
||||||
|
pixels += rowstride;
|
||||||
}
|
}
|
||||||
|
|
||||||
pixels += rowstride;
|
luminance = GIMP_RGB_LUMINANCE (sum[0] / count,
|
||||||
|
sum[1] / count,
|
||||||
|
sum[2] / count);
|
||||||
|
|
||||||
|
luminance = CLAMP0255 (luminance > 127 ?
|
||||||
|
luminance - 223 : luminance + 223);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
luminance = GIMP_RGB_LUMINANCE (sum[0] / count,
|
|
||||||
sum[1] / count,
|
|
||||||
sum[2] / count);
|
|
||||||
|
|
||||||
luminance = CLAMP0255 (luminance > 127 ? luminance - 223 : luminance + 223);
|
|
||||||
|
|
||||||
color->red = color->green = color->blue = (luminance << 8 | luminance);
|
color->red = color->green = color->blue = (luminance << 8 | luminance);
|
||||||
|
|
||||||
return gdk_colormap_alloc_color (gtk_widget_get_colormap (widget),
|
return gdk_colormap_alloc_color (gtk_widget_get_colormap (splash->area),
|
||||||
color, FALSE, TRUE);
|
color, FALSE, TRUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user