Commit 7ef3447a authored by David Odin's avatar David Odin

modules/colorsel_triangle.c Replaced the GtkPreviews by GimpPreviewAreas.

* modules/colorsel_triangle.c
* modules/colorsel_water.c: Replaced the GtkPreviews by
  GimpPreviewAreas.
parent c1d5c94b
2004-08-15 DindinX <david@dindinx.org>
* modules/colorsel_triangle.c
* modules/colorsel_water.c: Replaced the GtkPreviews by
GimpPreviewAreas.
2004-08-14 Manish Singh <yosh@gimp.org>
* libgimpbase/gimpprotocol.c (_gp_params_read): make sure array
......
......@@ -23,11 +23,7 @@
#include "config.h"
#include <stdlib.h>
#ifdef __GNUC__
#warning GTK_DISABLE_DEPRECATED
#endif
#undef GTK_DISABLE_DEPRECATED
#include <string.h>
#include <gtk/gtk.h>
......@@ -97,8 +93,7 @@ static void colorsel_xy_to_triangle_buf (gint x,
gint hy);
static GtkWidget *colorsel_triangle_create_preview (ColorselTriangle *triangle);
static void colorsel_triangle_update_previews (ColorselTriangle *triangle,
gboolean hue_changed);
static void colorsel_triangle_update_previews (ColorselTriangle *triangle);
static gboolean colorsel_triangle_event (GtkWidget *widget,
GdkEvent *event,
ColorselTriangle *triangle);
......@@ -146,14 +141,14 @@ colorsel_triangle_get_type (GTypeModule *module)
static const GTypeInfo select_info =
{
sizeof (ColorselTriangleClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) colorsel_triangle_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (ColorselTriangle),
0, /* n_preallocs */
(GInstanceInitFunc) colorsel_triangle_init,
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) colorsel_triangle_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (ColorselTriangle),
0, /* n_preallocs */
(GInstanceInitFunc) colorsel_triangle_init,
};
colorsel_triangle_type =
......@@ -208,7 +203,7 @@ colorsel_triangle_init (ColorselTriangle *triangle)
gtk_container_add (GTK_CONTAINER (frame), triangle->preview);
gtk_widget_show (triangle->preview);
colorsel_triangle_update_previews (triangle, TRUE);
colorsel_triangle_update_previews (triangle);
}
static void
......@@ -223,27 +218,24 @@ colorsel_triangle_finalize (GObject *object)
static void
colorsel_triangle_set_color (GimpColorSelector *selector,
const GimpRGB *rgb,
const GimpHSV *hsv)
const GimpRGB *rgb,
const GimpHSV *hsv)
{
ColorselTriangle *triangle;
triangle = COLORSEL_TRIANGLE (selector);
colorsel_triangle_update_previews (triangle, TRUE);
colorsel_triangle_update_previews (triangle);
}
static GtkWidget *
colorsel_triangle_create_preview (ColorselTriangle *triangle)
{
GtkWidget *preview;
guchar buf[3 * PREVIEWSIZE];
gint i;
preview = gtk_preview_new (GTK_PREVIEW_COLOR);
gtk_preview_set_dither (GTK_PREVIEW (preview), GDK_RGB_DITHER_MAX);
preview = gimp_preview_area_new ();
gtk_widget_set_events (GTK_WIDGET (preview), PREVIEW_MASK );
gtk_preview_size (GTK_PREVIEW (preview), PREVIEWSIZE, PREVIEWSIZE);
gtk_widget_set_size_request (preview, PREVIEWSIZE, PREVIEWSIZE);
g_signal_connect (preview, "motion_notify_event",
G_CALLBACK (colorsel_triangle_event),
......@@ -254,26 +246,27 @@ colorsel_triangle_create_preview (ColorselTriangle *triangle)
g_signal_connect (preview, "button_release_event",
G_CALLBACK (colorsel_triangle_event),
triangle);
for (i=0; i < 3 * PREVIEWSIZE; i += 3)
buf[i] = buf[i+1] = buf[i+2] = BGCOLOR;
for (i=0; i < PREVIEWSIZE; i++)
gtk_preview_draw_row (GTK_PREVIEW (preview), buf, 0, i, PREVIEWSIZE);
g_signal_connect_swapped (preview, "size_allocate",
G_CALLBACK (colorsel_triangle_update_previews),
triangle);
return preview;
}
static void
colorsel_triangle_update_previews (ColorselTriangle *triangle,
gboolean hue_changed)
colorsel_triangle_update_previews (ColorselTriangle *triangle)
{
GimpColorSelector *selector;
guchar buf[3 * PREVIEWSIZE];
guchar *preview_buf;
gint x, y, k, r2, dx, col;
gint x0, y0;
gdouble hue, sat, val, atn;
gint hx,hy, sx,sy, vx,vy;
preview_buf = g_new (guchar, 3 * PREVIEWSIZE * PREVIEWSIZE);
memset (preview_buf, BGCOLOR, 3 * PREVIEWSIZE * PREVIEWSIZE);
selector = GIMP_COLOR_SELECTOR (triangle);
hue = (gdouble) selector->hsv.h * 2 * G_PI;
......@@ -292,116 +285,80 @@ colorsel_triangle_update_previews (ColorselTriangle *triangle,
hue = selector->hsv.h * 360.0;
if (hue_changed)
for (y = COLORWHEELRADIUS; y >= -COLORWHEELRADIUS; y--)
{
for (y = COLORWHEELRADIUS; y >= -COLORWHEELRADIUS; y--)
{
dx = RINT (sqrt (fabs (COLORWHEELRADIUS * COLORWHEELRADIUS - y * y)));
for (x = -dx, k = 0; x <= dx; x++)
{
buf[k] = buf[k+1] = buf[k+2] = BGCOLOR;
r2 = (x * x) + (y * y);
if (r2 <= COLORWHEELRADIUS * COLORWHEELRADIUS)
{
if (r2 > COLORTRIANGLERADIUS * COLORTRIANGLERADIUS)
{
atn = atan2 (x, y);
if (atn < 0)
atn = atn + 2 * G_PI;
gimp_hsv_to_rgb4 (buf + k, atn / (2 * G_PI), 1, 1);
}
else
{
colorsel_xy_to_triangle_buf (x, y, hue, buf + k, hx, hy, sx, sy, vx, vy);
}
}
k += 3;
}
gtk_preview_draw_row (GTK_PREVIEW (triangle->preview), buf,
COLORWHEELRADIUS - dx,
COLORWHEELRADIUS - y, 2 * dx + 1);
}
/* marker in outer ring */
x0 = RINT (sin (hue * G_PI / 180) *
((gdouble) (COLORWHEELRADIUS - COLORTRIANGLERADIUS + 1) / 2 +
COLORTRIANGLERADIUS));
y0 = RINT (cos (hue * G_PI / 180) *
((gdouble) (COLORWHEELRADIUS - COLORTRIANGLERADIUS + 1) / 2 +
COLORTRIANGLERADIUS));
atn = atan2 (x0, y0);
if (atn < 0)
atn = atn + 2 * G_PI;
gimp_hsv_to_rgb4 (buf, atn / (2 * G_PI), 1, 1);
col = GIMP_RGB_INTENSITY (buf[0], buf[1], buf[2]) > 127 ? 0 : 255;
for (y = y0 - 4 ; y <= y0 + 4 ; y++)
{
for (x = x0 - 4, k=0 ; x <= x0 + 4 ; x++)
{
r2 = (x - x0) * (x - x0) + (y - y0) * (y - y0);
if (r2 <= 20 && r2 >= 6)
{
buf[k] = buf[k+1] = buf[k+2] = col;
}
else
{
atn = atan2 (x, y);
if (atn < 0)
atn = atn + 2 * G_PI;
gimp_hsv_to_rgb4 (buf + k, atn / (2 * G_PI), 1, 1);
}
k += 3;
}
gtk_preview_draw_row (GTK_PREVIEW (triangle->preview), buf,
COLORWHEELRADIUS + x0 - 4,
COLORWHEELRADIUS - y, 9);
}
dx = RINT (sqrt (fabs (COLORWHEELRADIUS * COLORWHEELRADIUS - y * y)));
for (x = -dx, k = 0; x <= dx; x++)
{
buf[k] = buf[k+1] = buf[k+2] = BGCOLOR;
r2 = (x * x) + (y * y);
if (r2 <= COLORWHEELRADIUS * COLORWHEELRADIUS)
{
if (r2 > COLORTRIANGLERADIUS * COLORTRIANGLERADIUS)
{
atn = atan2 (x, y);
if (atn < 0)
atn = atn + 2 * G_PI;
gimp_hsv_to_rgb4 (buf + k, atn / (2 * G_PI), 1, 1);
}
else
{
colorsel_xy_to_triangle_buf (x, y, hue, buf + k, hx, hy, sx, sy, vx, vy);
}
}
k += 3;
}
memcpy (preview_buf +
( (COLORWHEELRADIUS - y) * PREVIEWSIZE +
COLORWHEELRADIUS - dx ) * 3,
buf,
3 * (2 * dx + 1));
}
else
/* marker in outer ring */
x0 = RINT (sin (hue * G_PI / 180) *
((gdouble) (COLORWHEELRADIUS - COLORTRIANGLERADIUS + 1) / 2 +
COLORTRIANGLERADIUS));
y0 = RINT (cos (hue * G_PI / 180) *
((gdouble) (COLORWHEELRADIUS - COLORTRIANGLERADIUS + 1) / 2 +
COLORTRIANGLERADIUS));
atn = atan2 (x0, y0);
if (atn < 0)
atn = atn + 2 * G_PI;
gimp_hsv_to_rgb4 (buf, atn / (2 * G_PI), 1, 1);
col = GIMP_RGB_INTENSITY (buf[0], buf[1], buf[2]) > 127 ? 0 : 255;
for (y = y0 - 4 ; y <= y0 + 4 ; y++)
{
/* delete marker in triangle */
sat = triangle->oldsat;
val = triangle->oldval;
x0 = RINT (sx + (vx - sx) * val + (hx - vx) * sat * val);
y0 = RINT (sy + (vy - sy) * val + (hy - vy) * sat * val);
for (y = y0 - 4 ; y <= y0 + 4 ; y++)
{
for (x = x0 - 4, k=0 ; x <= x0 + 4 ; x++)
{
buf[k] = buf[k+1] = buf[k+2] = BGCOLOR;
r2 = (x - x0) * (x - x0) + (y - y0) * (y - y0);
if (x * x + y * y > COLORTRIANGLERADIUS * COLORTRIANGLERADIUS)
{
atn = atan2 (x, y);
if (atn < 0)
atn = atn + 2 * G_PI;
gimp_hsv_to_rgb4 (buf + k, atn / (2 * G_PI), 1, 1);
}
else
{
colorsel_xy_to_triangle_buf (x, y, hue, buf + k, hx, hy, sx, sy, vx, vy);
}
k += 3;
}
gtk_preview_draw_row (GTK_PREVIEW (triangle->preview), buf,
COLORWHEELRADIUS + x0 - 4,
COLORWHEELRADIUS - y, 9);
}
for (x = x0 - 4, k=0 ; x <= x0 + 4 ; x++)
{
r2 = (x - x0) * (x - x0) + (y - y0) * (y - y0);
if (r2 <= 20 && r2 >= 6)
{
buf[k] = buf[k+1] = buf[k+2] = col;
}
else
{
atn = atan2 (x, y);
if (atn < 0)
atn = atn + 2 * G_PI;
gimp_hsv_to_rgb4 (buf + k, atn / (2 * G_PI), 1, 1);
}
k += 3;
}
memcpy (preview_buf +
( (COLORWHEELRADIUS - y) * PREVIEWSIZE +
COLORWHEELRADIUS + x0 - 4) * 3,
buf,
27);
}
/* marker in triangle */
......@@ -417,50 +374,56 @@ colorsel_triangle_update_previews (ColorselTriangle *triangle,
for (y = y0 - 4 ; y <= y0 + 4 ; y++)
{
for (x = x0 - 4, k=0 ; x <= x0 + 4 ; x++)
{
buf[k] = buf[k+1] = buf[k+2] = BGCOLOR;
r2 = (x - x0) * (x - x0) + (y - y0) * (y - y0);
if (r2 <= 20 && r2 >= 6)
{
buf[k] = buf[k+1] = buf[k+2] = col;
}
else
{
if (x * x + y * y > COLORTRIANGLERADIUS * COLORTRIANGLERADIUS)
{
atn = atan2 (x, y);
if (atn < 0)
atn = atn + 2 * G_PI;
gimp_hsv_to_rgb4 (buf + k, atn / (2 * G_PI), 1, 1);
}
else
{
colorsel_xy_to_triangle_buf (x, y, hue, buf + k, hx, hy, sx, sy, vx, vy);
}
}
k += 3;
}
gtk_preview_draw_row (GTK_PREVIEW (triangle->preview), buf,
COLORWHEELRADIUS + x0 - 4,
COLORWHEELRADIUS - y, 9);
{
buf[k] = buf[k+1] = buf[k+2] = BGCOLOR;
r2 = (x - x0) * (x - x0) + (y - y0) * (y - y0);
if (r2 <= 20 && r2 >= 6)
{
buf[k] = buf[k+1] = buf[k+2] = col;
}
else
{
if (x * x + y * y > COLORTRIANGLERADIUS * COLORTRIANGLERADIUS)
{
atn = atan2 (x, y);
if (atn < 0)
atn = atn + 2 * G_PI;
gimp_hsv_to_rgb4 (buf + k, atn / (2 * G_PI), 1, 1);
}
else
{
colorsel_xy_to_triangle_buf (x, y, hue, buf + k, hx, hy, sx, sy, vx, vy);
}
}
k += 3;
}
memcpy (preview_buf +
( (COLORWHEELRADIUS - y) * PREVIEWSIZE +
COLORWHEELRADIUS + x0 - 4) * 3,
buf,
27);
}
gtk_widget_queue_draw (triangle->preview);
gimp_preview_area_draw (GIMP_PREVIEW_AREA (triangle->preview),
0, 0, PREVIEWSIZE, PREVIEWSIZE,
GIMP_RGB_IMAGE,
preview_buf,
3 * PREVIEWSIZE);
g_free (preview_buf);
}
static void
colorsel_xy_to_triangle_buf (gint x,
gint y,
gdouble hue,
gdouble hue,
guchar *buf,
gint hx,
gint hx,
gint hy, /* colored point */
gint sx,
gint sx,
gint sy, /* black point */
gint vx,
gint vx,
gint vy) /* white point */
{
gdouble sat, val;
......@@ -470,24 +433,24 @@ colorsel_xy_to_triangle_buf (gint x,
* I forgot the linear algebra behind it...
*/
val = (gdouble) ( (x - sx) * (hy - vy) - (y - sy) * (hx - vx)) /
(gdouble) ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx));
(gdouble) ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx));
if (val >= 0 && val<= 1)
{
if (abs (hy - vy) < abs (hx - vx))
{
sat = (val == 0 ? 0: ((gdouble) (x - sx - val * (vx - sx)) /
(val * (hx - vx))));
}
{
sat = (val == 0 ? 0: ((gdouble) (x - sx - val * (vx - sx)) /
(val * (hx - vx))));
}
else
{
sat = (val == 0 ? 0: ((gdouble) (y - sy - val * (vy - sy)) /
(val * (hy - vy))));
}
{
sat = (val == 0 ? 0: ((gdouble) (y - sy - val * (vy - sy)) /
(val * (hy - vy))));
}
/* Yes, this ugly 1.00*01 fixes some subtle rounding errors... */
if (sat >= 0 && sat <= 1.000000000000001)
gimp_hsv_to_rgb4 (buf, hue / 360, sat, val);
gimp_hsv_to_rgb4 (buf, hue / 360, sat, val);
}
}
......@@ -552,11 +515,11 @@ colorsel_triangle_event (GtkWidget *widget,
if (triangle->mode == 1 ||
(r > COLORWHEELRADIUS &&
(abs (angle - selector->hsv.h * 360.0) < 30 ||
abs (abs (angle - selector->hsv.h * 360.0) - 360) < 30)))
abs (abs (angle - selector->hsv.h * 360.0) - 360) < 30)))
{
selector->hsv.h = angle / 360.0;
gimp_hsv_to_rgb (&selector->hsv, &selector->rgb);
colorsel_triangle_update_previews (triangle, TRUE);
colorsel_triangle_update_previews (triangle);
}
else
{
......@@ -570,64 +533,64 @@ colorsel_triangle_event (GtkWidget *widget,
hue = selector->hsv.h * 360.0;
if ((x - sx) * vx + (y - sy) * vy < 0)
{
sat = 1;
val = ((gdouble) ( (x - sx) * (hx - sx) + (y - sy) * (hy - sy)))
/ ((hx - sx) * (hx - sx) + (hy - sy) * (hy - sy));
if (val < 0)
val = 0;
else if (val > 1)
val = 1;
}
{
sat = 1;
val = ((gdouble) ( (x - sx) * (hx - sx) + (y - sy) * (hy - sy)))
/ ((hx - sx) * (hx - sx) + (hy - sy) * (hy - sy));
if (val < 0)
val = 0;
else if (val > 1)
val = 1;
}
else if ((x - sx) * hx + (y - sy) * hy < 0)
{
sat = 0;
val = ((gdouble) ( (x - sx) * (vx - sx) + (y - sy) * (vy - sy)))
/ ((vx - sx) * (vx - sx) + (vy - sy) * (vy - sy));
if (val < 0)
val = 0;
else if (val > 1)
val = 1;
}
{
sat = 0;
val = ((gdouble) ( (x - sx) * (vx - sx) + (y - sy) * (vy - sy)))
/ ((vx - sx) * (vx - sx) + (vy - sy) * (vy - sy));
if (val < 0)
val = 0;
else if (val > 1)
val = 1;
}
else if ((x - hx) * sx + (y - hy) * sy < 0)
{
val = 1;
sat = ((gdouble) ( (x - vx) * (hx - vx) + (y - vy) * (hy - vy)))
/ ((hx - vx) * (hx - vx) + (hy - vy) * (hy - vy));
if (sat < 0)
sat = 0;
else if (sat > 1)
sat = 1;
}
{
val = 1;
sat = ((gdouble) ( (x - vx) * (hx - vx) + (y - vy) * (hy - vy)))
/ ((hx - vx) * (hx - vx) + (hy - vy) * (hy - vy));
if (sat < 0)
sat = 0;
else if (sat > 1)
sat = 1;
}
else
{
val = (gdouble) ( (x - sx) * (hy - vy) - (y - sy) * (hx - vx))
/ (gdouble) ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx));
if (val <= 0)
{
val = 0;
sat = 0;
}
else
{
if (val > 1)
val = 1;
if (hy == vy)
sat = (gdouble) (x - sx - val * (vx - sx)) / (val * (gdouble) (hx - vx));
else
sat = (gdouble) (y - sy - val * (vy - sy)) / (val * (gdouble) (hy - vy));
if (sat < 0)
sat = 0;
else if (sat > 1)
sat = 1;
}
}
{
val = (gdouble) ( (x - sx) * (hy - vy) - (y - sy) * (hx - vx))
/ (gdouble) ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx));
if (val <= 0)
{
val = 0;
sat = 0;
}
else
{
if (val > 1)
val = 1;
if (hy == vy)
sat = (gdouble) (x - sx - val * (vx - sx)) / (val * (gdouble) (hx - vx));
else
sat = (gdouble) (y - sy - val * (vy - sy)) / (val * (gdouble) (hy - vy));
if (sat < 0)
sat = 0;
else if (sat > 1)
sat = 1;
}
}
selector->hsv.s = sat;
selector->hsv.v = val;
gimp_hsv_to_rgb (&selector->hsv, &selector->rgb);
colorsel_triangle_update_previews (triangle, FALSE);
colorsel_triangle_update_previews (triangle);
}
/* callback the user */
......
......@@ -22,11 +22,6 @@
#include <stdlib.h>
#include <stdio.h>
#ifdef __GNUC__
#warning GTK_DISABLE_DEPRECATED
#endif
#undef GTK_DISABLE_DEPRECATED
#include <gtk/gtk.h>
#include "libgimpcolor/gimpcolor.h"
......@@ -140,14 +135,14 @@ colorsel_water_get_type (GTypeModule *module)
static const GTypeInfo select_info =
{
sizeof (ColorselWaterClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) colorsel_water_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (ColorselWater),
0, /* n_preallocs */
(GInstanceInitFunc) colorsel_water_init,
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) colorsel_water_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (ColorselWater),
0, /* n_preallocs */
(GInstanceInitFunc) colorsel_water_init,
};
colorsel_water_type =
......@@ -206,10 +201,11 @@ colorsel_water_init (ColorselWater *water)
event_box = gtk_event_box_new ();
gtk_container_add (GTK_CONTAINER (frame), event_box);
preview = gtk_preview_new (GTK_PREVIEW_COLOR);
gtk_preview_size (GTK_PREVIEW (preview), IMAGE_SIZE, IMAGE_SIZE);
preview = gimp_preview_area_new ();
gtk_widget_set_size_request (preview, IMAGE_SIZE, IMAGE_SIZE);
gtk_container_add (GTK_CONTAINER (event_box), preview);
select_area_draw (preview);
g_signal_connect (preview, "size-allocate",
G_CALLBACK (select_area_draw), NULL);
/* Event signals */
g_signal_connect (event_box, "motion_notify_event",
......@@ -226,13 +222,13 @@ colorsel_water_init (ColorselWater *water)
water);
gtk_widget_set_events (event_box,
GDK_EXPOSURE_MASK |
GDK_LEAVE_NOTIFY_MASK |
GDK_BUTTON_PRESS_MASK |
GDK_KEY_PRESS_MASK |
GDK_POINTER_MOTION_MASK |
GDK_POINTER_MOTION_HINT_MASK |
GDK_PROXIMITY_OUT_MASK);
GDK_EXPOSURE_MASK |
GDK_LEAVE_NOTIFY_MASK |
GDK_BUTTON_PRESS_MASK |
GDK_KEY_PRESS_MASK |
GDK_POINTER_MOTION_MASK |
GDK_POINTER_MOTION_HINT_MASK |
GDK_PROXIMITY_OUT_MASK);
/* The following call enables tracking and processing of extension
* events for the drawing area
......@@ -241,7 +237,7 @@ colorsel_water_init (ColorselWater *water)
gtk_widget_grab_focus (event_box);
adj = gtk_adjustment_new (200.0 - water->pressure_adjust * 100.0,
0.0, 200.0, 1.0, 1.0, 0.0);
0.0, 200.0, 1.0, 1.0, 0.0);
g_signal_connect (adj, "value_changed",
G_CALLBACK (pressure_adjust_update),
water);
......@@ -291,7 +287,7 @@ calc (gdouble x,
static void
select_area_draw (GtkWidget *preview)
{
guchar buf[3 * IMAGE_SIZE];
guchar buf[3 * IMAGE_SIZE * IMAGE_SIZE];
gint x, y;
gdouble r, g, b;
gdouble dr, dg, db;
......@@ -307,25 +303,28 @@ select_area_draw (GtkWidget *preview)
db = calc (1, y, 240) - b;
for (x = 0; x < IMAGE_SIZE; x++)
{
buf[x * 3] = CLAMP ((gint) r, 0, 255);