colorsel_water.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /* Watercolor color_select_module, Raph Levien <raph@acm.org>, February 1998
  2. *
  3. * Ported to loadable color-selector, Sven Neumann <sven@gimp.org>, May 1999
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include "config.h"
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #ifdef __GNUC__
  23. #warning GTK_DISABLE_DEPRECATED
  24. #endif
  25. #undef GTK_DISABLE_DEPRECATED
  26. #include <gtk/gtk.h>
  27. #include "libgimpcolor/gimpcolor.h"
  28. #include "libgimpmath/gimpmath.h"
  29. #include "libgimpmodule/gimpmodule.h"
  30. #include "libgimpwidgets/gimpwidgets.h"
  31. #include "libgimp/libgimp-intl.h"
  32. /* definitions and variables */
  33. #define IMAGE_SIZE GIMP_COLOR_SELECTOR_SIZE
  34. #define COLORSEL_TYPE_WATER (colorsel_water_type)
  35. #define COLORSEL_WATER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), COLORSEL_TYPE_WATER, ColorselWater))
  36. #define COLORSEL_WATER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), COLORSEL_TYPE_WATER, ColorselWaterClass))
  37. #define COLORSEL_IS_WATER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), COLORSEL_TYPE_WATER))
  38. #define COLORSEL_IS_WATER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), COLORSEL_TYPE_WATER))
  39. typedef struct _ColorselWater ColorselWater;
  40. typedef struct _ColorselWaterClass ColorselWaterClass;
  41. struct _ColorselWater
  42. {
  43. GimpColorSelector parent_instance;
  44. gdouble last_x;
  45. gdouble last_y;
  46. gdouble last_pressure;
  47. gfloat pressure_adjust;
  48. guint32 motion_time;
  49. gint button_state;
  50. };
  51. struct _ColorselWaterClass
  52. {
  53. GimpColorSelectorClass parent_class;
  54. };
  55. static GType colorsel_water_get_type (GTypeModule *module);
  56. static void colorsel_water_class_init (ColorselWaterClass *klass);
  57. static void colorsel_water_init (ColorselWater *water);
  58. static void colorsel_water_finalize (GObject *object);
  59. static void colorsel_water_set_color (GimpColorSelector *selector,
  60. const GimpRGB *rgb,
  61. const GimpHSV *hsv);
  62. static void select_area_draw (GtkWidget *preview);
  63. static gboolean button_press_event (GtkWidget *widget,
  64. GdkEventButton *event,
  65. ColorselWater *water);
  66. static gboolean button_release_event (GtkWidget *widget,
  67. GdkEventButton *event,
  68. ColorselWater *water);
  69. static gboolean motion_notify_event (GtkWidget *widget,
  70. GdkEventMotion *event,
  71. ColorselWater *water);
  72. static gboolean proximity_out_event (GtkWidget *widget,
  73. GdkEventProximity *event,
  74. ColorselWater *water);
  75. static void pressure_adjust_update (GtkAdjustment *adj,
  76. ColorselWater *water);
  77. static const GimpModuleInfo colorsel_water_info =
  78. {
  79. GIMP_MODULE_ABI_VERSION,
  80. N_("Watercolor style color selector"),
  81. "Raph Levien <raph@acm.org>, Sven Neumann <sven@gimp.org>",
  82. "v0.3",
  83. "(c) 1998-1999, released under the GPL",
  84. "May, 10 1999"
  85. };
  86. static const GtkTargetEntry targets[] =
  87. {
  88. { "application/x-color", 0 }
  89. };
  90. static GType colorsel_water_type = 0;
  91. static GimpColorSelectorClass *parent_class = NULL;
  92. G_MODULE_EXPORT const GimpModuleInfo *
  93. gimp_module_query (GTypeModule *module)
  94. {
  95. return &colorsel_water_info;
  96. }
  97. G_MODULE_EXPORT gboolean
  98. gimp_module_register (GTypeModule *module)
  99. {
  100. colorsel_water_get_type (module);
  101. return TRUE;
  102. }
  103. static GType
  104. colorsel_water_get_type (GTypeModule *module)
  105. {
  106. if (! colorsel_water_type)
  107. {
  108. static const GTypeInfo select_info =
  109. {
  110. sizeof (ColorselWaterClass),
  111. (GBaseInitFunc) NULL,
  112. (GBaseFinalizeFunc) NULL,
  113. (GClassInitFunc) colorsel_water_class_init,
  114. NULL, /* class_finalize */
  115. NULL, /* class_data */
  116. sizeof (ColorselWater),
  117. 0, /* n_preallocs */
  118. (GInstanceInitFunc) colorsel_water_init,
  119. };
  120. colorsel_water_type =
  121. g_type_module_register_type (module,
  122. GIMP_TYPE_COLOR_SELECTOR,
  123. "ColorselWater",
  124. &select_info, 0);
  125. }
  126. return colorsel_water_type;
  127. }
  128. static void
  129. colorsel_water_class_init (ColorselWaterClass *klass)
  130. {
  131. GObjectClass *object_class;
  132. GimpColorSelectorClass *selector_class;
  133. object_class = G_OBJECT_CLASS (klass);
  134. selector_class = GIMP_COLOR_SELECTOR_CLASS (klass);
  135. parent_class = g_type_class_peek_parent (klass);
  136. object_class->finalize = colorsel_water_finalize;
  137. selector_class->name = _("_Watercolor");
  138. selector_class->help_page = "watercolor.html";
  139. selector_class->stock_id = GIMP_STOCK_TOOL_PAINTBRUSH;
  140. selector_class->set_color = colorsel_water_set_color;
  141. }
  142. static void
  143. colorsel_water_init (ColorselWater *water)
  144. {
  145. GtkWidget *preview;
  146. GtkWidget *event_box;
  147. GtkWidget *frame;
  148. GtkWidget *hbox;
  149. GtkWidget *hbox2;
  150. GtkObject *adj;
  151. GtkWidget *scale;
  152. water->pressure_adjust = 1.0;
  153. hbox = gtk_hbox_new (FALSE, 0);
  154. gtk_box_pack_start (GTK_BOX (water), hbox, TRUE, FALSE, 0);
  155. hbox2 = gtk_hbox_new (FALSE, 4);
  156. gtk_box_pack_start (GTK_BOX (hbox), hbox2, TRUE, FALSE, 0);
  157. /* the event box */
  158. frame = gtk_frame_new (NULL);
  159. gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
  160. gtk_box_pack_start (GTK_BOX (hbox2), frame, FALSE, FALSE, 0);
  161. event_box = gtk_event_box_new ();
  162. gtk_container_add (GTK_CONTAINER (frame), event_box);
  163. preview = gtk_preview_new (GTK_PREVIEW_COLOR);
  164. gtk_preview_size (GTK_PREVIEW (preview), IMAGE_SIZE, IMAGE_SIZE);
  165. gtk_container_add (GTK_CONTAINER (event_box), preview);
  166. select_area_draw (preview);
  167. /* Event signals */
  168. g_signal_connect (event_box, "motion_notify_event",
  169. G_CALLBACK (motion_notify_event),
  170. water);
  171. g_signal_connect (event_box, "button_press_event",
  172. G_CALLBACK (button_press_event),
  173. water);
  174. g_signal_connect (event_box, "button_release_event",
  175. G_CALLBACK (button_release_event),
  176. water);
  177. g_signal_connect (event_box, "proximity_out_event",
  178. G_CALLBACK (proximity_out_event),
  179. water);
  180. gtk_widget_set_events (event_box,
  181. GDK_EXPOSURE_MASK |
  182. GDK_LEAVE_NOTIFY_MASK |
  183. GDK_BUTTON_PRESS_MASK |
  184. GDK_KEY_PRESS_MASK |
  185. GDK_POINTER_MOTION_MASK |
  186. GDK_POINTER_MOTION_HINT_MASK |
  187. GDK_PROXIMITY_OUT_MASK);
  188. /* The following call enables tracking and processing of extension
  189. * events for the drawing area
  190. */
  191. gtk_widget_set_extension_events (event_box, GDK_EXTENSION_EVENTS_ALL);
  192. gtk_widget_grab_focus (event_box);
  193. adj = gtk_adjustment_new (200.0 - water->pressure_adjust * 100.0,
  194. 0.0, 200.0, 1.0, 1.0, 0.0);
  195. g_signal_connect (adj, "value_changed",
  196. G_CALLBACK (pressure_adjust_update),
  197. water);
  198. scale = gtk_vscale_new (GTK_ADJUSTMENT (adj));
  199. gtk_scale_set_digits (GTK_SCALE (scale), 0);
  200. gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE);
  201. gimp_help_set_help_data (scale, _("Pressure"), NULL);
  202. gtk_box_pack_start (GTK_BOX (hbox2), scale, FALSE, FALSE, 0);
  203. gtk_widget_show_all (hbox);
  204. }
  205. static void
  206. colorsel_water_finalize (GObject *object)
  207. {
  208. ColorselWater *water;
  209. water = COLORSEL_WATER (object);
  210. G_OBJECT_CLASS (parent_class)->finalize (object);
  211. }
  212. static void
  213. colorsel_water_set_color (GimpColorSelector *selector,
  214. const GimpRGB *rgb,
  215. const GimpHSV *hsv)
  216. {
  217. ColorselWater *water;
  218. water = COLORSEL_WATER (selector);
  219. }
  220. static gdouble
  221. calc (gdouble x,
  222. gdouble y,
  223. gdouble angle)
  224. {
  225. gdouble s, c;
  226. s = 1.6 * sin (angle * G_PI / 180) * 256.0 / IMAGE_SIZE;
  227. c = 1.6 * cos (angle * G_PI / 180) * 256.0 / IMAGE_SIZE;
  228. return 128 + (x - (IMAGE_SIZE >> 1)) * c - (y - (IMAGE_SIZE >> 1)) * s;
  229. }
  230. /* Initialize the preview */
  231. static void
  232. select_area_draw (GtkWidget *preview)
  233. {
  234. guchar buf[3 * IMAGE_SIZE];
  235. gint x, y;
  236. gdouble r, g, b;
  237. gdouble dr, dg, db;
  238. for (y = 0; y < IMAGE_SIZE; y++)
  239. {
  240. r = calc (0, y, 0);
  241. g = calc (0, y, 120);
  242. b = calc (0, y, 240);
  243. dr = calc (1, y, 0) - r;
  244. dg = calc (1, y, 120) - g;
  245. db = calc (1, y, 240) - b;
  246. for (x = 0; x < IMAGE_SIZE; x++)
  247. {
  248. buf[x * 3] = CLAMP ((gint) r, 0, 255);
  249. buf[x * 3 + 1] = CLAMP ((gint) g, 0, 255);
  250. buf[x * 3 + 2] = CLAMP ((gint) b, 0, 255);
  251. r += dr;
  252. g += dg;
  253. b += db;
  254. }
  255. gtk_preview_draw_row (GTK_PREVIEW (preview), buf, 0, y, IMAGE_SIZE);
  256. }
  257. }
  258. static void
  259. add_pigment (ColorselWater *water,
  260. gboolean erase,
  261. gdouble x,
  262. gdouble y,
  263. gdouble much)
  264. {
  265. GimpColorSelector *selector;
  266. gdouble r, g, b;
  267. selector = GIMP_COLOR_SELECTOR (water);
  268. much *= (gdouble) water->pressure_adjust;
  269. if (erase)
  270. {
  271. selector->rgb.r = 1 - (1 - selector->rgb.r) * (1 - much);
  272. selector->rgb.g = 1 - (1 - selector->rgb.g) * (1 - much);
  273. selector->rgb.b = 1 - (1 - selector->rgb.b) * (1 - much);
  274. }
  275. else
  276. {
  277. r = calc (x, y, 0) / 255.0;
  278. if (r < 0) r = 0;
  279. if (r > 1) r = 1;
  280. g = calc (x, y, 120) / 255.0;
  281. if (g < 0) g = 0;
  282. if (g > 1) g = 1;
  283. b = calc (x, y, 240) / 255.0;
  284. if (b < 0) b = 0;
  285. if (b > 1) b = 1;
  286. selector->rgb.r *= (1 - (1 - r) * much);
  287. selector->rgb.g *= (1 - (1 - g) * much);
  288. selector->rgb.b *= (1 - (1 - b) * much);
  289. }
  290. gimp_rgb_to_hsv (&selector->rgb, &selector->hsv);
  291. gimp_color_selector_color_changed (selector);
  292. }
  293. static void
  294. draw_brush (ColorselWater *water,
  295. GtkWidget *widget,
  296. gboolean erase,
  297. gdouble x,
  298. gdouble y,
  299. gdouble pressure)
  300. {
  301. gdouble much; /* how much pigment to mix in */
  302. if (pressure < water->last_pressure)
  303. water->last_pressure = pressure;
  304. much = sqrt ((x - water->last_x) * (x - water->last_x) +
  305. (y - water->last_y) * (y - water->last_y) +
  306. 1000 *
  307. (pressure - water->last_pressure) *
  308. (pressure - water->last_pressure));
  309. much *= pressure * 0.05;
  310. add_pigment (water, erase, x, y, much);
  311. water->last_x = x;
  312. water->last_y = y;
  313. water->last_pressure = pressure;
  314. }
  315. static gboolean
  316. button_press_event (GtkWidget *widget,
  317. GdkEventButton *event,
  318. ColorselWater *water)
  319. {
  320. gboolean erase = FALSE;
  321. water->last_x = event->x;
  322. water->last_y = event->y;
  323. water->last_pressure = 0.5;
  324. gdk_event_get_axis ((GdkEvent *) event, GDK_AXIS_PRESSURE,
  325. &water->last_pressure);
  326. water->button_state |= 1 << event->button;
  327. erase = (event->button != 1) || FALSE;
  328. /* FIXME: (event->source == GDK_SOURCE_ERASER) */
  329. add_pigment (water, erase, event->x, event->y, 0.05);
  330. water->motion_time = event->time;
  331. return FALSE;
  332. }
  333. static gboolean
  334. button_release_event (GtkWidget *widget,
  335. GdkEventButton *event,
  336. ColorselWater *water)
  337. {
  338. water->button_state &= ~(1 << event->button);
  339. return TRUE;
  340. }
  341. static gboolean
  342. motion_notify_event (GtkWidget *widget,
  343. GdkEventMotion *event,
  344. ColorselWater *water)
  345. {
  346. GdkTimeCoord **coords;
  347. gint nevents;
  348. gint i;
  349. gboolean erase;
  350. if (event->state & (GDK_BUTTON1_MASK |
  351. GDK_BUTTON2_MASK |
  352. GDK_BUTTON3_MASK |
  353. GDK_BUTTON4_MASK))
  354. {
  355. guint32 last_motion_time;
  356. last_motion_time = event->time;
  357. erase = ((event->state &
  358. (GDK_BUTTON2_MASK | GDK_BUTTON3_MASK | GDK_BUTTON4_MASK)) ||
  359. FALSE);
  360. /* FIXME: (event->source == GDK_SOURCE_ERASER) */
  361. water->motion_time = event->time;
  362. if (gdk_device_get_history (event->device,
  363. event->window,
  364. last_motion_time,
  365. event->time,
  366. &coords,
  367. &nevents))
  368. {
  369. for (i = 0; i < nevents; i++)
  370. {
  371. gdouble x = 0.0;
  372. gdouble y = 0.0;
  373. gdouble pressure = 0.5;
  374. gdk_device_get_axis (event->device, coords[i]->axes,
  375. GDK_AXIS_X, &x);
  376. gdk_device_get_axis (event->device, coords[i]->axes,
  377. GDK_AXIS_Y, &y);
  378. gdk_device_get_axis (event->device, coords[i]->axes,
  379. GDK_AXIS_PRESSURE, &pressure);
  380. draw_brush (water, widget, erase, x, y, pressure);
  381. }
  382. g_free (coords);
  383. }
  384. else
  385. {
  386. gdouble pressure = 0.5;
  387. gdk_event_get_axis ((GdkEvent *) event, GDK_AXIS_PRESSURE, &pressure);
  388. draw_brush (water, widget, erase, event->x, event->y, pressure);
  389. }
  390. }
  391. if (event->is_hint)
  392. gdk_device_get_state (event->device, event->window, NULL, NULL);
  393. return TRUE;
  394. }
  395. static gboolean
  396. proximity_out_event (GtkWidget *widget,
  397. GdkEventProximity *event,
  398. ColorselWater *water)
  399. {
  400. return TRUE;
  401. }
  402. static void
  403. pressure_adjust_update (GtkAdjustment *adj,
  404. ColorselWater *water)
  405. {
  406. water->pressure_adjust = (adj->upper - adj->value) / 100.0;
  407. }