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 ("outer_border",
286 "The outer border for the chooser",
290 gtk_widget_class_install_style_property (widget_class,
291 g_param_spec_boxed ("graphic_border",
293 "Size of graphical border",
298 * HildonColorChooser:color:
300 * The currently selected color.
302 g_object_class_install_property (object_class, PROP_COLOR,
303 g_param_spec_boxed ("color",
305 "The selected color",
309 color_chooser_signals[COLOR_CHANGED] = g_signal_new("color-changed",
310 G_OBJECT_CLASS_TYPE (object_class),
312 G_STRUCT_OFFSET (HildonColorChooserClass, color_changed),
315 g_cclosure_marshal_VOID__VOID,
319 g_type_class_add_private (klass, sizeof (HildonColorChooserPrivate));
323 hildon_color_chooser_dispose (HildonColorChooser *sel)
325 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (sel);
328 if (priv->dimmed_bar != NULL) {
329 g_object_unref (priv->dimmed_bar);
330 priv->dimmed_bar = NULL;
333 if (priv->dimmed_plane != NULL) {
334 g_object_unref (priv->dimmed_plane);
335 priv->dimmed_plane = NULL;
338 G_OBJECT_CLASS (parent_class)->dispose (G_OBJECT (sel));
342 hildon_color_chooser_size_request (GtkWidget *widget,
345 GtkBorder inner, outer;
347 init_borders (widget, &inner, &outer);
349 req->width = inner.left + inner.top + inner.bottom + outer.left + outer.right;
350 req->height = inner.right + outer.top + outer.bottom;
354 hildon_color_chooser_size_allocate (GtkWidget *widget,
355 GtkAllocation *alloc)
357 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
358 GtkBorder outer, inner;
362 widget->allocation = *alloc;
364 init_borders(widget, &inner, &outer);
366 priv->hba.height = alloc->height - outer.top - outer.bottom;
367 priv->hba.y = alloc->y + outer.top;
368 priv->hba.width = inner.top;
369 priv->hba.x = alloc->x + alloc->width - outer.right - inner.top;
371 priv->spa.x = alloc->x + outer.left;
372 priv->spa.y = alloc->y + outer.top;
373 priv->spa.height = alloc->height - outer.top - outer.bottom;
374 priv->spa.width = alloc->width - outer.left - outer.right - inner.top - inner.bottom;
376 if (GTK_WIDGET_REALIZED (widget)) {
377 gdk_window_move_resize (priv->event_window,
378 widget->allocation.x,
379 widget->allocation.y,
380 widget->allocation.width,
381 widget->allocation.height);
386 hildon_color_chooser_realize (GtkWidget *widget)
388 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
391 GdkWindowAttr attributes;
392 gint attributes_mask;
394 attributes.x = widget->allocation.x;
395 attributes.y = widget->allocation.y;
396 attributes.width = widget->allocation.width;
397 attributes.height = widget->allocation.height;
398 attributes.wclass = GDK_INPUT_ONLY;
399 attributes.window_type = GDK_WINDOW_CHILD;
401 attributes.event_mask = gtk_widget_get_events (widget) |
402 GDK_BUTTON_PRESS_MASK |
403 GDK_BUTTON_RELEASE_MASK |
404 GDK_POINTER_MOTION_MASK |
405 GDK_POINTER_MOTION_HINT_MASK |
406 GDK_BUTTON_MOTION_MASK |
407 GDK_BUTTON1_MOTION_MASK;
409 attributes.visual = gtk_widget_get_visual (widget);
410 attributes.colormap = gtk_widget_get_colormap (widget);
412 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_WMCLASS;
413 priv->event_window = gdk_window_new (widget->parent->window, &attributes, attributes_mask);
416 gdk_window_set_user_data (priv->event_window, widget);
418 GTK_WIDGET_CLASS (parent_class)->realize (widget);
422 hildon_color_chooser_unrealize (GtkWidget *widget)
424 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
428 if (priv->event_window) {
429 gdk_window_set_user_data (priv->event_window, NULL);
430 gdk_window_destroy (priv->event_window);
431 priv->event_window = NULL;
434 GTK_WIDGET_CLASS(parent_class)->unrealize(widget);
438 hildon_color_chooser_map (GtkWidget *widget)
440 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
443 GTK_WIDGET_CLASS(parent_class)->map(widget);
445 if (priv->event_window) {
446 gdk_window_show (priv->event_window);
451 hildon_color_chooser_unmap (GtkWidget *widget)
453 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
457 if (priv->event_window) {
458 gdk_window_hide (priv->event_window);
461 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
465 inline_clip_to_alloc (void *s,
473 if (area->x < a->x) {
474 area->w -= a->x - area->x;
476 } if (area->y < a->y) {
477 area->h -= a->y - area->y;
480 if (area->x + area->w > a->x + a->width)
481 area->w = a->width - (area->x - a->x);
483 if (area->y + area->h > a->y + a->height)
484 area->h = a->height - (area->y - a->y);
488 hildon_color_chooser_expose (GtkWidget *widget,
489 GdkEventExpose *event)
491 HildonColorChooser *sel = HILDON_COLOR_CHOOSER (widget);
492 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
496 GtkBorder graphical_border;
503 if(! GTK_WIDGET_REALIZED (widget)) {
507 get_border (widget, "graphic_border", &graphical_border);
509 if (event->area.width || event->area.height) {
511 gdk_draw_rectangle (widget->window,
512 widget->style->black_gc,
517 priv->hba.height + 3);
519 gdk_draw_rectangle (widget->window,
520 widget->style->black_gc,
525 priv->spa.height + 3);
528 if (priv->expose_info.expose_queued) {
529 if (GTK_WIDGET_SENSITIVE (widget)) {
530 inline_draw_hue_bar (widget, priv->hba.x, priv->hba.y, priv->hba.width, priv->hba.height, priv->hba.y, priv->hba.height);
532 inline_draw_sv_plane (sel, priv->spa.x, priv->spa.y, priv->spa.width, priv->spa.height);
534 inline_draw_hue_bar_dimmed (widget, priv->hba.x, priv->hba.y, priv->hba.width, priv->hba.height, priv->hba.y, priv->hba.height);
536 inline_draw_sv_plane_dimmed (sel, priv->spa.x, priv->spa.y, priv->spa.width, priv->spa.height);
539 priv->expose_info.expose_queued = 0;
541 g_get_current_time (&priv->expose_info.last_expose_time);
544 /* clip hue bar region */
545 area.x = event->area.x;
546 area.y = event->area.y;
547 area.w = event->area.width;
548 area.h = event->area.height;
550 inline_clip_to_alloc (&area, &priv->hba);
552 if(GTK_WIDGET_SENSITIVE (widget)) {
553 inline_draw_hue_bar (widget, area.x, area.y, area.w, area.h, priv->hba.y, priv->hba.height);
555 inline_draw_hue_bar_dimmed (widget, area.x, area.y, area.w, area.h, priv->hba.y, priv->hba.height);
558 area.x = event->area.x;
559 area.y = event->area.y;
560 area.w = event->area.width;
561 area.h = event->area.height;
563 inline_clip_to_alloc (&area, &priv->spa);
565 if (GTK_WIDGET_SENSITIVE (widget)) {
566 inline_draw_sv_plane (sel, area.x, area.y, area.w, area.h);
568 inline_draw_sv_plane_dimmed (sel, area.x, area.y, area.w, area.h);
577 inline_sub_times (GTimeVal *result,
581 result->tv_sec = greater->tv_sec - lesser->tv_sec;
582 result->tv_usec = greater->tv_usec - lesser->tv_usec;
584 if (result->tv_usec < 0) {
586 result->tv_usec += 1000000;
591 inline_limited_expose (HildonColorChooser *sel)
593 GTimeVal curr_time, result;
594 GdkEventExpose event;
595 HildonColorChooserPrivate *priv;
597 if (! GTK_WIDGET_REALIZED (GTK_WIDGET (sel))) {
601 priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (sel);
604 if(priv->currhue == priv->expose_info.last_expose_hue) {
605 return; /* no need to redraw */
608 priv->expose_info.last_expose_hue = priv->currhue;
610 g_get_current_time (&curr_time);
612 inline_sub_times (&result, &curr_time, &priv->expose_info.last_expose_time);
614 if(result.tv_sec != 0 || result.tv_usec >= EXPOSE_INTERVAL) {
616 priv->expose_info.expose_queued = 1;
618 event.type = GDK_EXPOSE;
619 event.area.width = 0;
620 event.area.height = 0;
621 event.window = GTK_WIDGET(sel)->window;
623 gtk_widget_send_expose(GTK_WIDGET(sel), (GdkEvent *)&event);
625 } else if(! priv->expose_info.expose_queued) {
626 priv->expose_info.expose_queued = 1;
627 g_timeout_add ((EXPOSE_INTERVAL - result.tv_usec) / 1000, hildon_color_chooser_expose_timer, sel);
632 hildon_color_chooser_button_press (GtkWidget *widget,
633 GdkEventButton *event)
635 HildonColorChooser *sel = HILDON_COLOR_CHOOSER (widget);
636 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
641 x = (int) event->x + widget->allocation.x;
642 y = (int) event->y + widget->allocation.y;
644 if (x >= priv->spa.x && x <= priv->spa.x + priv->spa.width &&
645 y >= priv->spa.y && y <= priv->spa.y + priv->spa.height) {
647 tmp = y - priv->spa.y;
648 priv->currsat = tmp * 0xffff / priv->spa.height;
649 tmp = x - priv->spa.x;
650 priv->currval = tmp * 0xffff / priv->spa.width;
652 g_signal_emit (sel, color_chooser_signals[COLOR_CHANGED], 0);
653 gtk_widget_queue_draw (widget);
655 priv->mousestate = 1;
656 priv->mousein = TRUE;
658 gtk_grab_add(widget);
660 } else if (x >= priv->hba.x && x <= priv->hba.x + priv->hba.width &&
661 y >= priv->hba.y && y <= priv->hba.y + priv->hba.height) {
663 tmp = y - priv->hba.y;
664 priv->currhue = tmp * 0xffff / priv->hba.height;
666 g_signal_emit (sel, color_chooser_signals[COLOR_CHANGED], 0);
667 inline_limited_expose (sel);
669 priv->mousestate = 2;
670 priv->mousein = TRUE;
672 gtk_grab_add (widget);
679 hildon_color_chooser_button_release (GtkWidget *widget,
680 GdkEventButton *event)
682 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
686 if (priv->mousestate) {
687 gtk_grab_remove (widget);
690 priv->mousestate = 0;
691 priv->mousein = FALSE;
697 hildon_color_chooser_pointer_motion (GtkWidget *widget,
698 GdkEventMotion *event)
700 HildonColorChooser *sel = HILDON_COLOR_CHOOSER (widget);
701 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
703 GdkModifierType mods;
708 if (event->is_hint || (event->window != widget->window))
709 gdk_window_get_pointer (widget->window, &x, &y, &mods);
711 if (priv->mousestate == 1) {
712 if (x >= priv->spa.x && x <= priv->spa.x + priv->spa.width &&
713 y >= priv->spa.y && y <= priv->spa.y + priv->spa.height) {
715 priv->currsat = (((long)(y - priv->spa.y)) * 0xffff) / priv->spa.height;
716 priv->currval = (((long)(x - priv->spa.x)) * 0xffff) / priv->spa.width;
718 g_signal_emit (sel, color_chooser_signals[COLOR_CHANGED], 0);
719 gtk_widget_queue_draw(widget);
721 } else if (priv->mousein == TRUE) {
724 } else if (priv->mousestate == 2) {
725 if (x >= priv->hba.x && x <= priv->hba.x + priv->hba.width &&
726 y >= priv->hba.y && y <= priv->hba.y + priv->hba.height) {
727 tmp = y - priv->hba.y;
729 tmp /= priv->hba.height;
731 if(tmp != priv->currhue) {
734 g_signal_emit (sel, color_chooser_signals[COLOR_CHANGED], 0);
735 inline_limited_expose (sel);
738 } else if (priv->mousein == TRUE) {
746 get_border (GtkWidget *w,
750 GtkBorder *tb = NULL;
752 gtk_widget_style_get (w, name, &tb, NULL);
756 gtk_border_free (tb);
766 init_borders (GtkWidget *w,
772 get_border (w, "outer_border", outer);
774 gtk_widget_style_get (w, "inner_size", &tb, NULL);
786 if (inner->left < 2) inner->left = 2;
787 if (inner->right < 2) inner->right = 2;
788 if (inner->top < 2) inner->top = 2;
792 * hildon_color_chooser_set_color:
793 * @chooser: a #HildonColorChooser
794 * @color: a color to be set
796 * Sets the color selected in the widget.
797 * Will move the crosshair pointer to indicate the passed color.
800 hildon_color_chooser_set_color (HildonColorChooser *chooser,
803 unsigned short hue, sat, val;
804 unsigned long min, max;
805 signed long tmp, diff;
806 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (chooser);
811 min = MIN (MIN (color->red, color->green), color->blue);
812 max = MAX (MAX (color->red, color->green), color->blue);
817 if (val > 0 && diff != 0) {
818 sat = (diff * 0x0000ffff) / max;
820 if (color->red == max) {
821 tmp = (signed) color->green - (signed) color->blue;
828 } else if (color->green == max) {
829 hue = (((signed long) color->blue - (signed long)color->red) * 10922 / diff) + 21844;
831 hue = (((signed long) color->red -(signed long) color->green) * 10922 / diff) + 43688;
842 inline_limited_expose (chooser);
843 g_signal_emit (chooser, color_chooser_signals[COLOR_CHANGED], 0);
847 inline_h2rgb (unsigned short hue,
850 unsigned short hue_rotation, hue_value;
852 hue_rotation = hue / 10922;
853 hue_value = hue % 10922;
855 switch (hue_rotation) {
860 rgb[1] = hue_value * 6*256;
865 rgb[0] = FULL_COLOR - (hue_value * 6*256);
873 rgb[2] = hue_value * 6*256;
878 rgb[1] = FULL_COLOR - (hue_value * 6*256);
883 rgb[0] = hue_value * 6*256;
891 rgb[2] = FULL_COLOR - (hue_value * 6*256);
903 intern_h2rgb8 (unsigned short hue,
906 unsigned short hue_rotation, hue_value;
909 hue_rotation = hue / 42;
910 hue_value = hue % 42;
912 switch (hue_rotation) {
915 rgb[0] = FULL_COLOR8;
916 rgb[1] = hue_value * 6;
921 rgb[0] = FULL_COLOR8 - (hue_value * 6);
922 rgb[1] = FULL_COLOR8;
928 rgb[1] = FULL_COLOR8;
929 rgb[2] = hue_value * 6;
934 rgb[1] = FULL_COLOR8 - (hue_value * 6);
935 rgb[2] = FULL_COLOR8;
939 rgb[0] = hue_value * 6;
941 rgb[2] = FULL_COLOR8;
945 rgb[0] = FULL_COLOR8;
947 rgb[2] = FULL_COLOR8 - (hue_value * 6);
958 /* optimization: do not ask hue for each round but have bilinear vectors */
959 /* rethink: benefits from handling data 8 bit? (no shift round) */
961 inline_draw_hue_bar (GtkWidget *widget,
969 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
971 unsigned short hvec, hcurr;
972 unsigned char *buf, *ptr, tmp[3];
976 if (w <= 0 || h <= 0) {
980 buf = (unsigned char *) g_malloc (w * h * 3);
983 hcurr = hvec * (y - sy);
987 for (i = 0; i < h; i++) {
988 intern_h2rgb8 (hcurr, tmp);
990 for (j = 0; j < w; j++) {
1001 gdk_draw_rgb_image (widget->parent->window,
1002 widget->style->fg_gc[0],
1005 GDK_RGB_DITHER_NONE, buf, w * 3);
1007 tmpy = priv->hba.y + (priv->currhue * priv->hba.height / 0xffff);
1008 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);
1010 if ((((priv->currhue * priv->hba.height) & 0xffff) > 0x8000) && (tmpy < (priv->hba.y + priv->hba.height))) {
1011 gdk_draw_line (widget->parent->window, widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
1012 priv->hba.x, tmpy+1, priv->hba.x + priv->hba.width - 1, tmpy+1);
1013 } else if (tmpy > priv->hba.y) {
1014 gdk_draw_line(widget->parent->window, widget->style->fg_gc[GTK_WIDGET_STATE(widget)], priv->hba.x,
1015 tmpy-1, priv->hba.x + priv->hba.width - 1, tmpy-1);
1022 inline_draw_hue_bar_dimmed (GtkWidget *widget,
1030 HildonColorChooser *sel = HILDON_COLOR_CHOOSER (widget);
1031 HildonColorChooserPrivate *priv;
1033 if (w <= 0 || h <= 0) {
1037 priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (sel);
1040 /* We need to create (and cache) the pixbuf if we don't
1042 if (priv->dimmed_bar == NULL) {
1044 unsigned short hvec, hcurr, avg;
1045 unsigned char *buf, *ptr, tmp[3];
1046 buf = (unsigned char *) g_malloc (w * h * 3);
1049 hcurr = hvec * (y - sy);
1052 for (i = 0; i < h; i++) {
1053 intern_h2rgb8 (hcurr, tmp);
1055 for(j = 0; j < w; j++) {
1056 avg = ((unsigned short) tmp[0]*3 + (unsigned short) tmp[1]*2 + (unsigned short) tmp[2])/6;
1057 ptr[0] = ((((i % 2) + j) % 2) == 0) ? MIN ((avg * 0.7) + 180, 255) : MIN ((avg * 0.7) + 120, 255);
1058 ptr[1] = ((((i % 2) + j) % 2) == 0) ? MIN ((avg * 0.7) + 180, 255) : MIN ((avg * 0.7) + 120, 255);
1059 ptr[2] = ((((i % 2) + j) % 2) == 0) ? MIN ((avg * 0.7) + 180, 255) : MIN ((avg * 0.7) + 120, 255);
1066 priv->dimmed_bar = gdk_pixbuf_new_from_data (buf, GDK_COLORSPACE_RGB, FALSE, 8, w, h, w * 3, (gpointer) g_free, buf);
1069 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);
1073 inline_draw_crosshair (unsigned char *buf,
1081 /* bad "clipping", clip the loop to save cpu */
1082 for(i = 0; i < 8; i++) {
1083 for(j = 0; j < 8; j++) {
1084 sx = j + x; sy = i + y;
1086 if (sx >= 0 && sx < w && sy >= 0 && sy < h) {
1087 if (crosshair[j + 8*i]) {
1088 if (crosshair[j + 8*i] & 0x1) {
1089 buf[(sx)*3+(sy)*w*3+0] = 255;
1090 buf[(sx)*3+(sy)*w*3+1] = 255;
1091 buf[(sx)*3+(sy)*w*3+2] = 255;
1093 buf[(sx)*3+(sy)*w*3+0] = 0;
1094 buf[(sx)*3+(sy)*w*3+1] = 0;
1095 buf[(sx)*3+(sy)*w*3+2] = 0;
1104 inline_draw_sv_plane (HildonColorChooser *sel,
1110 GtkWidget *widget = GTK_WIDGET (sel);
1111 unsigned char *buf, *ptr;
1112 unsigned long rgbx[3] = { 0x00ffffff, 0x00ffffff, 0x00ffffff }, rgbtmp[3];
1113 signed long rgby[3];
1114 HildonColorChooserPrivate *priv;
1118 if (w <= 0 || h <= 0) {
1122 priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (sel);
1124 tmp = priv->spa.width * priv->spa.height;
1126 buf = (unsigned char *) g_malloc (w * h * 3);
1129 inline_h2rgb (priv->currhue, rgbtmp);
1131 rgby[0] = rgbtmp[0] - rgbx[0];
1132 rgby[1] = rgbtmp[1] - rgbx[1];
1133 rgby[2] = rgbtmp[2] - rgbx[2];
1135 rgbx[0] /= priv->spa.width;
1136 rgbx[1] /= priv->spa.width;
1137 rgbx[2] /= priv->spa.width;
1143 rgbx[0] += (y - priv->spa.y) * rgby[0];
1144 rgbx[1] += (y - priv->spa.y) * rgby[1];
1145 rgbx[2] += (y - priv->spa.y) * rgby[2];
1147 for(i = 0; i < h; i++) {
1148 rgbtmp[0] = rgbx[0] * (x - priv->spa.x);
1149 rgbtmp[1] = rgbx[1] * (x - priv->spa.x);
1150 rgbtmp[2] = rgbx[2] * (x - priv->spa.x);
1152 for(j = 0; j < w; j++) {
1153 ptr[0] = rgbtmp[0] >> 16;
1154 ptr[1] = rgbtmp[1] >> 16;
1155 ptr[2] = rgbtmp[2] >> 16;
1156 rgbtmp[0] += rgbx[0];
1157 rgbtmp[1] += rgbx[1];
1158 rgbtmp[2] += rgbx[2];
1167 inline_draw_crosshair (buf,
1168 (priv->spa.width * priv->currval / 0xffff) - x + priv->spa.x - 4,
1169 (priv->spa.height * priv->currsat / 0xffff) - y + priv->spa.y - 4,
1172 gdk_draw_rgb_image (widget->parent->window, widget->style->fg_gc[0], x, y, w, h, GDK_RGB_DITHER_NONE, buf, w * 3);
1177 inline_draw_sv_plane_dimmed (HildonColorChooser *sel,
1183 GtkWidget *widget = GTK_WIDGET (sel);
1184 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (sel);
1188 if (w <= 0 || h <= 0) {
1192 /* We need to create (and cache) the pixbuf if we don't
1194 if (priv->dimmed_plane == NULL) {
1195 unsigned char *buf, *ptr;
1196 unsigned long rgbx[3] = { 0x00ffffff, 0x00ffffff, 0x00ffffff }, rgbtmp[3];
1198 signed long rgby[3];
1199 int tmp = priv->spa.width * priv->spa.height, i, j;
1201 buf = (unsigned char *) g_malloc (w * h * 3);
1205 /* possibe optimization: as we are drawing grayscale plane, there might
1206 be some simpler algorithm to do this*/
1207 rgbtmp[0] = 0x00ffffff;
1208 rgbtmp[1] = 0x00000000;
1209 rgbtmp[2] = 0x00000000;
1211 rgby[0] = rgbtmp[0] - rgbx[0];
1212 rgby[1] = rgbtmp[1] - rgbx[1];
1213 rgby[2] = rgbtmp[2] - rgbx[2];
1215 rgbx[0] /= priv->spa.width;
1216 rgbx[1] /= priv->spa.width;
1217 rgbx[2] /= priv->spa.width;
1223 rgbx[0] += (y - priv->spa.y) * rgby[0];
1224 rgbx[1] += (y - priv->spa.y) * rgby[1];
1225 rgbx[2] += (y - priv->spa.y) * rgby[2];
1227 for(i = 0; i < h; i++) {
1228 rgbtmp[0] = rgbx[0] * (x - priv->spa.x);
1229 rgbtmp[1] = rgbx[1] * (x - priv->spa.x);
1230 rgbtmp[2] = rgbx[2] * (x - priv->spa.x);
1232 for(j = 0; j < w; j++) {
1233 avg = (rgbtmp[0] + rgbtmp[1] + rgbtmp[2])/3;
1235 ptr[0] = ((((i % 2) + j) % 2) == 0) ? MIN ((avg * 0.7) + 180, 255) : MIN ((avg * 0.7) + 120, 255);
1236 ptr[1] = ((((i % 2) + j) % 2) == 0) ? MIN ((avg * 0.7) + 180, 255) : MIN ((avg * 0.7) + 120, 255);
1237 ptr[2] = ((((i % 2) + j) % 2) == 0) ? MIN ((avg * 0.7) + 180, 255) : MIN ((avg * 0.7) + 120, 255);
1238 rgbtmp[0] += rgbx[0];
1239 rgbtmp[1] += rgbx[1];
1240 rgbtmp[2] += rgbx[2];
1249 priv->dimmed_plane = gdk_pixbuf_new_from_data (buf, GDK_COLORSPACE_RGB, FALSE, 8, w, h, w * 3, (gpointer) g_free, buf);
1252 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);
1257 hildon_color_chooser_expose_timer (gpointer data)
1259 HildonColorChooser *sel = HILDON_COLOR_CHOOSER (data);
1260 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (sel);
1264 if (priv->expose_info.expose_queued) {
1265 gtk_widget_queue_draw (GTK_WIDGET (data));
1272 * hildon_color_chooser_get_color:
1273 * @chooser: a #HildonColorChooser
1274 * @color: a color structure to fill with the currently selected color
1276 * Retrives the currently selected color in the chooser.
1280 hildon_color_chooser_get_color (HildonColorChooser *chooser,
1283 HildonColorChooserPrivate *priv;
1284 GdkVisual *system_visual = gdk_visual_get_system ();
1285 unsigned long rgb[3], rgb2[3];
1287 g_return_if_fail (HILDON_IS_COLOR_CHOOSER (chooser));
1288 g_return_if_fail (color != NULL);
1290 priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (chooser);
1293 inline_h2rgb (priv->currhue, rgb);
1295 rgb2[0] = 0xffffff - rgb[0];
1296 rgb2[1] = 0xffffff - rgb[1];
1297 rgb2[2] = 0xffffff - rgb[2];
1299 color->red = ((rgb[0] >> 8) + ((rgb2[0] >> 8) * (0xffff - priv->currsat) / 0xffff)) * priv->currval / 0xffff;
1300 color->green = ((rgb[1] >> 8) + ((rgb2[1] >> 8) * (0xffff - priv->currsat) / 0xffff)) * priv->currval / 0xffff;
1301 color->blue = ((rgb[2] >> 8) + ((rgb2[2] >> 8) * (0xffff - priv->currsat) / 0xffff)) * priv->currval / 0xffff;
1303 color->pixel = ((color->red >> (16 - system_visual->red_prec)) << system_visual->red_shift) |
1304 ((color->green >> (16 - system_visual->green_prec)) << system_visual->green_shift) |
1305 ((color->blue >> (16 - system_visual->blue_prec)) << system_visual->blue_shift);
1309 hildon_color_chooser_new (void)
1311 return (GtkWidget *) g_object_new (HILDON_TYPE_COLOR_CHOOSER, NULL);
1315 hildon_color_chooser_set_property (GObject *object,
1317 const GValue *value,
1320 g_return_if_fail (HILDON_IS_COLOR_CHOOSER (object));
1326 GdkColor *color = g_value_get_boxed (value);
1327 hildon_color_chooser_set_color ((HildonColorChooser *) object, color);
1331 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1337 hildon_color_chooser_get_property (GObject *object,
1342 g_return_if_fail (HILDON_IS_COLOR_CHOOSER (object));
1349 hildon_color_chooser_get_color ((HildonColorChooser *) object, &color);
1350 g_value_set_boxed (value, &color);
1354 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);