2 * This file is a part of hildon
4 * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
6 * Author: Kuisma Salonen <kuisma.salonen@nokia.com>
7 * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; version 2.1 of
12 * the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
27 * SECTION:hildon-color-chooser
28 * @short_description: A widget used to select a color from an HSV colorspace.
29 * @see_also: #HildonColorChooserDialog
31 * HildonColorChooser is a widget that displays an HSV colorspace.
32 * The user can manipulate the colorspace and easily select and shade of any color
35 * Normally you should not need to use this widget directly. Using #HildonColorButton or
36 * #HildonColorChooserDialog is much more handy.
40 #include "hildon-color-chooser.h"
41 #include "hildon-color-chooser-private.h"
43 static GtkWidgetClass* parent_class = NULL;
45 /* "crosshair" is hardcoded for now */
46 static gchar crosshair[64] = { 0, 0, 0, 2, 2, 0, 0, 0,
47 0, 2, 2, 3, 3, 2, 2, 0,
48 0, 2, 3, 0, 0, 3, 2, 0,
49 2, 3, 0, 0, 0, 0, 3, 2,
50 2, 3, 0, 0, 0, 0, 3, 2,
51 0, 2, 3, 0, 0, 3, 2, 0,
52 0, 2, 2, 3, 3, 2, 2, 0,
53 0, 0, 0, 2, 2, 0, 0, 0};
56 hildon_color_chooser_init (HildonColorChooser *self);
59 hildon_color_chooser_class_init (HildonColorChooserClass *klass);
62 hildon_color_chooser_dispose (HildonColorChooser *self);
65 hildon_color_chooser_size_request (GtkWidget *widget,
69 hildon_color_chooser_size_allocate (GtkWidget *widget,
70 GtkAllocation *alloc);
73 hildon_color_chooser_realize (GtkWidget *widget);
76 hildon_color_chooser_unrealize (GtkWidget *widget);
79 hildon_color_chooser_map (GtkWidget *widget);
82 hildon_color_chooser_unmap (GtkWidget *widget);
85 hildon_color_chooser_expose (GtkWidget *widget,
86 GdkEventExpose *event);
89 hildon_color_chooser_button_press (GtkWidget *widget,
90 GdkEventButton *event);
93 hildon_color_chooser_button_release (GtkWidget *widget,
94 GdkEventButton *event);
97 hildon_color_chooser_pointer_motion (GtkWidget *widget,
98 GdkEventMotion *event);
101 get_border (GtkWidget *w,
106 init_borders (GtkWidget *w,
111 inline_clip_to_alloc (void *s,
115 inline_sub_times (GTimeVal *result,
120 inline_limited_expose (HildonColorChooser *self);
123 inline_draw_hue_bar (GtkWidget *widget,
132 inline_draw_hue_bar_dimmed (GtkWidget *widget,
141 inline_draw_sv_plane (HildonColorChooser *self,
148 inline_draw_sv_plane_dimmed (HildonColorChooser *self,
155 inline_draw_crosshair (unsigned char *buf,
162 inline_h2rgb (unsigned short hue,
166 hildon_color_chooser_expose_timer (gpointer data);
169 hildon_color_chooser_set_property (GObject *object,
175 hildon_color_chooser_get_property (GObject *object,
180 #define EXPOSE_INTERVAL 50000
182 #define FULL_COLOR8 0xff
184 #define FULL_COLOR 0x00ffffff
198 static guint color_chooser_signals [LAST_SIGNAL] = { 0 };
201 hildon_color_chooser_get_type (void)
203 static GType chooser_type = 0;
206 static const GTypeInfo chooser_info =
208 sizeof (HildonColorChooserClass),
211 (GClassInitFunc) hildon_color_chooser_class_init,
214 sizeof (HildonColorChooser),
216 (GInstanceInitFunc) hildon_color_chooser_init,
220 chooser_type = g_type_register_static (GTK_TYPE_WIDGET,
221 "HildonColorChooser",
229 hildon_color_chooser_init (HildonColorChooser *sel)
232 GTK_WIDGET_SET_FLAGS (sel, GTK_NO_WINDOW);
233 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (sel);
240 priv->mousestate = 0;
241 priv->mousein = FALSE;
243 g_get_current_time (&priv->expose_info.last_expose_time);
245 priv->expose_info.last_expose_hue = priv->currhue;
246 priv->expose_info.expose_queued = 0;
248 priv->dimmed_plane = NULL;
249 priv->dimmed_bar = NULL;
253 hildon_color_chooser_class_init (HildonColorChooserClass *klass)
255 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
256 GObjectClass *object_class = G_OBJECT_CLASS (klass);
258 parent_class = g_type_class_peek_parent (klass);
260 object_class->dispose = (gpointer) hildon_color_chooser_dispose;
261 object_class->get_property = hildon_color_chooser_get_property;
262 object_class->set_property = hildon_color_chooser_set_property;
264 widget_class->size_request = hildon_color_chooser_size_request;
265 widget_class->size_allocate = hildon_color_chooser_size_allocate;
266 widget_class->realize = hildon_color_chooser_realize;
267 widget_class->unrealize = hildon_color_chooser_unrealize;
268 widget_class->map = hildon_color_chooser_map;
269 widget_class->unmap = hildon_color_chooser_unmap;
270 widget_class->expose_event = hildon_color_chooser_expose;
271 widget_class->button_press_event = hildon_color_chooser_button_press;
272 widget_class->button_release_event = hildon_color_chooser_button_release;
273 widget_class->motion_notify_event = hildon_color_chooser_pointer_motion;
275 gtk_widget_class_install_style_property (widget_class,
276 g_param_spec_boxed ("inner_size",
278 "Sizes of SV plane, H bar and spacing",
282 gtk_widget_class_install_style_property (widget_class,
283 g_param_spec_boxed ("outer_border",
285 "The outer border for the chooser",
289 gtk_widget_class_install_style_property (widget_class,
290 g_param_spec_boxed ("graphic_border",
292 "Size of graphical border",
297 * HildonColorChooser:color:
299 * The currently selected color.
301 g_object_class_install_property (object_class, PROP_COLOR,
302 g_param_spec_boxed ("color",
304 "The selected color",
308 color_chooser_signals[COLOR_CHANGED] = g_signal_new("color-changed",
309 G_OBJECT_CLASS_TYPE (object_class),
311 G_STRUCT_OFFSET (HildonColorChooserClass, color_changed),
314 g_cclosure_marshal_VOID__VOID,
318 g_type_class_add_private (klass, sizeof (HildonColorChooserPrivate));
322 hildon_color_chooser_dispose (HildonColorChooser *sel)
324 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (sel);
327 if (priv->dimmed_bar != NULL) {
328 g_object_unref (priv->dimmed_bar);
329 priv->dimmed_bar = NULL;
332 if (priv->dimmed_plane != NULL) {
333 g_object_unref (priv->dimmed_plane);
334 priv->dimmed_plane = NULL;
337 G_OBJECT_CLASS (parent_class)->dispose (G_OBJECT (sel));
341 hildon_color_chooser_size_request (GtkWidget *widget,
344 GtkBorder inner, outer;
346 init_borders (widget, &inner, &outer);
348 req->width = inner.left + inner.top + inner.bottom + outer.left + outer.right;
349 req->height = inner.right + outer.top + outer.bottom;
353 hildon_color_chooser_size_allocate (GtkWidget *widget,
354 GtkAllocation *alloc)
356 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
357 GtkBorder outer, inner;
361 widget->allocation = *alloc;
363 init_borders(widget, &inner, &outer);
365 priv->hba.height = alloc->height - outer.top - outer.bottom;
366 priv->hba.y = alloc->y + outer.top;
367 priv->hba.width = inner.top;
368 priv->hba.x = alloc->x + alloc->width - outer.right - inner.top;
370 priv->spa.x = alloc->x + outer.left;
371 priv->spa.y = alloc->y + outer.top;
372 priv->spa.height = alloc->height - outer.top - outer.bottom;
373 priv->spa.width = alloc->width - outer.left - outer.right - inner.top - inner.bottom;
375 if (GTK_WIDGET_REALIZED (widget)) {
376 gdk_window_move_resize (priv->event_window,
377 widget->allocation.x,
378 widget->allocation.y,
379 widget->allocation.width,
380 widget->allocation.height);
385 hildon_color_chooser_realize (GtkWidget *widget)
387 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
390 GdkWindowAttr attributes;
391 gint attributes_mask;
393 attributes.x = widget->allocation.x;
394 attributes.y = widget->allocation.y;
395 attributes.width = widget->allocation.width;
396 attributes.height = widget->allocation.height;
397 attributes.wclass = GDK_INPUT_ONLY;
398 attributes.window_type = GDK_WINDOW_CHILD;
400 attributes.event_mask = gtk_widget_get_events (widget) |
401 GDK_BUTTON_PRESS_MASK |
402 GDK_BUTTON_RELEASE_MASK |
403 GDK_POINTER_MOTION_MASK |
404 GDK_POINTER_MOTION_HINT_MASK |
405 GDK_BUTTON_MOTION_MASK |
406 GDK_BUTTON1_MOTION_MASK;
408 attributes.visual = gtk_widget_get_visual (widget);
409 attributes.colormap = gtk_widget_get_colormap (widget);
411 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_WMCLASS;
412 priv->event_window = gdk_window_new (widget->parent->window, &attributes, attributes_mask);
415 gdk_window_set_user_data (priv->event_window, widget);
417 GTK_WIDGET_CLASS (parent_class)->realize (widget);
421 hildon_color_chooser_unrealize (GtkWidget *widget)
423 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
427 if (priv->event_window) {
428 gdk_window_set_user_data (priv->event_window, NULL);
429 gdk_window_destroy (priv->event_window);
430 priv->event_window = NULL;
433 GTK_WIDGET_CLASS(parent_class)->unrealize(widget);
437 hildon_color_chooser_map (GtkWidget *widget)
439 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
442 GTK_WIDGET_CLASS(parent_class)->map(widget);
444 if (priv->event_window) {
445 gdk_window_show (priv->event_window);
450 hildon_color_chooser_unmap (GtkWidget *widget)
452 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
456 if (priv->event_window) {
457 gdk_window_hide (priv->event_window);
460 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
464 inline_clip_to_alloc (void *s,
472 if (area->x < a->x) {
473 area->w -= a->x - area->x;
475 } if (area->y < a->y) {
476 area->h -= a->y - area->y;
479 if (area->x + area->w > a->x + a->width)
480 area->w = a->width - (area->x - a->x);
482 if (area->y + area->h > a->y + a->height)
483 area->h = a->height - (area->y - a->y);
487 hildon_color_chooser_expose (GtkWidget *widget,
488 GdkEventExpose *event)
490 HildonColorChooser *sel = HILDON_COLOR_CHOOSER (widget);
491 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
495 GtkBorder graphical_border;
502 if(! GTK_WIDGET_REALIZED (widget)) {
506 get_border (widget, "graphic_border", &graphical_border);
508 if (event->area.width || event->area.height) {
510 gdk_draw_rectangle (widget->window,
511 widget->style->black_gc,
516 priv->hba.height + 3);
518 gdk_draw_rectangle (widget->window,
519 widget->style->black_gc,
524 priv->spa.height + 3);
527 if (priv->expose_info.expose_queued) {
528 if (GTK_WIDGET_SENSITIVE (widget)) {
529 inline_draw_hue_bar (widget, priv->hba.x, priv->hba.y, priv->hba.width, priv->hba.height, priv->hba.y, priv->hba.height);
531 inline_draw_sv_plane (sel, priv->spa.x, priv->spa.y, priv->spa.width, priv->spa.height);
533 inline_draw_hue_bar_dimmed (widget, priv->hba.x, priv->hba.y, priv->hba.width, priv->hba.height, priv->hba.y, priv->hba.height);
535 inline_draw_sv_plane_dimmed (sel, priv->spa.x, priv->spa.y, priv->spa.width, priv->spa.height);
538 priv->expose_info.expose_queued = 0;
540 g_get_current_time (&priv->expose_info.last_expose_time);
543 /* clip hue bar region */
544 area.x = event->area.x;
545 area.y = event->area.y;
546 area.w = event->area.width;
547 area.h = event->area.height;
549 inline_clip_to_alloc (&area, &priv->hba);
551 if(GTK_WIDGET_SENSITIVE (widget)) {
552 inline_draw_hue_bar (widget, area.x, area.y, area.w, area.h, priv->hba.y, priv->hba.height);
554 inline_draw_hue_bar_dimmed (widget, area.x, area.y, area.w, area.h, priv->hba.y, priv->hba.height);
557 area.x = event->area.x;
558 area.y = event->area.y;
559 area.w = event->area.width;
560 area.h = event->area.height;
562 inline_clip_to_alloc (&area, &priv->spa);
564 if (GTK_WIDGET_SENSITIVE (widget)) {
565 inline_draw_sv_plane (sel, area.x, area.y, area.w, area.h);
567 inline_draw_sv_plane_dimmed (sel, area.x, area.y, area.w, area.h);
576 inline_sub_times (GTimeVal *result,
580 result->tv_sec = greater->tv_sec - lesser->tv_sec;
581 result->tv_usec = greater->tv_usec - lesser->tv_usec;
583 if (result->tv_usec < 0) {
585 result->tv_usec += 1000000;
590 inline_limited_expose (HildonColorChooser *sel)
592 GTimeVal curr_time, result;
593 GdkEventExpose event;
594 HildonColorChooserPrivate *priv;
596 if (! GTK_WIDGET_REALIZED (GTK_WIDGET (sel))) {
600 priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (sel);
603 if(priv->currhue == priv->expose_info.last_expose_hue) {
604 return; /* no need to redraw */
607 priv->expose_info.last_expose_hue = priv->currhue;
609 g_get_current_time (&curr_time);
611 inline_sub_times (&result, &curr_time, &priv->expose_info.last_expose_time);
613 if(result.tv_sec != 0 || result.tv_usec >= EXPOSE_INTERVAL) {
615 priv->expose_info.expose_queued = 1;
617 event.type = GDK_EXPOSE;
618 event.area.width = 0;
619 event.area.height = 0;
620 event.window = GTK_WIDGET(sel)->window;
622 gtk_widget_send_expose(GTK_WIDGET(sel), (GdkEvent *)&event);
624 } else if(! priv->expose_info.expose_queued) {
625 priv->expose_info.expose_queued = 1;
626 g_timeout_add ((EXPOSE_INTERVAL - result.tv_usec) / 1000, hildon_color_chooser_expose_timer, sel);
631 hildon_color_chooser_button_press (GtkWidget *widget,
632 GdkEventButton *event)
634 HildonColorChooser *sel = HILDON_COLOR_CHOOSER (widget);
635 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
640 x = (int) event->x + widget->allocation.x;
641 y = (int) event->y + widget->allocation.y;
643 if (x >= priv->spa.x && x <= priv->spa.x + priv->spa.width &&
644 y >= priv->spa.y && y <= priv->spa.y + priv->spa.height) {
646 tmp = y - priv->spa.y;
647 priv->currsat = tmp * 0xffff / priv->spa.height;
648 tmp = x - priv->spa.x;
649 priv->currval = tmp * 0xffff / priv->spa.width;
651 g_signal_emit (sel, color_chooser_signals[COLOR_CHANGED], 0);
652 gtk_widget_queue_draw (widget);
654 priv->mousestate = 1;
655 priv->mousein = TRUE;
657 gtk_grab_add(widget);
659 } else if (x >= priv->hba.x && x <= priv->hba.x + priv->hba.width &&
660 y >= priv->hba.y && y <= priv->hba.y + priv->hba.height) {
662 tmp = y - priv->hba.y;
663 priv->currhue = tmp * 0xffff / priv->hba.height;
665 g_signal_emit (sel, color_chooser_signals[COLOR_CHANGED], 0);
666 inline_limited_expose (sel);
668 priv->mousestate = 2;
669 priv->mousein = TRUE;
671 gtk_grab_add (widget);
678 hildon_color_chooser_button_release (GtkWidget *widget,
679 GdkEventButton *event)
681 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
685 if (priv->mousestate) {
686 gtk_grab_remove (widget);
689 priv->mousestate = 0;
690 priv->mousein = FALSE;
696 hildon_color_chooser_pointer_motion (GtkWidget *widget,
697 GdkEventMotion *event)
699 HildonColorChooser *sel = HILDON_COLOR_CHOOSER (widget);
700 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
702 GdkModifierType mods;
707 if (event->is_hint || (event->window != widget->window))
708 gdk_window_get_pointer (widget->window, &x, &y, &mods);
710 if (priv->mousestate == 1) {
711 if (x >= priv->spa.x && x <= priv->spa.x + priv->spa.width &&
712 y >= priv->spa.y && y <= priv->spa.y + priv->spa.height) {
714 priv->currsat = (((long)(y - priv->spa.y)) * 0xffff) / priv->spa.height;
715 priv->currval = (((long)(x - priv->spa.x)) * 0xffff) / priv->spa.width;
717 g_signal_emit (sel, color_chooser_signals[COLOR_CHANGED], 0);
718 gtk_widget_queue_draw(widget);
720 } else if (priv->mousein == TRUE) {
723 } else if (priv->mousestate == 2) {
724 if (x >= priv->hba.x && x <= priv->hba.x + priv->hba.width &&
725 y >= priv->hba.y && y <= priv->hba.y + priv->hba.height) {
726 tmp = y - priv->hba.y;
728 tmp /= priv->hba.height;
730 if(tmp != priv->currhue) {
733 g_signal_emit (sel, color_chooser_signals[COLOR_CHANGED], 0);
734 inline_limited_expose (sel);
737 } else if (priv->mousein == TRUE) {
745 get_border (GtkWidget *w,
749 GtkBorder *tb = NULL;
751 gtk_widget_style_get (w, name, &tb, NULL);
755 gtk_border_free (tb);
765 init_borders (GtkWidget *w,
771 get_border (w, "outer_border", outer);
773 gtk_widget_style_get (w, "inner_size", &tb, NULL);
777 gtk_border_free (tb);
785 if (inner->left < 2) inner->left = 2;
786 if (inner->right < 2) inner->right = 2;
787 if (inner->top < 2) inner->top = 2;
791 * hildon_color_chooser_set_color:
792 * @chooser: a #HildonColorChooser
793 * @color: a color to be set
795 * Sets the color selected in the widget.
796 * Will move the crosshair pointer to indicate the passed color.
799 hildon_color_chooser_set_color (HildonColorChooser *chooser,
802 unsigned short hue, sat, val;
803 unsigned long min, max;
804 signed long tmp, diff;
805 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (chooser);
810 min = MIN (MIN (color->red, color->green), color->blue);
811 max = MAX (MAX (color->red, color->green), color->blue);
816 if (val > 0 && diff != 0) {
817 sat = (diff * 0x0000ffff) / max;
819 if (color->red == max) {
820 tmp = (signed) color->green - (signed) color->blue;
827 } else if (color->green == max) {
828 hue = (((signed long) color->blue - (signed long)color->red) * 10922 / diff) + 21844;
830 hue = (((signed long) color->red -(signed long) color->green) * 10922 / diff) + 43688;
841 inline_limited_expose (chooser);
842 g_signal_emit (chooser, color_chooser_signals[COLOR_CHANGED], 0);
846 inline_h2rgb (unsigned short hue,
849 unsigned short hue_rotation, hue_value;
851 hue_rotation = hue / 10922;
852 hue_value = hue % 10922;
854 switch (hue_rotation) {
859 rgb[1] = hue_value * 6*256;
864 rgb[0] = FULL_COLOR - (hue_value * 6*256);
872 rgb[2] = hue_value * 6*256;
877 rgb[1] = FULL_COLOR - (hue_value * 6*256);
882 rgb[0] = hue_value * 6*256;
890 rgb[2] = FULL_COLOR - (hue_value * 6*256);
902 intern_h2rgb8 (unsigned short hue,
905 unsigned short hue_rotation, hue_value;
908 hue_rotation = hue / 42;
909 hue_value = hue % 42;
911 switch (hue_rotation) {
914 rgb[0] = FULL_COLOR8;
915 rgb[1] = hue_value * 6;
920 rgb[0] = FULL_COLOR8 - (hue_value * 6);
921 rgb[1] = FULL_COLOR8;
927 rgb[1] = FULL_COLOR8;
928 rgb[2] = hue_value * 6;
933 rgb[1] = FULL_COLOR8 - (hue_value * 6);
934 rgb[2] = FULL_COLOR8;
938 rgb[0] = hue_value * 6;
940 rgb[2] = FULL_COLOR8;
944 rgb[0] = FULL_COLOR8;
946 rgb[2] = FULL_COLOR8 - (hue_value * 6);
957 /* optimization: do not ask hue for each round but have bilinear vectors */
958 /* rethink: benefits from handling data 8 bit? (no shift round) */
960 inline_draw_hue_bar (GtkWidget *widget,
968 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
970 unsigned short hvec, hcurr;
971 unsigned char *buf, *ptr, tmp[3];
975 if (w <= 0 || h <= 0) {
979 buf = (unsigned char *) g_malloc (w * h * 3);
982 hcurr = hvec * (y - sy);
986 for (i = 0; i < h; i++) {
987 intern_h2rgb8 (hcurr, tmp);
989 for (j = 0; j < w; j++) {
1000 gdk_draw_rgb_image (widget->parent->window,
1001 widget->style->fg_gc[0],
1004 GDK_RGB_DITHER_NONE, buf, w * 3);
1006 tmpy = priv->hba.y + (priv->currhue * priv->hba.height / 0xffff);
1007 gdk_draw_line (widget->parent->window, widget->style->fg_gc[GTK_WIDGET_STATE(widget)], priv->hba.x, tmpy, priv->hba.x + priv->hba.width - 1, tmpy);
1009 if ((((priv->currhue * priv->hba.height) & 0xffff) > 0x8000) && (tmpy < (priv->hba.y + priv->hba.height))) {
1010 gdk_draw_line (widget->parent->window, widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
1011 priv->hba.x, tmpy+1, priv->hba.x + priv->hba.width - 1, tmpy+1);
1012 } else if (tmpy > priv->hba.y) {
1013 gdk_draw_line(widget->parent->window, widget->style->fg_gc[GTK_WIDGET_STATE(widget)], priv->hba.x,
1014 tmpy-1, priv->hba.x + priv->hba.width - 1, tmpy-1);
1021 inline_draw_hue_bar_dimmed (GtkWidget *widget,
1029 HildonColorChooser *sel = HILDON_COLOR_CHOOSER (widget);
1030 HildonColorChooserPrivate *priv;
1032 if (w <= 0 || h <= 0) {
1036 priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (sel);
1039 /* We need to create (and cache) the pixbuf if we don't
1041 if (priv->dimmed_bar == NULL) {
1043 unsigned short hvec, hcurr, avg;
1044 unsigned char *buf, *ptr, tmp[3];
1045 buf = (unsigned char *) g_malloc (w * h * 3);
1048 hcurr = hvec * (y - sy);
1051 for (i = 0; i < h; i++) {
1052 intern_h2rgb8 (hcurr, tmp);
1054 for(j = 0; j < w; j++) {
1055 avg = ((unsigned short) tmp[0]*3 + (unsigned short) tmp[1]*2 + (unsigned short) tmp[2])/6;
1056 ptr[0] = ((((i % 2) + j) % 2) == 0) ? MIN ((avg * 0.7) + 180, 255) : MIN ((avg * 0.7) + 120, 255);
1057 ptr[1] = ((((i % 2) + j) % 2) == 0) ? MIN ((avg * 0.7) + 180, 255) : MIN ((avg * 0.7) + 120, 255);
1058 ptr[2] = ((((i % 2) + j) % 2) == 0) ? MIN ((avg * 0.7) + 180, 255) : MIN ((avg * 0.7) + 120, 255);
1065 priv->dimmed_bar = gdk_pixbuf_new_from_data (buf, GDK_COLORSPACE_RGB, FALSE, 8, w, h, w * 3, (gpointer) g_free, buf);
1068 gdk_draw_pixbuf (widget->parent->window, widget->style->fg_gc [0], priv->dimmed_bar, 0, 0, x, y, w, h, GDK_RGB_DITHER_NONE, 0, 0);
1072 inline_draw_crosshair (unsigned char *buf,
1080 /* bad "clipping", clip the loop to save cpu */
1081 for(i = 0; i < 8; i++) {
1082 for(j = 0; j < 8; j++) {
1083 sx = j + x; sy = i + y;
1085 if (sx >= 0 && sx < w && sy >= 0 && sy < h) {
1086 if (crosshair[j + 8*i]) {
1087 if (crosshair[j + 8*i] & 0x1) {
1088 buf[(sx)*3+(sy)*w*3+0] = 255;
1089 buf[(sx)*3+(sy)*w*3+1] = 255;
1090 buf[(sx)*3+(sy)*w*3+2] = 255;
1092 buf[(sx)*3+(sy)*w*3+0] = 0;
1093 buf[(sx)*3+(sy)*w*3+1] = 0;
1094 buf[(sx)*3+(sy)*w*3+2] = 0;
1103 inline_draw_sv_plane (HildonColorChooser *sel,
1109 GtkWidget *widget = GTK_WIDGET (sel);
1110 unsigned char *buf, *ptr;
1111 unsigned long rgbx[3] = { 0x00ffffff, 0x00ffffff, 0x00ffffff }, rgbtmp[3];
1112 signed long rgby[3];
1113 HildonColorChooserPrivate *priv;
1117 if (w <= 0 || h <= 0) {
1121 priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (sel);
1123 tmp = priv->spa.width * priv->spa.height;
1125 buf = (unsigned char *) g_malloc (w * h * 3);
1128 inline_h2rgb (priv->currhue, rgbtmp);
1130 rgby[0] = rgbtmp[0] - rgbx[0];
1131 rgby[1] = rgbtmp[1] - rgbx[1];
1132 rgby[2] = rgbtmp[2] - rgbx[2];
1134 rgbx[0] /= priv->spa.width;
1135 rgbx[1] /= priv->spa.width;
1136 rgbx[2] /= priv->spa.width;
1142 rgbx[0] += (y - priv->spa.y) * rgby[0];
1143 rgbx[1] += (y - priv->spa.y) * rgby[1];
1144 rgbx[2] += (y - priv->spa.y) * rgby[2];
1146 for(i = 0; i < h; i++) {
1147 rgbtmp[0] = rgbx[0] * (x - priv->spa.x);
1148 rgbtmp[1] = rgbx[1] * (x - priv->spa.x);
1149 rgbtmp[2] = rgbx[2] * (x - priv->spa.x);
1151 for(j = 0; j < w; j++) {
1152 ptr[0] = rgbtmp[0] >> 16;
1153 ptr[1] = rgbtmp[1] >> 16;
1154 ptr[2] = rgbtmp[2] >> 16;
1155 rgbtmp[0] += rgbx[0];
1156 rgbtmp[1] += rgbx[1];
1157 rgbtmp[2] += rgbx[2];
1166 inline_draw_crosshair (buf,
1167 (priv->spa.width * priv->currval / 0xffff) - x + priv->spa.x - 4,
1168 (priv->spa.height * priv->currsat / 0xffff) - y + priv->spa.y - 4,
1171 gdk_draw_rgb_image (widget->parent->window, widget->style->fg_gc[0], x, y, w, h, GDK_RGB_DITHER_NONE, buf, w * 3);
1176 inline_draw_sv_plane_dimmed (HildonColorChooser *sel,
1182 GtkWidget *widget = GTK_WIDGET (sel);
1183 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (sel);
1187 if (w <= 0 || h <= 0) {
1191 /* We need to create (and cache) the pixbuf if we don't
1193 if (priv->dimmed_plane == NULL) {
1194 unsigned char *buf, *ptr;
1195 unsigned long rgbx[3] = { 0x00ffffff, 0x00ffffff, 0x00ffffff }, rgbtmp[3];
1197 signed long rgby[3];
1198 int tmp = priv->spa.width * priv->spa.height, i, j;
1200 buf = (unsigned char *) g_malloc (w * h * 3);
1204 /* possibe optimization: as we are drawing grayscale plane, there might
1205 be some simpler algorithm to do this*/
1206 rgbtmp[0] = 0x00ffffff;
1207 rgbtmp[1] = 0x00000000;
1208 rgbtmp[2] = 0x00000000;
1210 rgby[0] = rgbtmp[0] - rgbx[0];
1211 rgby[1] = rgbtmp[1] - rgbx[1];
1212 rgby[2] = rgbtmp[2] - rgbx[2];
1214 rgbx[0] /= priv->spa.width;
1215 rgbx[1] /= priv->spa.width;
1216 rgbx[2] /= priv->spa.width;
1222 rgbx[0] += (y - priv->spa.y) * rgby[0];
1223 rgbx[1] += (y - priv->spa.y) * rgby[1];
1224 rgbx[2] += (y - priv->spa.y) * rgby[2];
1226 for(i = 0; i < h; i++) {
1227 rgbtmp[0] = rgbx[0] * (x - priv->spa.x);
1228 rgbtmp[1] = rgbx[1] * (x - priv->spa.x);
1229 rgbtmp[2] = rgbx[2] * (x - priv->spa.x);
1231 for(j = 0; j < w; j++) {
1232 avg = (rgbtmp[0] + rgbtmp[1] + rgbtmp[2])/3;
1234 ptr[0] = ((((i % 2) + j) % 2) == 0) ? MIN ((avg * 0.7) + 180, 255) : MIN ((avg * 0.7) + 120, 255);
1235 ptr[1] = ((((i % 2) + j) % 2) == 0) ? MIN ((avg * 0.7) + 180, 255) : MIN ((avg * 0.7) + 120, 255);
1236 ptr[2] = ((((i % 2) + j) % 2) == 0) ? MIN ((avg * 0.7) + 180, 255) : MIN ((avg * 0.7) + 120, 255);
1237 rgbtmp[0] += rgbx[0];
1238 rgbtmp[1] += rgbx[1];
1239 rgbtmp[2] += rgbx[2];
1248 priv->dimmed_plane = gdk_pixbuf_new_from_data (buf, GDK_COLORSPACE_RGB, FALSE, 8, w, h, w * 3, (gpointer) g_free, buf);
1251 gdk_draw_pixbuf (widget->parent->window, widget->style->fg_gc [0], priv->dimmed_plane, 0, 0, x, y, w, h, GDK_RGB_DITHER_NONE, 0, 0);
1256 hildon_color_chooser_expose_timer (gpointer data)
1258 HildonColorChooser *sel = HILDON_COLOR_CHOOSER (data);
1259 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (sel);
1263 if (priv->expose_info.expose_queued) {
1264 gtk_widget_queue_draw (GTK_WIDGET (data));
1271 * hildon_color_chooser_get_color:
1272 * @chooser: a #HildonColorChooser
1273 * @color: a color structure to fill with the currently selected color
1275 * Retrives the currently selected color in the chooser.
1279 hildon_color_chooser_get_color (HildonColorChooser *chooser,
1282 HildonColorChooserPrivate *priv;
1283 GdkVisual *system_visual = gdk_visual_get_system ();
1284 unsigned long rgb[3], rgb2[3];
1286 g_return_if_fail (HILDON_IS_COLOR_CHOOSER (chooser));
1287 g_return_if_fail (color != NULL);
1289 priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (chooser);
1292 inline_h2rgb (priv->currhue, rgb);
1294 rgb2[0] = 0xffffff - rgb[0];
1295 rgb2[1] = 0xffffff - rgb[1];
1296 rgb2[2] = 0xffffff - rgb[2];
1298 color->red = ((rgb[0] >> 8) + ((rgb2[0] >> 8) * (0xffff - priv->currsat) / 0xffff)) * priv->currval / 0xffff;
1299 color->green = ((rgb[1] >> 8) + ((rgb2[1] >> 8) * (0xffff - priv->currsat) / 0xffff)) * priv->currval / 0xffff;
1300 color->blue = ((rgb[2] >> 8) + ((rgb2[2] >> 8) * (0xffff - priv->currsat) / 0xffff)) * priv->currval / 0xffff;
1302 color->pixel = ((color->red >> (16 - system_visual->red_prec)) << system_visual->red_shift) |
1303 ((color->green >> (16 - system_visual->green_prec)) << system_visual->green_shift) |
1304 ((color->blue >> (16 - system_visual->blue_prec)) << system_visual->blue_shift);
1308 hildon_color_chooser_new (void)
1310 return (GtkWidget *) g_object_new (HILDON_TYPE_COLOR_CHOOSER, NULL);
1314 hildon_color_chooser_set_property (GObject *object,
1316 const GValue *value,
1319 g_return_if_fail (HILDON_IS_COLOR_CHOOSER (object));
1325 GdkColor *color = g_value_get_boxed (value);
1326 hildon_color_chooser_set_color ((HildonColorChooser *) object, color);
1330 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1336 hildon_color_chooser_get_property (GObject *object,
1341 g_return_if_fail (HILDON_IS_COLOR_CHOOSER (object));
1348 hildon_color_chooser_get_color ((HildonColorChooser *) object, &color);
1349 g_value_set_boxed (value, &color);
1353 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);