2006-12-10 05:33:38 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
2004-02-20 03:56:04 +08:00
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
2004-02-20 03:56:04 +08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-18 06:28:01 +08:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2004-02-20 03:56:04 +08:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2009-01-18 06:28:01 +08:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2004-02-20 03:56:04 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2007-03-09 21:00:01 +08:00
|
|
|
#include "libgimpmath/gimpmath.h"
|
2004-02-20 03:56:04 +08:00
|
|
|
#include "libgimpcolor/gimpcolor.h"
|
2007-03-09 21:00:01 +08:00
|
|
|
#include "libgimpconfig/gimpconfig.h"
|
2004-02-20 03:56:04 +08:00
|
|
|
|
|
|
|
#include "widgets-types.h"
|
|
|
|
|
|
|
|
#include "gimpcolorbar.h"
|
|
|
|
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_ORIENTATION,
|
|
|
|
PROP_COLOR,
|
|
|
|
PROP_CHANNEL
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* local function prototypes */
|
|
|
|
|
2005-12-20 06:37:49 +08:00
|
|
|
static void gimp_color_bar_set_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec);
|
2007-11-20 07:39:56 +08:00
|
|
|
static void gimp_color_bar_get_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec);
|
2004-02-20 03:56:04 +08:00
|
|
|
|
2005-12-20 06:37:49 +08:00
|
|
|
static gboolean gimp_color_bar_expose (GtkWidget *widget,
|
|
|
|
GdkEventExpose *event);
|
2004-02-20 03:56:04 +08:00
|
|
|
|
|
|
|
|
2007-11-23 01:07:33 +08:00
|
|
|
G_DEFINE_TYPE (GimpColorBar, gimp_color_bar, GTK_TYPE_EVENT_BOX)
|
2005-12-20 06:37:49 +08:00
|
|
|
|
|
|
|
#define parent_class gimp_color_bar_parent_class
|
|
|
|
|
2004-02-20 03:56:04 +08:00
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_color_bar_class_init (GimpColorBarClass *klass)
|
|
|
|
{
|
2007-11-20 07:39:56 +08:00
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
|
|
|
|
GimpRGB white = { 1.0, 1.0, 1.0, 1.0 };
|
2004-02-20 03:56:04 +08:00
|
|
|
|
|
|
|
object_class->set_property = gimp_color_bar_set_property;
|
2007-11-20 07:39:56 +08:00
|
|
|
object_class->get_property = gimp_color_bar_get_property;
|
|
|
|
|
|
|
|
widget_class->expose_event = gimp_color_bar_expose;
|
2004-02-20 03:56:04 +08:00
|
|
|
|
|
|
|
g_object_class_install_property (object_class, PROP_ORIENTATION,
|
|
|
|
g_param_spec_enum ("orientation",
|
|
|
|
NULL, NULL,
|
|
|
|
GTK_TYPE_ORIENTATION,
|
|
|
|
GTK_ORIENTATION_HORIZONTAL,
|
2007-11-20 07:39:56 +08:00
|
|
|
GIMP_PARAM_READWRITE |
|
2004-02-20 03:56:04 +08:00
|
|
|
G_PARAM_CONSTRUCT_ONLY));
|
2007-11-20 07:39:56 +08:00
|
|
|
|
2004-02-20 03:56:04 +08:00
|
|
|
g_object_class_install_property (object_class, PROP_COLOR,
|
2004-07-27 03:56:47 +08:00
|
|
|
gimp_param_spec_rgb ("color",
|
|
|
|
NULL, NULL,
|
2006-04-27 23:19:59 +08:00
|
|
|
FALSE, &white,
|
2006-01-19 04:29:40 +08:00
|
|
|
GIMP_PARAM_WRITABLE |
|
2004-07-27 03:56:47 +08:00
|
|
|
G_PARAM_CONSTRUCT));
|
2007-11-20 07:39:56 +08:00
|
|
|
|
2008-01-22 00:18:51 +08:00
|
|
|
g_object_class_install_property (object_class, PROP_CHANNEL,
|
2004-02-21 20:25:09 +08:00
|
|
|
g_param_spec_enum ("histogram-channel",
|
2004-02-20 03:56:04 +08:00
|
|
|
NULL, NULL,
|
|
|
|
GIMP_TYPE_HISTOGRAM_CHANNEL,
|
|
|
|
GIMP_HISTOGRAM_VALUE,
|
2006-01-19 04:29:40 +08:00
|
|
|
GIMP_PARAM_WRITABLE));
|
2004-02-20 03:56:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_color_bar_init (GimpColorBar *bar)
|
|
|
|
{
|
2007-11-23 01:07:33 +08:00
|
|
|
gtk_event_box_set_visible_window (GTK_EVENT_BOX (bar), FALSE);
|
2004-02-20 03:56:04 +08:00
|
|
|
|
2007-11-20 07:39:56 +08:00
|
|
|
bar->orientation = GTK_ORIENTATION_HORIZONTAL;
|
2004-02-20 03:56:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_color_bar_set_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GimpColorBar *bar = GIMP_COLOR_BAR (object);
|
|
|
|
|
|
|
|
switch (property_id)
|
|
|
|
{
|
|
|
|
case PROP_ORIENTATION:
|
|
|
|
bar->orientation = g_value_get_enum (value);
|
|
|
|
break;
|
|
|
|
case PROP_COLOR:
|
|
|
|
gimp_color_bar_set_color (bar, g_value_get_boxed (value));
|
|
|
|
break;
|
|
|
|
case PROP_CHANNEL:
|
|
|
|
gimp_color_bar_set_channel (bar, g_value_get_enum (value));
|
|
|
|
break;
|
2007-11-20 07:39:56 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_color_bar_get_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GimpColorBar *bar = GIMP_COLOR_BAR (object);
|
|
|
|
|
|
|
|
switch (property_id)
|
|
|
|
{
|
|
|
|
case PROP_ORIENTATION:
|
|
|
|
g_value_set_enum (value, bar->orientation);
|
|
|
|
break;
|
|
|
|
|
2004-02-20 03:56:04 +08:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gimp_color_bar_expose (GtkWidget *widget,
|
|
|
|
GdkEventExpose *event)
|
|
|
|
{
|
2008-06-28 23:50:27 +08:00
|
|
|
GimpColorBar *bar = GIMP_COLOR_BAR (widget);
|
|
|
|
GtkStyle *style = gtk_widget_get_style (widget);
|
2004-02-20 03:56:04 +08:00
|
|
|
guchar *buf;
|
|
|
|
guchar *b;
|
|
|
|
gint x, y;
|
|
|
|
gint width, height;
|
|
|
|
gint i, j;
|
|
|
|
|
2008-06-28 23:50:27 +08:00
|
|
|
x = y = gtk_container_get_border_width (GTK_CONTAINER (bar));
|
2004-02-20 03:56:04 +08:00
|
|
|
|
|
|
|
width = widget->allocation.width - 2 * x;
|
|
|
|
height = widget->allocation.height - 2 * y;
|
|
|
|
|
|
|
|
if (width < 1 || height < 1)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
buf = g_alloca (width * height * 3);
|
|
|
|
|
|
|
|
switch (bar->orientation)
|
|
|
|
{
|
|
|
|
case GTK_ORIENTATION_HORIZONTAL:
|
|
|
|
for (i = 0, b = buf; i < width; i++, b += 3)
|
|
|
|
{
|
2007-11-20 17:14:05 +08:00
|
|
|
const guchar *src = bar->buf + 3 * ((i * 256) / width);
|
2004-02-20 03:56:04 +08:00
|
|
|
|
|
|
|
b[0] = src[0];
|
|
|
|
b[1] = src[1];
|
|
|
|
b[2] = src[2];
|
|
|
|
}
|
|
|
|
|
2004-06-13 04:26:16 +08:00
|
|
|
for (i = 1; i < height; i++)
|
2004-02-20 03:56:04 +08:00
|
|
|
memcpy (buf + i * width * 3, buf, width * 3);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GTK_ORIENTATION_VERTICAL:
|
|
|
|
for (i = 0, b = buf; i < height; i++, b += 3 * width)
|
|
|
|
{
|
2007-11-20 17:14:05 +08:00
|
|
|
const guchar *src = bar->buf + 3 * (255 - ((i * 256) / height));
|
|
|
|
guchar *dest = b;
|
2004-02-20 03:56:04 +08:00
|
|
|
|
|
|
|
for (j = 0; j < width; j++, dest += 3)
|
|
|
|
{
|
|
|
|
dest[0] = src[0];
|
|
|
|
dest[1] = src[1];
|
|
|
|
dest[2] = src[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-03-23 00:35:53 +08:00
|
|
|
gdk_draw_rgb_image (gtk_widget_get_window (widget), style->black_gc,
|
2004-02-20 03:56:04 +08:00
|
|
|
widget->allocation.x + x, widget->allocation.y + y,
|
|
|
|
width, height,
|
|
|
|
GDK_RGB_DITHER_NORMAL,
|
|
|
|
buf, 3 * width);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2007-11-20 07:39:56 +08:00
|
|
|
|
|
|
|
/* public functions */
|
|
|
|
|
2004-02-20 03:56:04 +08:00
|
|
|
/**
|
|
|
|
* gimp_color_bar_new:
|
2004-02-20 04:26:51 +08:00
|
|
|
* @orientation: whether the bar should be oriented horizontally or
|
|
|
|
* vertically
|
2004-02-20 03:56:04 +08:00
|
|
|
*
|
|
|
|
* Creates a new #GimpColorBar widget.
|
|
|
|
*
|
|
|
|
* Return value: The new #GimpColorBar widget.
|
|
|
|
**/
|
|
|
|
GtkWidget *
|
|
|
|
gimp_color_bar_new (GtkOrientation orientation)
|
|
|
|
{
|
|
|
|
return g_object_new (GIMP_TYPE_COLOR_BAR,
|
|
|
|
"orientation", orientation,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_color_bar_set_color:
|
|
|
|
* @bar: a #GimpColorBar widget
|
|
|
|
* @color: a #GimpRGB color
|
|
|
|
*
|
|
|
|
* Makes the @bar display a gradient from black (on the left or the
|
|
|
|
* bottom), to the given @color (on the right or at the top).
|
|
|
|
**/
|
|
|
|
void
|
|
|
|
gimp_color_bar_set_color (GimpColorBar *bar,
|
|
|
|
const GimpRGB *color)
|
|
|
|
{
|
|
|
|
guchar *buf;
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
g_return_if_fail (GIMP_IS_COLOR_BAR (bar));
|
|
|
|
g_return_if_fail (color != NULL);
|
|
|
|
|
|
|
|
for (i = 0, buf = bar->buf; i < 256; i++, buf += 3)
|
|
|
|
{
|
|
|
|
buf[0] = ROUND (color->r * (gdouble) i);
|
|
|
|
buf[1] = ROUND (color->g * (gdouble) i);
|
|
|
|
buf[2] = ROUND (color->b * (gdouble) i);
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_widget_queue_draw (GTK_WIDGET (bar));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2004-02-20 04:26:51 +08:00
|
|
|
* gimp_color_bar_set_channel:
|
2004-02-20 03:56:04 +08:00
|
|
|
* @bar: a #GimpColorBar widget
|
|
|
|
* @channel: a #GimpHistogramChannel
|
|
|
|
*
|
|
|
|
* Convenience function that calls gimp_color_bar_set_color() with the
|
|
|
|
* color that matches the @channel.
|
|
|
|
**/
|
|
|
|
void
|
|
|
|
gimp_color_bar_set_channel (GimpColorBar *bar,
|
|
|
|
GimpHistogramChannel channel)
|
|
|
|
{
|
|
|
|
GimpRGB color = { 1.0, 1.0, 1.0, 1.0 };
|
|
|
|
|
|
|
|
g_return_if_fail (GIMP_IS_COLOR_BAR (bar));
|
|
|
|
|
|
|
|
switch (channel)
|
|
|
|
{
|
|
|
|
case GIMP_HISTOGRAM_VALUE:
|
|
|
|
case GIMP_HISTOGRAM_ALPHA:
|
2004-07-07 00:33:30 +08:00
|
|
|
case GIMP_HISTOGRAM_RGB:
|
2004-02-20 03:56:04 +08:00
|
|
|
gimp_rgb_set (&color, 1.0, 1.0, 1.0);
|
|
|
|
break;
|
|
|
|
case GIMP_HISTOGRAM_RED:
|
|
|
|
gimp_rgb_set (&color, 1.0, 0.0, 0.0);
|
|
|
|
break;
|
|
|
|
case GIMP_HISTOGRAM_GREEN:
|
|
|
|
gimp_rgb_set (&color, 0.0, 1.0, 0.0);
|
|
|
|
break;
|
|
|
|
case GIMP_HISTOGRAM_BLUE:
|
|
|
|
gimp_rgb_set (&color, 0.0, 0.0, 1.0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
gimp_color_bar_set_color (bar, &color);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_color_bar_set_buffers:
|
|
|
|
* @bar: a #GimpColorBar widget
|
|
|
|
* @red: an array of 256 values
|
|
|
|
* @green: an array of 256 values
|
|
|
|
* @blue: an array of 256 values
|
|
|
|
*
|
|
|
|
* This function gives full control over the colors displayed by the
|
|
|
|
* @bar widget. The 3 arrays can for example be taken from a #Levels
|
|
|
|
* or a #Curves struct.
|
|
|
|
**/
|
|
|
|
void
|
|
|
|
gimp_color_bar_set_buffers (GimpColorBar *bar,
|
|
|
|
const guchar *red,
|
|
|
|
const guchar *green,
|
|
|
|
const guchar *blue)
|
|
|
|
{
|
|
|
|
guchar *buf;
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
g_return_if_fail (GIMP_IS_COLOR_BAR (bar));
|
|
|
|
g_return_if_fail (red != NULL);
|
|
|
|
g_return_if_fail (green != NULL);
|
|
|
|
g_return_if_fail (blue != NULL);
|
|
|
|
|
|
|
|
for (i = 0, buf = bar->buf; i < 256; i++, buf += 3)
|
|
|
|
{
|
|
|
|
buf[0] = red[i];
|
|
|
|
buf[1] = green[i];
|
|
|
|
buf[2] = blue[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_widget_queue_draw (GTK_WIDGET (bar));
|
|
|
|
}
|