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.
41 #include "hildon-color-chooser.h"
42 #include "hildon-color-chooser-private.h"
44 static GtkWidgetClass* parent_class = NULL;
46 /* "crosshair" is hardcoded for now */
47 static gchar crosshair[64] = { 0, 0, 0, 2, 2, 0, 0, 0,
48 0, 2, 2, 3, 3, 2, 2, 0,
49 0, 2, 3, 0, 0, 3, 2, 0,
50 2, 3, 0, 0, 0, 0, 3, 2,
51 2, 3, 0, 0, 0, 0, 3, 2,
52 0, 2, 3, 0, 0, 3, 2, 0,
53 0, 2, 2, 3, 3, 2, 2, 0,
54 0, 0, 0, 2, 2, 0, 0, 0};
57 hildon_color_chooser_init (HildonColorChooser *self);
60 hildon_color_chooser_class_init (HildonColorChooserClass *klass);
63 hildon_color_chooser_dispose (HildonColorChooser *self);
66 hildon_color_chooser_size_request (GtkWidget *widget,
70 hildon_color_chooser_size_allocate (GtkWidget *widget,
71 GtkAllocation *alloc);
74 hildon_color_chooser_realize (GtkWidget *widget);
77 hildon_color_chooser_unrealize (GtkWidget *widget);
80 hildon_color_chooser_map (GtkWidget *widget);
83 hildon_color_chooser_unmap (GtkWidget *widget);
86 hildon_color_chooser_expose (GtkWidget *widget,
87 GdkEventExpose *event);
90 hildon_color_chooser_button_press (GtkWidget *widget,
91 GdkEventButton *event);
94 hildon_color_chooser_button_release (GtkWidget *widget,
95 GdkEventButton *event);
98 hildon_color_chooser_pointer_motion (GtkWidget *widget,
99 GdkEventMotion *event);
102 get_border (GtkWidget *w,
107 init_borders (GtkWidget *w,
112 inline_clip_to_alloc (void *s,
116 inline_sub_times (GTimeVal *result,
121 inline_limited_expose (HildonColorChooser *self);
124 inline_draw_hue_bar (GtkWidget *widget,
133 inline_draw_hue_bar_dimmed (GtkWidget *widget,
142 inline_draw_sv_plane (HildonColorChooser *self,
149 inline_draw_sv_plane_dimmed (HildonColorChooser *self,
156 inline_draw_crosshair (unsigned char *buf,
163 inline_h2rgb (unsigned short hue,
167 hildon_color_chooser_expose_timer (gpointer data);
170 hildon_color_chooser_set_property (GObject *object,
176 hildon_color_chooser_get_property (GObject *object,
181 #define EXPOSE_INTERVAL 50000
183 #define FULL_COLOR8 0xff
185 #define FULL_COLOR 0x00ffffff
199 static guint color_chooser_signals [LAST_SIGNAL] = { 0 };
202 hildon_color_chooser_get_type (void)
204 static GType chooser_type = 0;
207 static const GTypeInfo chooser_info =
209 sizeof (HildonColorChooserClass),
212 (GClassInitFunc) hildon_color_chooser_class_init,
215 sizeof (HildonColorChooser),
217 (GInstanceInitFunc) hildon_color_chooser_init,
221 chooser_type = g_type_register_static (GTK_TYPE_WIDGET,
222 "HildonColorChooser",
230 hildon_color_chooser_init (HildonColorChooser *sel)
233 GTK_WIDGET_SET_FLAGS (sel, GTK_NO_WINDOW);
234 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (sel);
241 priv->mousestate = 0;
242 priv->mousein = FALSE;
244 g_get_current_time (&priv->expose_info.last_expose_time);
246 priv->expose_info.last_expose_hue = priv->currhue;
247 priv->expose_info.expose_queued = 0;
249 priv->dimmed_plane = NULL;
250 priv->dimmed_bar = NULL;
254 hildon_color_chooser_class_init (HildonColorChooserClass *klass)
256 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
257 GObjectClass *object_class = G_OBJECT_CLASS (klass);
259 parent_class = g_type_class_peek_parent (klass);
261 object_class->dispose = (gpointer) hildon_color_chooser_dispose;
262 object_class->get_property = hildon_color_chooser_get_property;
263 object_class->set_property = hildon_color_chooser_set_property;
265 widget_class->size_request = hildon_color_chooser_size_request;
266 widget_class->size_allocate = hildon_color_chooser_size_allocate;
267 widget_class->realize = hildon_color_chooser_realize;
268 widget_class->unrealize = hildon_color_chooser_unrealize;
269 widget_class->map = hildon_color_chooser_map;
270 widget_class->unmap = hildon_color_chooser_unmap;
271 widget_class->expose_event = hildon_color_chooser_expose;
272 widget_class->button_press_event = hildon_color_chooser_button_press;
273 widget_class->button_release_event = hildon_color_chooser_button_release;
274 widget_class->motion_notify_event = hildon_color_chooser_pointer_motion;
276 gtk_widget_class_install_style_property (widget_class,
277 g_param_spec_boxed ("inner_size",
279 "Sizes of SV plane, H bar and spacing",
283 gtk_widget_class_install_style_property (widget_class,
284 g_param_spec_boxed ("graphic_border",
286 "Size of graphical border",
291 * HildonColorChooser:color:
293 * The currently selected color.
295 g_object_class_install_property (object_class, PROP_COLOR,
296 g_param_spec_boxed ("color",
298 "The selected color",
302 color_chooser_signals[COLOR_CHANGED] = g_signal_new("color-changed",
303 G_OBJECT_CLASS_TYPE (object_class),
305 G_STRUCT_OFFSET (HildonColorChooserClass, color_changed),
308 g_cclosure_marshal_VOID__VOID,
312 g_type_class_add_private (klass, sizeof (HildonColorChooserPrivate));
316 hildon_color_chooser_dispose (HildonColorChooser *sel)
318 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (sel);
321 if (priv->dimmed_bar != NULL) {
322 g_object_unref (priv->dimmed_bar);
323 priv->dimmed_bar = NULL;
326 if (priv->dimmed_plane != NULL) {
327 g_object_unref (priv->dimmed_plane);
328 priv->dimmed_plane = NULL;
331 G_OBJECT_CLASS (parent_class)->dispose (G_OBJECT (sel));
335 hildon_color_chooser_size_request (GtkWidget *widget,
338 GtkBorder inner, outer;
340 init_borders (widget, &inner, &outer);
342 req->width = inner.left + inner.top + inner.bottom + outer.left + outer.right;
343 req->height = inner.right + outer.top + outer.bottom;
347 hildon_color_chooser_size_allocate (GtkWidget *widget,
348 GtkAllocation *alloc)
350 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
351 GtkBorder outer, inner;
355 widget->allocation = *alloc;
357 init_borders(widget, &inner, &outer);
359 priv->hba.height = alloc->height - outer.top - outer.bottom;
360 priv->hba.y = alloc->y + outer.top;
361 priv->hba.width = inner.top;
362 priv->hba.x = alloc->x + alloc->width - outer.right - inner.top;
364 priv->spa.x = alloc->x + outer.left;
365 priv->spa.y = alloc->y + outer.top;
366 priv->spa.height = alloc->height - outer.top - outer.bottom;
367 priv->spa.width = alloc->width - outer.left - outer.right - inner.top - inner.bottom;
369 if (GTK_WIDGET_REALIZED (widget)) {
370 gdk_window_move_resize (priv->event_window,
371 widget->allocation.x,
372 widget->allocation.y,
373 widget->allocation.width,
374 widget->allocation.height);
379 hildon_color_chooser_realize (GtkWidget *widget)
381 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
384 GdkWindowAttr attributes;
385 gint attributes_mask;
387 attributes.x = widget->allocation.x;
388 attributes.y = widget->allocation.y;
389 attributes.width = widget->allocation.width;
390 attributes.height = widget->allocation.height;
391 attributes.wclass = GDK_INPUT_ONLY;
392 attributes.window_type = GDK_WINDOW_CHILD;
394 attributes.event_mask = gtk_widget_get_events (widget) |
395 GDK_BUTTON_PRESS_MASK |
396 GDK_BUTTON_RELEASE_MASK |
397 GDK_POINTER_MOTION_MASK |
398 GDK_POINTER_MOTION_HINT_MASK |
399 GDK_BUTTON_MOTION_MASK |
400 GDK_BUTTON1_MOTION_MASK;
402 attributes.visual = gtk_widget_get_visual (widget);
403 attributes.colormap = gtk_widget_get_colormap (widget);
405 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_WMCLASS;
406 priv->event_window = gdk_window_new (widget->parent->window, &attributes, attributes_mask);
409 gdk_window_set_user_data (priv->event_window, widget);
411 GTK_WIDGET_CLASS (parent_class)->realize (widget);
415 hildon_color_chooser_unrealize (GtkWidget *widget)
417 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
421 if (priv->event_window) {
422 gdk_window_set_user_data (priv->event_window, NULL);
423 gdk_window_destroy (priv->event_window);
424 priv->event_window = NULL;
427 GTK_WIDGET_CLASS(parent_class)->unrealize(widget);
431 hildon_color_chooser_map (GtkWidget *widget)
433 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
436 GTK_WIDGET_CLASS(parent_class)->map(widget);
438 if (priv->event_window) {
439 gdk_window_show (priv->event_window);
444 hildon_color_chooser_unmap (GtkWidget *widget)
446 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
450 if (priv->event_window) {
451 gdk_window_hide (priv->event_window);
454 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
458 inline_clip_to_alloc (void *s,
466 if (area->x < a->x) {
467 area->w -= a->x - area->x;
469 } if (area->y < a->y) {
470 area->h -= a->y - area->y;
473 if (area->x + area->w > a->x + a->width)
474 area->w = a->width - (area->x - a->x);
476 if (area->y + area->h > a->y + a->height)
477 area->h = a->height - (area->y - a->y);
481 hildon_color_chooser_expose (GtkWidget *widget,
482 GdkEventExpose *event)
484 HildonColorChooser *sel = HILDON_COLOR_CHOOSER (widget);
485 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
489 GtkBorder graphical_border;
496 if(! GTK_WIDGET_REALIZED (widget)) {
500 get_border (widget, "graphic_border", &graphical_border);
502 if (event->area.width || event->area.height) {
504 gdk_draw_rectangle (widget->window,
505 widget->style->black_gc,
510 priv->hba.height + 3);
512 gdk_draw_rectangle (widget->window,
513 widget->style->black_gc,
518 priv->spa.height + 3);
521 if (priv->expose_info.expose_queued) {
522 if (GTK_WIDGET_SENSITIVE (widget)) {
523 inline_draw_hue_bar (widget, priv->hba.x, priv->hba.y, priv->hba.width, priv->hba.height, priv->hba.y, priv->hba.height);
525 inline_draw_sv_plane (sel, priv->spa.x, priv->spa.y, priv->spa.width, priv->spa.height);
527 inline_draw_hue_bar_dimmed (widget, priv->hba.x, priv->hba.y, priv->hba.width, priv->hba.height, priv->hba.y, priv->hba.height);
529 inline_draw_sv_plane_dimmed (sel, priv->spa.x, priv->spa.y, priv->spa.width, priv->spa.height);
532 priv->expose_info.expose_queued = 0;
534 g_get_current_time (&priv->expose_info.last_expose_time);
537 /* clip hue bar region */
538 area.x = event->area.x;
539 area.y = event->area.y;
540 area.w = event->area.width;
541 area.h = event->area.height;
543 inline_clip_to_alloc (&area, &priv->hba);
545 if(GTK_WIDGET_SENSITIVE (widget)) {
546 inline_draw_hue_bar (widget, area.x, area.y, area.w, area.h, priv->hba.y, priv->hba.height);
548 inline_draw_hue_bar_dimmed (widget, area.x, area.y, area.w, area.h, priv->hba.y, priv->hba.height);
551 area.x = event->area.x;
552 area.y = event->area.y;
553 area.w = event->area.width;
554 area.h = event->area.height;
556 inline_clip_to_alloc (&area, &priv->spa);
558 if (GTK_WIDGET_SENSITIVE (widget)) {
559 inline_draw_sv_plane (sel, area.x, area.y, area.w, area.h);
561 inline_draw_sv_plane_dimmed (sel, area.x, area.y, area.w, area.h);
570 inline_sub_times (GTimeVal *result,
574 result->tv_sec = greater->tv_sec - lesser->tv_sec;
575 result->tv_usec = greater->tv_usec - lesser->tv_usec;
577 if (result->tv_usec < 0) {
579 result->tv_usec += 1000000;
584 inline_limited_expose (HildonColorChooser *sel)
586 GTimeVal curr_time, result;
587 GdkEventExpose event;
588 HildonColorChooserPrivate *priv;
590 if (! GTK_WIDGET_REALIZED (GTK_WIDGET (sel))) {
594 priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (sel);
597 if(priv->currhue == priv->expose_info.last_expose_hue) {
598 return; /* no need to redraw */
601 priv->expose_info.last_expose_hue = priv->currhue;
603 g_get_current_time (&curr_time);
605 inline_sub_times (&result, &curr_time, &priv->expose_info.last_expose_time);
607 if(result.tv_sec != 0 || result.tv_usec >= EXPOSE_INTERVAL) {
609 priv->expose_info.expose_queued = 1;
611 event.type = GDK_EXPOSE;
612 event.area.width = 0;
613 event.area.height = 0;
614 event.window = GTK_WIDGET(sel)->window;
616 gtk_widget_send_expose(GTK_WIDGET(sel), (GdkEvent *)&event);
618 } else if(! priv->expose_info.expose_queued) {
619 priv->expose_info.expose_queued = 1;
620 g_timeout_add ((EXPOSE_INTERVAL - result.tv_usec) / 1000, hildon_color_chooser_expose_timer, sel);
625 hildon_color_chooser_button_press (GtkWidget *widget,
626 GdkEventButton *event)
628 HildonColorChooser *sel = HILDON_COLOR_CHOOSER (widget);
629 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
634 x = (int) event->x + widget->allocation.x;
635 y = (int) event->y + widget->allocation.y;
637 if (x >= priv->spa.x && x <= priv->spa.x + priv->spa.width &&
638 y >= priv->spa.y && y <= priv->spa.y + priv->spa.height) {
640 tmp = y - priv->spa.y;
641 priv->currsat = tmp * 0xffff / priv->spa.height;
642 tmp = x - priv->spa.x;
643 priv->currval = tmp * 0xffff / priv->spa.width;
645 g_signal_emit (sel, color_chooser_signals[COLOR_CHANGED], 0);
646 gtk_widget_queue_draw (widget);
648 priv->mousestate = 1;
649 priv->mousein = TRUE;
651 gtk_grab_add(widget);
653 } else if (x >= priv->hba.x && x <= priv->hba.x + priv->hba.width &&
654 y >= priv->hba.y && y <= priv->hba.y + priv->hba.height) {
656 tmp = y - priv->hba.y;
657 priv->currhue = tmp * 0xffff / priv->hba.height;
659 g_signal_emit (sel, color_chooser_signals[COLOR_CHANGED], 0);
660 inline_limited_expose (sel);
662 priv->mousestate = 2;
663 priv->mousein = TRUE;
665 gtk_grab_add (widget);
672 hildon_color_chooser_button_release (GtkWidget *widget,
673 GdkEventButton *event)
675 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
679 if (priv->mousestate) {
680 gtk_grab_remove (widget);
683 priv->mousestate = 0;
684 priv->mousein = FALSE;
690 hildon_color_chooser_pointer_motion (GtkWidget *widget,
691 GdkEventMotion *event)
693 HildonColorChooser *sel = HILDON_COLOR_CHOOSER (widget);
694 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
696 GdkModifierType mods;
701 if (event->is_hint || (event->window != widget->window))
702 gdk_window_get_pointer (widget->window, &x, &y, &mods);
704 if (priv->mousestate == 1) {
705 if (x >= priv->spa.x && x <= priv->spa.x + priv->spa.width &&
706 y >= priv->spa.y && y <= priv->spa.y + priv->spa.height) {
708 priv->currsat = (((long)(y - priv->spa.y)) * 0xffff) / priv->spa.height;
709 priv->currval = (((long)(x - priv->spa.x)) * 0xffff) / priv->spa.width;
711 g_signal_emit (sel, color_chooser_signals[COLOR_CHANGED], 0);
712 gtk_widget_queue_draw(widget);
714 } else if (priv->mousein == TRUE) {
717 } else if (priv->mousestate == 2) {
718 if (x >= priv->hba.x && x <= priv->hba.x + priv->hba.width &&
719 y >= priv->hba.y && y <= priv->hba.y + priv->hba.height) {
720 tmp = y - priv->hba.y;
722 tmp /= priv->hba.height;
724 if(tmp != priv->currhue) {
727 g_signal_emit (sel, color_chooser_signals[COLOR_CHANGED], 0);
728 inline_limited_expose (sel);
731 } else if (priv->mousein == TRUE) {
739 get_border (GtkWidget *w,
743 GtkBorder *tb = NULL;
745 gtk_widget_style_get (w, name, &tb, NULL);
749 gtk_border_free (tb);
759 init_borders (GtkWidget *w,
765 get_border (w, "outer_border", outer);
767 gtk_widget_style_get (w, "inner_size", &tb, NULL);
779 if (inner->left < 2) inner->left = 2;
780 if (inner->right < 2) inner->right = 2;
781 if (inner->top < 2) inner->top = 2;
785 * hildon_color_chooser_set_color:
786 * @chooser: a #HildonColorChooser
787 * @color: a color to be set
789 * Sets the color selected in the widget.
790 * Will move the crosshair pointer to indicate the passed color.
793 hildon_color_chooser_set_color (HildonColorChooser *chooser,
796 unsigned short hue, sat, val;
797 unsigned long min, max;
798 signed long tmp, diff;
799 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (chooser);
804 min = MIN (MIN (color->red, color->green), color->blue);
805 max = MAX (MAX (color->red, color->green), color->blue);
810 if (val > 0 && diff != 0) {
811 sat = (diff * 0x0000ffff) / max;
813 if (color->red == max) {
814 tmp = (signed) color->green - (signed) color->blue;
821 } else if (color->green == max) {
822 hue = (((signed long) color->blue - (signed long)color->red) * 10922 / diff) + 21844;
824 hue = (((signed long) color->red -(signed long) color->green) * 10922 / diff) + 43688;
835 inline_limited_expose (chooser);
836 g_signal_emit (chooser, color_chooser_signals[COLOR_CHANGED], 0);
840 inline_h2rgb (unsigned short hue,
843 unsigned short hue_rotation, hue_value;
845 hue_rotation = hue / 10922;
846 hue_value = hue % 10922;
848 switch (hue_rotation) {
853 rgb[1] = hue_value * 6*256;
858 rgb[0] = FULL_COLOR - (hue_value * 6*256);
866 rgb[2] = hue_value * 6*256;
871 rgb[1] = FULL_COLOR - (hue_value * 6*256);
876 rgb[0] = hue_value * 6*256;
884 rgb[2] = FULL_COLOR - (hue_value * 6*256);
896 intern_h2rgb8 (unsigned short hue,
899 unsigned short hue_rotation, hue_value;
902 hue_rotation = hue / 42;
903 hue_value = hue % 42;
905 switch (hue_rotation) {
908 rgb[0] = FULL_COLOR8;
909 rgb[1] = hue_value * 6;
914 rgb[0] = FULL_COLOR8 - (hue_value * 6);
915 rgb[1] = FULL_COLOR8;
921 rgb[1] = FULL_COLOR8;
922 rgb[2] = hue_value * 6;
927 rgb[1] = FULL_COLOR8 - (hue_value * 6);
928 rgb[2] = FULL_COLOR8;
932 rgb[0] = hue_value * 6;
934 rgb[2] = FULL_COLOR8;
938 rgb[0] = FULL_COLOR8;
940 rgb[2] = FULL_COLOR8 - (hue_value * 6);
951 /* optimization: do not ask hue for each round but have bilinear vectors */
952 /* rethink: benefits from handling data 8 bit? (no shift round) */
954 inline_draw_hue_bar (GtkWidget *widget,
962 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
964 unsigned short hvec, hcurr;
965 unsigned char *buf, *ptr, tmp[3];
969 if (w <= 0 || h <= 0) {
973 buf = (unsigned char *) g_malloc (w * h * 3);
976 hcurr = hvec * (y - sy);
980 for (i = 0; i < h; i++) {
981 intern_h2rgb8 (hcurr, tmp);
983 for (j = 0; j < w; j++) {
994 gdk_draw_rgb_image (widget->parent->window,
995 widget->style->fg_gc[0],
998 GDK_RGB_DITHER_NONE, buf, w * 3);
1000 tmpy = priv->hba.y + (priv->currhue * priv->hba.height / 0xffff);
1001 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);
1003 if ((((priv->currhue * priv->hba.height) & 0xffff) > 0x8000) && (tmpy < (priv->hba.y + priv->hba.height))) {
1004 gdk_draw_line (widget->parent->window, widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
1005 priv->hba.x, tmpy+1, priv->hba.x + priv->hba.width - 1, tmpy+1);
1006 } else if (tmpy > priv->hba.y) {
1007 gdk_draw_line(widget->parent->window, widget->style->fg_gc[GTK_WIDGET_STATE(widget)], priv->hba.x,
1008 tmpy-1, priv->hba.x + priv->hba.width - 1, tmpy-1);
1015 inline_draw_hue_bar_dimmed (GtkWidget *widget,
1023 HildonColorChooser *sel = HILDON_COLOR_CHOOSER (widget);
1024 HildonColorChooserPrivate *priv;
1026 if (w <= 0 || h <= 0) {
1030 priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (sel);
1033 /* We need to create (and cache) the pixbuf if we don't
1035 if (priv->dimmed_bar == NULL) {
1037 unsigned short hvec, hcurr, avg;
1038 unsigned char *buf, *ptr, tmp[3];
1039 buf = (unsigned char *) g_malloc (w * h * 3);
1042 hcurr = hvec * (y - sy);
1045 for (i = 0; i < h; i++) {
1046 intern_h2rgb8 (hcurr, tmp);
1048 for(j = 0; j < w; j++) {
1049 avg = ((unsigned short) tmp[0]*3 + (unsigned short) tmp[1]*2 + (unsigned short) tmp[2])/6;
1050 ptr[0] = ((((i % 2) + j) % 2) == 0) ? MIN ((avg * 0.7) + 180, 255) : MIN ((avg * 0.7) + 120, 255);
1051 ptr[1] = ((((i % 2) + j) % 2) == 0) ? MIN ((avg * 0.7) + 180, 255) : MIN ((avg * 0.7) + 120, 255);
1052 ptr[2] = ((((i % 2) + j) % 2) == 0) ? MIN ((avg * 0.7) + 180, 255) : MIN ((avg * 0.7) + 120, 255);
1059 priv->dimmed_bar = gdk_pixbuf_new_from_data (buf, GDK_COLORSPACE_RGB, FALSE, 8, w, h, w * 3, (gpointer) g_free, buf);
1062 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);
1066 inline_draw_crosshair (unsigned char *buf,
1074 /* bad "clipping", clip the loop to save cpu */
1075 for(i = 0; i < 8; i++) {
1076 for(j = 0; j < 8; j++) {
1077 sx = j + x; sy = i + y;
1079 if (sx >= 0 && sx < w && sy >= 0 && sy < h) {
1080 if (crosshair[j + 8*i]) {
1081 if (crosshair[j + 8*i] & 0x1) {
1082 buf[(sx)*3+(sy)*w*3+0] = 255;
1083 buf[(sx)*3+(sy)*w*3+1] = 255;
1084 buf[(sx)*3+(sy)*w*3+2] = 255;
1086 buf[(sx)*3+(sy)*w*3+0] = 0;
1087 buf[(sx)*3+(sy)*w*3+1] = 0;
1088 buf[(sx)*3+(sy)*w*3+2] = 0;
1097 inline_draw_sv_plane (HildonColorChooser *sel,
1103 GtkWidget *widget = GTK_WIDGET (sel);
1104 unsigned char *buf, *ptr;
1105 unsigned long rgbx[3] = { 0x00ffffff, 0x00ffffff, 0x00ffffff }, rgbtmp[3];
1106 signed long rgby[3];
1107 HildonColorChooserPrivate *priv;
1111 if (w <= 0 || h <= 0) {
1115 priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (sel);
1117 tmp = priv->spa.width * priv->spa.height;
1119 buf = (unsigned char *) g_malloc (w * h * 3);
1122 inline_h2rgb (priv->currhue, rgbtmp);
1124 rgby[0] = rgbtmp[0] - rgbx[0];
1125 rgby[1] = rgbtmp[1] - rgbx[1];
1126 rgby[2] = rgbtmp[2] - rgbx[2];
1128 rgbx[0] /= priv->spa.width;
1129 rgbx[1] /= priv->spa.width;
1130 rgbx[2] /= priv->spa.width;
1136 rgbx[0] += (y - priv->spa.y) * rgby[0];
1137 rgbx[1] += (y - priv->spa.y) * rgby[1];
1138 rgbx[2] += (y - priv->spa.y) * rgby[2];
1140 for(i = 0; i < h; i++) {
1141 rgbtmp[0] = rgbx[0] * (x - priv->spa.x);
1142 rgbtmp[1] = rgbx[1] * (x - priv->spa.x);
1143 rgbtmp[2] = rgbx[2] * (x - priv->spa.x);
1145 for(j = 0; j < w; j++) {
1146 ptr[0] = rgbtmp[0] >> 16;
1147 ptr[1] = rgbtmp[1] >> 16;
1148 ptr[2] = rgbtmp[2] >> 16;
1149 rgbtmp[0] += rgbx[0];
1150 rgbtmp[1] += rgbx[1];
1151 rgbtmp[2] += rgbx[2];
1160 inline_draw_crosshair (buf,
1161 (priv->spa.width * priv->currval / 0xffff) - x + priv->spa.x - 4,
1162 (priv->spa.height * priv->currsat / 0xffff) - y + priv->spa.y - 4,
1165 gdk_draw_rgb_image (widget->parent->window, widget->style->fg_gc[0], x, y, w, h, GDK_RGB_DITHER_NONE, buf, w * 3);
1170 inline_draw_sv_plane_dimmed (HildonColorChooser *sel,
1176 GtkWidget *widget = GTK_WIDGET (sel);
1177 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (sel);
1181 if (w <= 0 || h <= 0) {
1185 /* We need to create (and cache) the pixbuf if we don't
1187 if (priv->dimmed_plane == NULL) {
1188 unsigned char *buf, *ptr;
1189 unsigned long rgbx[3] = { 0x00ffffff, 0x00ffffff, 0x00ffffff }, rgbtmp[3];
1191 signed long rgby[3];
1192 int tmp = priv->spa.width * priv->spa.height, i, j;
1194 buf = (unsigned char *) g_malloc (w * h * 3);
1198 /* possibe optimization: as we are drawing grayscale plane, there might
1199 be some simpler algorithm to do this*/
1200 rgbtmp[0] = 0x00ffffff;
1201 rgbtmp[1] = 0x00000000;
1202 rgbtmp[2] = 0x00000000;
1204 rgby[0] = rgbtmp[0] - rgbx[0];
1205 rgby[1] = rgbtmp[1] - rgbx[1];
1206 rgby[2] = rgbtmp[2] - rgbx[2];
1208 rgbx[0] /= priv->spa.width;
1209 rgbx[1] /= priv->spa.width;
1210 rgbx[2] /= priv->spa.width;
1216 rgbx[0] += (y - priv->spa.y) * rgby[0];
1217 rgbx[1] += (y - priv->spa.y) * rgby[1];
1218 rgbx[2] += (y - priv->spa.y) * rgby[2];
1220 for(i = 0; i < h; i++) {
1221 rgbtmp[0] = rgbx[0] * (x - priv->spa.x);
1222 rgbtmp[1] = rgbx[1] * (x - priv->spa.x);
1223 rgbtmp[2] = rgbx[2] * (x - priv->spa.x);
1225 for(j = 0; j < w; j++) {
1226 avg = (rgbtmp[0] + rgbtmp[1] + rgbtmp[2])/3;
1228 ptr[0] = ((((i % 2) + j) % 2) == 0) ? MIN ((avg * 0.7) + 180, 255) : MIN ((avg * 0.7) + 120, 255);
1229 ptr[1] = ((((i % 2) + j) % 2) == 0) ? MIN ((avg * 0.7) + 180, 255) : MIN ((avg * 0.7) + 120, 255);
1230 ptr[2] = ((((i % 2) + j) % 2) == 0) ? MIN ((avg * 0.7) + 180, 255) : MIN ((avg * 0.7) + 120, 255);
1231 rgbtmp[0] += rgbx[0];
1232 rgbtmp[1] += rgbx[1];
1233 rgbtmp[2] += rgbx[2];
1242 priv->dimmed_plane = gdk_pixbuf_new_from_data (buf, GDK_COLORSPACE_RGB, FALSE, 8, w, h, w * 3, (gpointer) g_free, buf);
1245 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);
1250 hildon_color_chooser_expose_timer (gpointer data)
1252 HildonColorChooser *sel = HILDON_COLOR_CHOOSER (data);
1253 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (sel);
1257 if (priv->expose_info.expose_queued) {
1258 gtk_widget_queue_draw (GTK_WIDGET (data));
1265 * hildon_color_chooser_get_color:
1266 * @chooser: a #HildonColorChooser
1267 * @color: a color structure to fill with the currently selected color
1269 * Retrives the currently selected color in the chooser.
1273 hildon_color_chooser_get_color (HildonColorChooser *chooser,
1276 HildonColorChooserPrivate *priv;
1277 GdkVisual *system_visual = gdk_visual_get_system ();
1278 unsigned long rgb[3], rgb2[3];
1280 g_return_if_fail (HILDON_IS_COLOR_CHOOSER (chooser));
1281 g_return_if_fail (color != NULL);
1283 priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (chooser);
1286 inline_h2rgb (priv->currhue, rgb);
1288 rgb2[0] = 0xffffff - rgb[0];
1289 rgb2[1] = 0xffffff - rgb[1];
1290 rgb2[2] = 0xffffff - rgb[2];
1292 color->red = ((rgb[0] >> 8) + ((rgb2[0] >> 8) * (0xffff - priv->currsat) / 0xffff)) * priv->currval / 0xffff;
1293 color->green = ((rgb[1] >> 8) + ((rgb2[1] >> 8) * (0xffff - priv->currsat) / 0xffff)) * priv->currval / 0xffff;
1294 color->blue = ((rgb[2] >> 8) + ((rgb2[2] >> 8) * (0xffff - priv->currsat) / 0xffff)) * priv->currval / 0xffff;
1296 color->pixel = ((color->red >> (16 - system_visual->red_prec)) << system_visual->red_shift) |
1297 ((color->green >> (16 - system_visual->green_prec)) << system_visual->green_shift) |
1298 ((color->blue >> (16 - system_visual->blue_prec)) << system_visual->blue_shift);
1302 hildon_color_chooser_new (void)
1304 return (GtkWidget *) g_object_new (HILDON_TYPE_COLOR_CHOOSER, NULL);
1308 hildon_color_chooser_set_property (GObject *object,
1310 const GValue *value,
1313 g_return_if_fail (HILDON_IS_COLOR_CHOOSER (object));
1319 GdkColor *color = g_value_get_boxed (value);
1320 hildon_color_chooser_set_color ((HildonColorChooser *) object, color);
1324 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1330 hildon_color_chooser_get_property (GObject *object,
1335 g_return_if_fail (HILDON_IS_COLOR_CHOOSER (object));
1342 hildon_color_chooser_get_color ((HildonColorChooser *) object, &color);
1343 g_value_set_boxed (value, &color);
1347 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);