gimp/modules/colorsel_triangle.c

625 lines
18 KiB
C
Raw Normal View History

/*
* colorsel_triangle module (C) 1999 Simon Budig <Simon.Budig@unix-ag.org>
* http://www.home.unix-ag.org/simon/gimp/colorsel.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (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
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Ported to loadable colour selector interface by Austin Donnelly
* <austin@gimp.org>
*/
#include "config.h"
configure.in po-plug-ins/POTFILES.in plug-ins/common/Makefile.am 2000-01-25 Michael Natterer <mitch@gimp.org> * configure.in * po-plug-ins/POTFILES.in * plug-ins/common/Makefile.am * plug-ins/common/plugin-defs.pl * plug-ins/megawidget/*: removed. (There were only 3 functions left which were used by ~5 plugins, so I moved the resp. functions to the plugins). More preview stuff to come... * app/airbrush_blob.c * modules/colorsel_triangle.c * modules/colorsel_water.c: use G_PI instead of M_PI. * app/procedural_db.h * libgimp/gimpenums.h * plug-ins/script-fu/script-fu-constants.c * tools/pdbgen/enums.pl: new PDB return value STATUS_CANCEL which indicates that "Cancel" was pressed in a plugin dialog. (Useful only for file load/save plugins). * app/fileops.[ch] * app/menus.c: changes to handle STATUS_CANCEL correctly. Did some code cleanup in fileops.[ch]. Pop up a warning if File->Save failed. * app/plug_in.c: return_val[0] is of type PDB_STATUS, not PDB_INT32. * libgimp/gimpmath.h: new constant G_MAXRAND which equals to RAND_MAX if it exists or to G_MAXINT otherwise. * libgimp/gimpwidgets.[ch]: new function gimp_random_seed_new() which creates a spinbutton and a "Time" toggle. Call the function which does the "set_sensitive" magic from the radio button callback. * plug-ins/[75 plugins]: - Return STATUS_CANCEL in all file load/save dialogs if "Cancel" was pressed. - Standardized the file plugins' "run" functions. - Use G_PI and G_MAXRAND everywhere. - Added tons of scales and spinbuttons instead of text entries. - Applied uniform packing/spacings all over the place. - Reorganized some UIs (stuff like moving the preview to the top left corner of the dialog). - Removed many ui helper functions and callbacks and use the stuff from libgimp instead. - I tried not to restrict the range of possible values when I replaced entries with spinbuttons/scales but may have failed, though in some cases. Please test ;-) - #include <libgimp/gimpmath.h> where appropriate and use it's constants. - Indentation, s/int/gint/ et.al., code cleanup. RFC: The plugins are definitely not useable with GIMP 1.0 any more, so shouldn't we remove all the remaining compatibility stuff ??? (like "#ifdef GIMP_HAVE_PARASITES")
2000-01-26 01:46:56 +08:00
#include <gtk/gtk.h>
#include <gdk/gdk.h>
Libgimp cleanup part II (with a little help from Yosh who moved the CVS 2000-05-27 Michael Natterer <mitch@gimp.org> Libgimp cleanup part II (with a little help from Yosh who moved the CVS files). * libgimp/Makefile.am * libgimp/gimpchannel_pdb.c * libgimp/gimpdisplay_pdb.c * libgimp/gimpdrawable_pdb.c * libgimp/gimpgradient_pdb.c * libgimp/gimphelp_pdb.c * libgimp/gimpimage_pdb.c * libgimp/gimplayer_pdb.c * libgimp/gimppalette_pdb.c * libgimp/gimpparasite_pdb.c * libgimp/gimpselection_pdb.c * libgimp/gimpunit_pdb.c: new names of all files which contain PDB wrappers. * modules/Makefile.am * libgimp/gimpcolordisplay.h * libgimp/gimpcolorselector.h * modules/gimpmodregister.[ch]: renamed. * libgimp/gimpparasiteF.h * libgimp/gimpparasiteP.h: removed because gimp.h had to include the private header anyway. * app/color_notebook.c * app/color_select.c * app/gdisplay_color.[ch] * app/gdisplay_color_ui.c * app/gimpbrushpipe.c * app/gimpdrawable.[ch] * app/gimpimage.c * app/gimpimage.h * app/gimpparasite.[ch] * app/gimprc.c * app/image_new.c * app/layer.c * app/parasite_cmds.c * app/parasitelist.[ch] * app/plug_in.c * app/procedural_db.c * app/undo.c * app/xcf.c * libgimp/gimp.[ch] * libgimp/gimpcolordisplay.h * libgimp/gimpparasite.[ch] * modules/cdisplay_gamma.c * modules/cdisplay_highcontrast.c * modules/colorsel_gtk.c * modules/colorsel_triangle.c * modules/colorsel_water.c * plug-ins/FractalExplorer/Dialogs.c * plug-ins/FractalExplorer/Events.c * plug-ins/Lighting/lighting_apply.c * plug-ins/Lighting/lighting_shade.c * plug-ins/MapObject/mapobject_image.c * plug-ins/common/gpb.c * plug-ins/common/psp.c * plug-ins/sel2path/sel2path.c * po-libgimp/POTFILES.in * tools/pdbgen/pdb.pl * tools/pdbgen/pdb/parasite.pdb: changed includes accordingly.
2000-05-27 09:30:21 +08:00
#include "gimpmodregister.h"
#include <libgimp/gimpcolorselector.h>
plug-ins/libgck/gck/gck.h removed the GckRGB color type and all it's 2001-01-01 Michael Natterer <mitch@gimp.org> * plug-ins/libgck/gck/gck.h * plug-ins/libgck/gck/gckcolor.c: removed the GckRGB color type and all it's functions. * libgimp/Makefile.am * libgimp/gimpcolor.[ch]: new files containing the new GimpRGB color type and assorted functions. * libgimp/gimpcolorspace.[ch]: colorspace conversion routines for the new GimpRGB type. Also taken from LibGCK. * libgimp/gimp.h * libgimp/gimptypes.h: #include "gimpcolor.h". It's ugly to include it in both files but unavoidable to follow our new "*.c" file include policy. This will go away as libgimp will be chopped up into pieces anyway. * app/apptypes.h * app/asupsample.[ch] * app/blend.c * app/color_transfer.h * app/gradient_header.h: removed "color_t" and use GimpRGB instead. * plug-ins/Lighting/lighting_apply.c * plug-ins/Lighting/lighting_image.c * plug-ins/Lighting/lighting_image.h * plug-ins/Lighting/lighting_main.c * plug-ins/Lighting/lighting_main.h * plug-ins/Lighting/lighting_preview.c * plug-ins/Lighting/lighting_shade.c * plug-ins/Lighting/lighting_shade.h * plug-ins/MapObject/mapobject_apply.c * plug-ins/MapObject/mapobject_image.c * plug-ins/MapObject/mapobject_image.h * plug-ins/MapObject/mapobject_main.c * plug-ins/MapObject/mapobject_main.h * plug-ins/MapObject/mapobject_preview.c * plug-ins/MapObject/mapobject_shade.c * plug-ins/MapObject/mapobject_shade.h * modules/colorsel_triangle.c: s/GckRGB/GimpRGB/g * plug-ins/gdyntext/gdyntextcompat.h: check also for GIMP's minor version when deciding if to add a missing PDB wrapper. (All this compat cruft including libgimp/gimpcompat.h should go away ASAP)
2001-01-02 02:35:09 +08:00
#include <libgimp/gimpcolor.h>
#include <libgimp/gimpcolorspace.h>
#include <libgimp/gimpmodule.h>
configure.in po-plug-ins/POTFILES.in plug-ins/common/Makefile.am 2000-01-25 Michael Natterer <mitch@gimp.org> * configure.in * po-plug-ins/POTFILES.in * plug-ins/common/Makefile.am * plug-ins/common/plugin-defs.pl * plug-ins/megawidget/*: removed. (There were only 3 functions left which were used by ~5 plugins, so I moved the resp. functions to the plugins). More preview stuff to come... * app/airbrush_blob.c * modules/colorsel_triangle.c * modules/colorsel_water.c: use G_PI instead of M_PI. * app/procedural_db.h * libgimp/gimpenums.h * plug-ins/script-fu/script-fu-constants.c * tools/pdbgen/enums.pl: new PDB return value STATUS_CANCEL which indicates that "Cancel" was pressed in a plugin dialog. (Useful only for file load/save plugins). * app/fileops.[ch] * app/menus.c: changes to handle STATUS_CANCEL correctly. Did some code cleanup in fileops.[ch]. Pop up a warning if File->Save failed. * app/plug_in.c: return_val[0] is of type PDB_STATUS, not PDB_INT32. * libgimp/gimpmath.h: new constant G_MAXRAND which equals to RAND_MAX if it exists or to G_MAXINT otherwise. * libgimp/gimpwidgets.[ch]: new function gimp_random_seed_new() which creates a spinbutton and a "Time" toggle. Call the function which does the "set_sensitive" magic from the radio button callback. * plug-ins/[75 plugins]: - Return STATUS_CANCEL in all file load/save dialogs if "Cancel" was pressed. - Standardized the file plugins' "run" functions. - Use G_PI and G_MAXRAND everywhere. - Added tons of scales and spinbuttons instead of text entries. - Applied uniform packing/spacings all over the place. - Reorganized some UIs (stuff like moving the preview to the top left corner of the dialog). - Removed many ui helper functions and callbacks and use the stuff from libgimp instead. - I tried not to restrict the range of possible values when I replaced entries with spinbuttons/scales but may have failed, though in some cases. Please test ;-) - #include <libgimp/gimpmath.h> where appropriate and use it's constants. - Indentation, s/int/gint/ et.al., code cleanup. RFC: The plugins are definitely not useable with GIMP 1.0 any more, so shouldn't we remove all the remaining compatibility stuff ??? (like "#ifdef GIMP_HAVE_PARASITES")
2000-01-26 01:46:56 +08:00
#include <libgimp/gimpmath.h>
Libgimp cleanup part II (with a little help from Yosh who moved the CVS 2000-05-27 Michael Natterer <mitch@gimp.org> Libgimp cleanup part II (with a little help from Yosh who moved the CVS files). * libgimp/Makefile.am * libgimp/gimpchannel_pdb.c * libgimp/gimpdisplay_pdb.c * libgimp/gimpdrawable_pdb.c * libgimp/gimpgradient_pdb.c * libgimp/gimphelp_pdb.c * libgimp/gimpimage_pdb.c * libgimp/gimplayer_pdb.c * libgimp/gimppalette_pdb.c * libgimp/gimpparasite_pdb.c * libgimp/gimpselection_pdb.c * libgimp/gimpunit_pdb.c: new names of all files which contain PDB wrappers. * modules/Makefile.am * libgimp/gimpcolordisplay.h * libgimp/gimpcolorselector.h * modules/gimpmodregister.[ch]: renamed. * libgimp/gimpparasiteF.h * libgimp/gimpparasiteP.h: removed because gimp.h had to include the private header anyway. * app/color_notebook.c * app/color_select.c * app/gdisplay_color.[ch] * app/gdisplay_color_ui.c * app/gimpbrushpipe.c * app/gimpdrawable.[ch] * app/gimpimage.c * app/gimpimage.h * app/gimpparasite.[ch] * app/gimprc.c * app/image_new.c * app/layer.c * app/parasite_cmds.c * app/parasitelist.[ch] * app/plug_in.c * app/procedural_db.c * app/undo.c * app/xcf.c * libgimp/gimp.[ch] * libgimp/gimpcolordisplay.h * libgimp/gimpparasite.[ch] * modules/cdisplay_gamma.c * modules/cdisplay_highcontrast.c * modules/colorsel_gtk.c * modules/colorsel_triangle.c * modules/colorsel_water.c * plug-ins/FractalExplorer/Dialogs.c * plug-ins/FractalExplorer/Events.c * plug-ins/Lighting/lighting_apply.c * plug-ins/Lighting/lighting_shade.c * plug-ins/MapObject/mapobject_image.c * plug-ins/common/gpb.c * plug-ins/common/psp.c * plug-ins/sel2path/sel2path.c * po-libgimp/POTFILES.in * tools/pdbgen/pdb.pl * tools/pdbgen/pdb/parasite.pdb: changed includes accordingly.
2000-05-27 09:30:21 +08:00
#include "libgimp/gimpintl.h"
/* prototypes */
static GtkWidget * colorsel_triangle_new (gint red,
gint green,
gint blue,
gint alpha,
gboolean show_alpha,
GimpColorSelectorCallback callback,
gpointer callback_data,
gpointer *selector_data);
static void colorsel_triangle_free (gpointer selector_data);
static void colorsel_triangle_setcolor (gpointer selector_data,
gint red,
gint green,
gint blue,
gint alpha);
/* local methods */
static GimpColorSelectorMethods methods =
{
colorsel_triangle_new,
colorsel_triangle_free,
colorsel_triangle_setcolor
};
static GimpModuleInfo info =
{
NULL,
N_("Painter-style color selector as a pluggable color selector"),
"Simon Budig <Simon.Budig@unix-ag.org>",
"v0.02",
"(c) 1999, released under the GPL",
"17 Jan 1999"
};
static const GtkTargetEntry targets[] =
{
{ "application/x-color", 0 }
};
#define COLORWHEELRADIUS 100
#define COLORTRIANGLERADIUS 80
#define PREVIEWSIZE (2 * COLORWHEELRADIUS + 1)
#define BGCOLOR 180
#define PREVIEW_MASK GDK_EXPOSURE_MASK | \
GDK_BUTTON_PRESS_MASK | \
GDK_BUTTON_RELEASE_MASK | \
GDK_BUTTON_MOTION_MASK
typedef enum
{
HUE = 0,
SATURATION,
VALUE,
RED,
GREEN,
BLUE,
ALPHA
} ColorSelectFillType;
struct _ColorSelect
{
gint values[7];
gdouble oldsat;
gdouble oldval;
gint mode;
GtkWidget *preview;
GimpColorSelectorCallback callback;
gpointer data;
};
typedef struct _ColorSelect ColorSelect;
static GtkWidget * create_preview (ColorSelect *coldata);
static void color_select_update_rgb_values (ColorSelect *coldata);
static void update_previews (ColorSelect *coldata,
gboolean hue_changed);
static void color_select_update_hsv_values (ColorSelect *coldata);
/*************************************************************/
/* globaly exported init function */
G_MODULE_EXPORT GimpModuleStatus
module_init (GimpModuleInfo **inforet)
{
GimpColorSelectorID id;
1999-05-20 02:02:24 +08:00
#ifndef __EMX__
id = gimp_color_selector_register (_("Triangle"), "triangle.html", &methods);
1999-05-20 02:02:24 +08:00
#else
id = mod_color_selector_register (_("Triangle"), "triangle.html", &methods);
1999-05-20 02:02:24 +08:00
#endif
if (id)
{
info.shutdown_data = id;
*inforet = &info;
return GIMP_MODULE_OK;
}
else
{
return GIMP_MODULE_UNLOAD;
}
}
G_MODULE_EXPORT void
module_unload (gpointer shutdown_data,
GimpColorSelectorFinishedCB completed_cb,
gpointer completed_data)
{
1999-05-20 02:02:24 +08:00
#ifndef __EMX__
gimp_color_selector_unregister (shutdown_data, completed_cb, completed_data);
1999-05-20 02:02:24 +08:00
#else
1999-11-18 00:31:50 +08:00
mod_color_selector_unregister (shutdown_data, completed_cb, completed_data);
1999-05-20 02:02:24 +08:00
#endif
}
/*************************************************************/
/* methods */
static GtkWidget *
colorsel_triangle_new (gint red,
gint green,
gint blue,
gint alpha,
gboolean show_alpha,
GimpColorSelectorCallback callback,
gpointer callback_data,
/* RETURNS: */
gpointer *selector_data)
{
ColorSelect *coldata;
GtkWidget *preview;
GtkWidget *frame;
GtkWidget *hbox;
GtkWidget *vbox;
coldata = g_new (ColorSelect, 1);
coldata->values[RED] = red;
coldata->values[GREEN] = green;
coldata->values[BLUE] = blue;
coldata->values[ALPHA] = alpha;
color_select_update_hsv_values (coldata);
coldata->oldsat = 0;
coldata->oldval = 0;
coldata->mode = 0;
coldata->callback = callback;
coldata->data = callback_data;
preview = create_preview (coldata);
1999-04-20 19:13:33 +08:00
coldata->preview = preview;
update_previews (coldata, TRUE);
*selector_data = coldata;
1999-04-20 19:13:33 +08:00
vbox = gtk_vbox_new (FALSE, 0);
hbox = gtk_hbox_new (FALSE, 0);
gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, FALSE, 0);
1999-04-20 19:13:33 +08:00
frame = gtk_frame_new (NULL);
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
gtk_container_add (GTK_CONTAINER (frame), preview);
gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, FALSE, 0);
gtk_widget_show_all (vbox);
1999-04-20 19:13:33 +08:00
return hbox;
}
static void
colorsel_triangle_free (gpointer selector_data)
{
/* anything else needed to go? */
g_free (selector_data);
}
static void
colorsel_triangle_setcolor (gpointer selector_data,
gint red,
gint green,
gint blue,
gint alpha)
{
ColorSelect *coldata;
coldata = selector_data;
coldata->values[RED] = red;
coldata->values[GREEN] = green;
coldata->values[BLUE] = blue;
coldata->values[ALPHA] = alpha;
color_select_update_hsv_values (coldata);
update_previews (coldata, TRUE);
}
/*************************************************************/
/* helper functions */
static void
color_select_update_rgb_values (ColorSelect *csp)
{
csp->values[RED] = RINT (((gdouble) csp->values[HUE]) / 360.0 * 255);
csp->values[GREEN] = RINT (((gdouble) csp->values[SATURATION]) / 100.0 * 255);
csp->values[BLUE] = RINT (((gdouble) csp->values[VALUE]) / 100.0 * 255);
gimp_hsv_to_rgb_int (&(csp->values[RED]),
&(csp->values[GREEN]),
&(csp->values[BLUE]));
}
static void
color_select_update_hsv_values (ColorSelect *csp)
{
gdouble hue, sat, val;
hue = (gdouble) csp->values[RED] / 255;
sat = (gdouble) csp->values[GREEN] / 255;
val = (gdouble) csp->values[BLUE] / 255;
gimp_rgb_to_hsv_double (&hue, &sat, &val);
csp->values[HUE] = RINT (hue * 360);
csp->values[SATURATION] = RINT (sat * 100);
csp->values[VALUE] = RINT (val * 100);
}
static void
update_previews (ColorSelect *coldata,
gint hue_changed)
{
GtkWidget *preview;
guchar buf[3*PREVIEWSIZE];
gint x, y, k, r2, dx, col;
gint x0, y0;
gdouble hue, sat, val, s, v, atn;
gint hx,hy, sx,sy, vx,vy;
hue = (gdouble) coldata->values[HUE] * G_PI / 180;
hx = sin (hue) * COLORTRIANGLERADIUS;
hy = cos (hue) * COLORTRIANGLERADIUS;
sx = sin (hue - 2 * G_PI/3) * COLORTRIANGLERADIUS;
sy = cos (hue - 2 * G_PI/3) * COLORTRIANGLERADIUS;
vx = sin (hue + 2 * G_PI/3) * COLORTRIANGLERADIUS;
vy = cos (hue + 2 * G_PI/3) * COLORTRIANGLERADIUS;
hue = (gdouble) coldata->values[HUE];
preview = coldata->preview;
if (hue_changed) {
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 {
val = (gdouble) ( (x - sx) * (hy - vy) - (y - sy) * (hx - vx)) /
(gdouble) ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx));
if (val >= 0 && val<= 1) { /* normally val>=0, but this results in
graphics errors... */
sat = (val == 0 ? 0: ((gdouble) (y - sy - val * (vy - sy)) /
(val * (gdouble) (hy - vy))));
if (sat >= 0 && sat <= 1)
gimp_hsv_to_rgb4 (buf + k, hue / 360, sat, val);
}
}
}
k += 3;
}
gtk_preview_draw_row (GTK_PREVIEW (preview), buf, COLORWHEELRADIUS - dx,
COLORWHEELRADIUS - y - 1, 2 * dx + 1);
}
/* marker in outer ring */
x0 = RINT (sin (hue * G_PI/180) *
((gdouble) (COLORWHEELRADIUS - COLORTRIANGLERADIUS + 1) / 2 +
COLORTRIANGLERADIUS) + 0.5);
y0 = RINT (cos (hue * G_PI/180) *
((gdouble) (COLORWHEELRADIUS - COLORTRIANGLERADIUS + 1) / 2 +
COLORTRIANGLERADIUS) + 0.5);
atn = atan2 (x0, y0);
if (atn < 0)
atn = atn + 2 * G_PI;
gimp_hsv_to_rgb4 (buf, atn / (2 * G_PI), 1, 1);
col = 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 (preview), buf,
COLORWHEELRADIUS + x0-4,
COLORWHEELRADIUS - 1 - y, 9);
}
} else {
/* delete marker in triangle */
s = coldata->oldsat;
v = coldata->oldval;
x0 = RINT (sx + (vx - sx) * v + (hx - vx) * s * v);
y0 = RINT (sy + (vy - sy) * v + (hy - vy) * s * v);
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 {
val = (gdouble) ( (x - sx) * (hy - vy) - (y - sy) * (hx - vx)) /
(gdouble) ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx));
if (val > 0 && val <= 1) { /* eigentlich val>=0, aber dann Grafikfehler... */
sat = (val == 0 ? 0 : ((gdouble) (y - sy - val * (vy - sy)) /
(val * (gdouble) (hy - vy))));
if (sat >= 0 && sat <= 1)
gimp_hsv_to_rgb4 (buf + k, hue / 360, sat, val);
}
}
k += 3;
}
gtk_preview_draw_row (GTK_PREVIEW (preview), buf,
COLORWHEELRADIUS + x0 - 4,
COLORWHEELRADIUS - 1 - y, 9);
}
coldata->oldsat = coldata->values[SATURATION] / 100.0;
coldata->oldval = coldata->values[VALUE] / 100.0;
}
/* marker in triangle */
col = INTENSITY (coldata->values[RED], coldata->values[GREEN],
coldata->values[BLUE]) > 127 ? 0 : 255 ;
s = coldata->values[SATURATION] / 100.0;
v = coldata->values[VALUE] / 100.0;
coldata->oldsat = s;
coldata->oldval = v;
x0 = RINT (sx + (vx - sx) * v + (hx - vx) * s * v);
y0 = RINT (sy + (vy - sy) * v + (hy - vy) * s * v);
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 {
val = (gdouble) ( (x - sx) * (hy - vy) - (y - sy) * (hx - vx)) /
(gdouble) ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx));
if (val > 0 && val <= 1) { /* eigentlich val>=0, aber dann Grafikfehler... */
sat = (val == 0 ? 0 : ((gdouble) (y - sy - val * (vy - sy)) /
(val * (gdouble) (hy - vy))));
if (sat >= 0 && sat <= 1)
gimp_hsv_to_rgb4 (buf + k, hue / 360, sat, val);
}
}
}
k += 3;
}
gtk_preview_draw_row (GTK_PREVIEW (preview), buf,
COLORWHEELRADIUS + x0 - 4,
COLORWHEELRADIUS - 1 - y, 9);
}
gtk_widget_draw (preview, NULL);
}
/*
* Color Preview
*/
static gint
color_selection_callback (GtkWidget *widget,
GdkEvent *event)
{
ColorSelect *coldata;
gint x,y, angle, mousex, mousey;
gdouble r;
gdouble hue, sat, val;
gint hx,hy, sx,sy, vx,vy;
coldata = gtk_object_get_user_data (GTK_OBJECT (widget));
switch (event->type) {
case GDK_BUTTON_PRESS:
gtk_grab_add (widget);
x = event->button.x - COLORWHEELRADIUS - 1;
y = - event->button.y + COLORWHEELRADIUS + 1;
r = sqrt ((gdouble) (x * x + y * y));
angle = ((gint) RINT (atan2 (x, y) / G_PI * 180) + 360 ) % 360;
if ( /* r <= COLORWHEELRADIUS && */ r > COLORTRIANGLERADIUS)
1999-01-22 06:07:42 +08:00
coldata->mode = 1; /* Dragging in the Ring */
else
1999-01-22 06:07:42 +08:00
coldata->mode = 2; /* Dragging in the Triangle */
break;
case GDK_MOTION_NOTIFY:
x = event->motion.x - COLORWHEELRADIUS - 1;
y = - event->motion.y + COLORWHEELRADIUS + 1;
r = sqrt ((gdouble) (x * x + y * y));
angle = ((gint) RINT (atan2 (x, y) / G_PI * 180) + 360 ) % 360;
break;
case GDK_BUTTON_RELEASE:
coldata->mode = 0;
gtk_grab_remove (widget);
/* callback the user */
(*coldata->callback) (coldata->data,
coldata->values[RED],
coldata->values[GREEN],
coldata->values[BLUE],
coldata->values[ALPHA]);
return FALSE;
break;
default:
gtk_widget_get_pointer (widget, &x, &y);
x = x - COLORWHEELRADIUS - 1;
y = - y + COLORWHEELRADIUS + 1;
r = sqrt ((gdouble) (x * x + y * y));
angle = ((gint) RINT (atan2 (x, y) / G_PI * 180) + 360 ) % 360;
break;
}
gtk_widget_get_pointer (widget, &mousex, &mousey);
if ((event->type == GDK_MOTION_NOTIFY &&
(mousex != event->motion.x || mousey != event->motion.y)))
return FALSE;
if (coldata->mode == 1 ||
(r > COLORWHEELRADIUS &&
(abs (angle - coldata->values[HUE]) < 30 ||
abs (abs (angle - coldata->values[HUE]) - 360) < 30))) {
coldata->values[HUE] = angle;
color_select_update_rgb_values (coldata);
update_previews (coldata, TRUE);
} else {
hue = (gdouble) coldata->values[HUE] * G_PI / 180;
hx = sin (hue) * COLORTRIANGLERADIUS;
hy = cos (hue) * COLORTRIANGLERADIUS;
sx = sin (hue - 2 * G_PI / 3) * COLORTRIANGLERADIUS;
sy = cos (hue - 2 * G_PI / 3) * COLORTRIANGLERADIUS;
vx = sin (hue + 2 * G_PI / 3) * COLORTRIANGLERADIUS;
vy = cos (hue + 2 * G_PI / 3) * COLORTRIANGLERADIUS;
hue = (gdouble) coldata->values[HUE];
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;
} 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;
} 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;
} 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;
sat = (gdouble) (y - sy - val * (vy - sy)) / (val * (gdouble) (hy - vy));
if (sat < 0)
sat = 0;
else if (sat > 1)
sat = 1;
}
}
coldata->values[SATURATION] = 100 * sat + 0.5;
coldata->values[VALUE] = 100 * val + 0.5;
color_select_update_rgb_values (coldata);
update_previews (coldata, FALSE);
}
/* callback the user */
(*coldata->callback) (coldata->data,
coldata->values[RED],
coldata->values[GREEN],
coldata->values[BLUE],
coldata->values[ALPHA]);
return FALSE;
}
static GtkWidget *
create_preview (ColorSelect *coldata)
{
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);
gtk_widget_set_events (GTK_WIDGET (preview), PREVIEW_MASK );
gtk_preview_size (GTK_PREVIEW (preview), PREVIEWSIZE, PREVIEWSIZE);
gtk_object_set_user_data (GTK_OBJECT (preview), coldata);
gtk_signal_connect (GTK_OBJECT (preview), "motion_notify_event",
GTK_SIGNAL_FUNC (color_selection_callback),
NULL);
gtk_signal_connect (GTK_OBJECT (preview), "button_press_event",
GTK_SIGNAL_FUNC (color_selection_callback),
NULL);
gtk_signal_connect (GTK_OBJECT (preview), "button_release_event",
GTK_SIGNAL_FUNC (color_selection_callback),
NULL);
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);
gtk_widget_draw (preview, NULL);
return preview;
}