diff options
author | Benjamin Berg <benjamin@sipsolutions.net> | 2007-11-02 00:42:14 (GMT) |
---|---|---|
committer | Benjamin Berg <benjamin@sipsolutions.net> | 2007-11-02 00:42:14 (GMT) |
commit | c1ca9c8c56912825b415e537d1cccdf27c5e56f7 (patch) | |
tree | b2fcc68717aa696ff561faa871004d37f156ab2b /gtk | |
parent | 011d24bc4d15fe6b963a591755291a79057e01de (diff) |
Implement the (second) suggested effect for insensitive icons.
Diffstat (limited to 'gtk')
-rw-r--r-- | gtk/engine/Makefile.am | 2 | ||||
-rw-r--r-- | gtk/engine/sugar-utils.c | 106 | ||||
-rw-r--r-- | gtk/engine/sugar-utils.h | 24 |
3 files changed, 132 insertions, 0 deletions
diff --git a/gtk/engine/Makefile.am b/gtk/engine/Makefile.am index 34bd007..eb720ff 100644 --- a/gtk/engine/Makefile.am +++ b/gtk/engine/Makefile.am @@ -15,6 +15,8 @@ libsugar_la_SOURCES = \ sugar-style.c \ sugar-info.h \ sugar-info.c \ + sugar-utils.h \ + sugar-utils.c \ sugar-drawing.h \ sugar-drawing.c diff --git a/gtk/engine/sugar-utils.c b/gtk/engine/sugar-utils.c new file mode 100644 index 0000000..776b1ef --- /dev/null +++ b/gtk/engine/sugar-utils.c @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2007, Benjamin Berg + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "sugar-utils.h" + +GdkPixbuf* +sugar_pixbuf_scale_or_ref (GdkPixbuf *pixbuf, + gint width, + gint height) +{ + gint pixbuf_width = gdk_pixbuf_get_width (pixbuf); + gint pixbuf_height = gdk_pixbuf_get_height (pixbuf); + + if ((pixbuf_width != width) || (pixbuf_height != height)) { + return gdk_pixbuf_scale_simple (pixbuf, width, height, GDK_INTERP_BILINEAR); + } else { + g_object_ref (pixbuf); + return pixbuf; + } +} + +#define RED 0 +#define GREEN 1 +#define BLUE 2 +#define ALPHA 3 + +GdkPixbuf* +sugar_get_insensitive_icon (GdkPixbuf *icon, + guint range, + guint base) +{ + GdkPixbuf *result = gdk_pixbuf_copy (icon); + guint x, y, rowstride, height, width; + guint n_channels; + guint8 *pixel, *pixels; + guint min_color_value = G_MAXUINT8; + guint max_color_value = 0; + guint mult; + gboolean has_alpha; + + width = gdk_pixbuf_get_width (result); + height = gdk_pixbuf_get_height (result); + rowstride = gdk_pixbuf_get_rowstride (result); + n_channels = gdk_pixbuf_get_n_channels (result); + pixels = gdk_pixbuf_get_pixels (result); + + for (y = 0; y < height; y++) { + pixel = pixels + y*rowstride; + for (x = 0; x < width; x++) { + if (n_channels == 4 && pixel[ALPHA] == 0) { + pixel += n_channels; + continue; + } + + min_color_value = MIN (min_color_value, pixel[RED]); + max_color_value = MAX (max_color_value, pixel[RED]); + + min_color_value = MIN (min_color_value, pixel[GREEN]); + max_color_value = MAX (max_color_value, pixel[GREEN]); + + min_color_value = MIN (min_color_value, pixel[BLUE]); + max_color_value = MAX (max_color_value, pixel[BLUE]); + + pixel += n_channels; + } + } + + /* Shift the base value, to whatever we really need to adjust to. */ + base = base - range / 2; + mult = (range << 8) / (max_color_value - min_color_value); + + for (y = 0; y < height; y++) { + pixel = pixels + y*rowstride; + for (x = 0; x < width; x++) { + pixel[RED] = ((((guint) (pixel[RED] - min_color_value)) * mult) >> 8) + base; + pixel[GREEN] = ((((guint) (pixel[GREEN] - min_color_value)) * mult) >> 8) + base; + pixel[BLUE] = ((((guint) (pixel[BLUE] - min_color_value)) * mult) >> 8) + base; + + pixel += n_channels; + } + } + + return result; +} + +#undef RED +#undef GREEN +#undef BLUE +#undef ALPHA + diff --git a/gtk/engine/sugar-utils.h b/gtk/engine/sugar-utils.h new file mode 100644 index 0000000..d80a389 --- /dev/null +++ b/gtk/engine/sugar-utils.h @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2007, Benjamin Berg + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include <gdk/gdk.h> + +G_GNUC_INTERNAL GdkPixbuf* sugar_pixbuf_scale_or_ref (GdkPixbuf *pixbuf, gint width, gint height); +G_GNUC_INTERNAL GdkPixbuf* sugar_get_insensitive_icon (GdkPixbuf *icon, guint range, guint base); + |