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_map (GtkWidget *widget);
80 hildon_color_chooser_unmap (GtkWidget *widget);
83 hildon_color_chooser_expose (GtkWidget *widget,
84 GdkEventExpose *event);
87 hildon_color_chooser_button_press (GtkWidget *widget,
88 GdkEventButton *event);
91 hildon_color_chooser_button_release (GtkWidget *widget,
92 GdkEventButton *event);
95 hildon_color_chooser_pointer_motion (GtkWidget *widget,
96 GdkEventMotion *event);
99 get_border (GtkWidget *w,
104 init_borders (GtkWidget *w,
109 inline_clip_to_alloc (void *s,
113 inline_sub_times (GTimeVal *result,
118 inline_limited_expose (HildonColorChooser *self);
121 inline_draw_hue_bar (GtkWidget *widget,
130 inline_draw_hue_bar_dimmed (GtkWidget *widget,
139 inline_draw_sv_plane (HildonColorChooser *self,
146 inline_draw_sv_plane_dimmed (HildonColorChooser *self,
153 inline_draw_crosshair (unsigned char *buf,
160 inline_h2rgb (unsigned short hue,
164 hildon_color_chooser_expose_timer (gpointer data);
167 hildon_color_chooser_set_property (GObject *object,
173 hildon_color_chooser_get_property (GObject *object,
178 #define EXPOSE_INTERVAL 50000
180 #define FULL_COLOR8 0xff
182 #define FULL_COLOR 0x00ffffff
196 static guint color_chooser_signals [LAST_SIGNAL] = { 0 };
199 hildon_color_chooser_get_type (void)
201 static GType chooser_type = 0;
204 static const GTypeInfo chooser_info =
206 sizeof (HildonColorChooserClass),
209 (GClassInitFunc) hildon_color_chooser_class_init,
212 sizeof (HildonColorChooser),
214 (GInstanceInitFunc) hildon_color_chooser_init,
218 chooser_type = g_type_register_static (GTK_TYPE_WIDGET,
219 "HildonColorChooser",
227 hildon_color_chooser_init (HildonColorChooser *sel)
230 GTK_WIDGET_SET_FLAGS (sel, GTK_NO_WINDOW);
231 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (sel);
238 priv->mousestate = 0;
239 priv->mousein = FALSE;
241 g_get_current_time (&priv->expose_info.last_expose_time);
243 priv->expose_info.last_expose_hue = priv->currhue;
244 priv->expose_info.expose_queued = 0;
246 priv->dimmed_plane = NULL;
247 priv->dimmed_bar = NULL;
251 hildon_color_chooser_class_init (HildonColorChooserClass *klass)
253 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
254 GObjectClass *object_class = G_OBJECT_CLASS (klass);
256 parent_class = g_type_class_peek_parent (klass);
258 object_class->dispose = (gpointer) hildon_color_chooser_dispose;
259 object_class->get_property = hildon_color_chooser_get_property;
260 object_class->set_property = hildon_color_chooser_set_property;
262 widget_class->size_request = hildon_color_chooser_size_request;
263 widget_class->size_allocate = hildon_color_chooser_size_allocate;
264 widget_class->realize = hildon_color_chooser_realize;
265 widget_class->map = hildon_color_chooser_map;
266 widget_class->unmap = hildon_color_chooser_unmap;
267 widget_class->expose_event = hildon_color_chooser_expose;
268 widget_class->button_press_event = hildon_color_chooser_button_press;
269 widget_class->button_release_event = hildon_color_chooser_button_release;
270 widget_class->motion_notify_event = hildon_color_chooser_pointer_motion;
272 gtk_widget_class_install_style_property (widget_class,
273 g_param_spec_boxed ("inner_size",
275 "Sizes of SV plane, H bar and spacing",
279 gtk_widget_class_install_style_property (widget_class,
280 g_param_spec_boxed ("graphic_border",
282 "Size of graphical border",
287 * HildonColorChooser:color:
289 * The currently selected color.
291 g_object_class_install_property (object_class, PROP_COLOR,
292 g_param_spec_boxed ("color",
294 "The selected color",
298 color_chooser_signals[COLOR_CHANGED] = g_signal_new("color-changed",
299 G_OBJECT_CLASS_TYPE (object_class),
301 G_STRUCT_OFFSET (HildonColorChooserClass, color_changed),
304 g_cclosure_marshal_VOID__VOID,
308 g_type_class_add_private (klass, sizeof (HildonColorChooserPrivate));
312 hildon_color_chooser_dispose (HildonColorChooser *sel)
314 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (sel);
317 if (priv->dimmed_bar != NULL) {
318 g_object_unref (priv->dimmed_bar);
319 priv->dimmed_bar = NULL;
322 if (priv->dimmed_plane != NULL) {
323 g_object_unref (priv->dimmed_plane);
324 priv->dimmed_plane = NULL;
327 G_OBJECT_CLASS (parent_class)->dispose (G_OBJECT (sel));
331 hildon_color_chooser_size_request (GtkWidget *widget,
334 GtkBorder inner, outer;
336 init_borders (widget, &inner, &outer);
338 req->width = inner.left + inner.top + inner.bottom + outer.left + outer.right;
339 req->height = inner.right + outer.top + outer.bottom;
343 hildon_color_chooser_size_allocate (GtkWidget *widget,
344 GtkAllocation *alloc)
346 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
347 GtkBorder outer, inner;
351 widget->allocation = *alloc;
353 init_borders(widget, &inner, &outer);
355 priv->hba.height = alloc->height - outer.top - outer.bottom;
356 priv->hba.y = alloc->y + outer.top;
357 priv->hba.width = inner.top;
358 priv->hba.x = alloc->x + alloc->width - outer.right - inner.top;
360 priv->spa.x = alloc->x + outer.left;
361 priv->spa.y = alloc->y + outer.top;
362 priv->spa.height = alloc->height - outer.top - outer.bottom;
363 priv->spa.width = alloc->width - outer.left - outer.right - inner.top - inner.bottom;
365 if (GTK_WIDGET_REALIZED (widget)) {
366 gdk_window_move_resize (priv->event_window,
367 widget->allocation.x,
368 widget->allocation.y,
369 widget->allocation.width,
370 widget->allocation.height);
375 hildon_color_chooser_realize (GtkWidget *widget)
377 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
380 GdkWindowAttr attributes;
381 gint attributes_mask;
383 attributes.x = widget->allocation.x;
384 attributes.y = widget->allocation.y;
385 attributes.width = widget->allocation.width;
386 attributes.height = widget->allocation.height;
387 attributes.wclass = GDK_INPUT_ONLY;
388 attributes.window_type = GDK_WINDOW_CHILD;
390 attributes.event_mask = gtk_widget_get_events (widget) |
391 GDK_BUTTON_PRESS_MASK |
392 GDK_BUTTON_RELEASE_MASK |
393 GDK_POINTER_MOTION_MASK |
394 GDK_POINTER_MOTION_HINT_MASK |
395 GDK_BUTTON_MOTION_MASK |
396 GDK_BUTTON1_MOTION_MASK;
398 attributes.visual = gtk_widget_get_visual (widget);
399 attributes.colormap = gtk_widget_get_colormap (widget);
401 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_WMCLASS;
402 priv->event_window = gdk_window_new (widget->parent->window, &attributes, attributes_mask);
405 gdk_window_set_user_data (priv->event_window, widget);
406 widget->window = gtk_widget_get_parent_window (widget);
408 widget->style = gtk_style_attach (widget->style, widget->window);
410 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
414 hildon_color_chooser_map (GtkWidget *widget)
416 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
419 GTK_WIDGET_CLASS(parent_class)->map(widget);
421 if (priv->event_window) {
422 gdk_window_show (priv->event_window);
427 hildon_color_chooser_unmap (GtkWidget *widget)
429 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
433 if (priv->event_window) {
434 gdk_window_hide (priv->event_window);
437 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
441 inline_clip_to_alloc (void *s,
449 if (area->x < a->x) {
450 area->w -= a->x - area->x;
452 } if (area->y < a->y) {
453 area->h -= a->y - area->y;
456 if (area->x + area->w > a->x + a->width)
457 area->w = a->width - (area->x - a->x);
459 if (area->y + area->h > a->y + a->height)
460 area->h = a->height - (area->y - a->y);
464 hildon_color_chooser_expose (GtkWidget *widget,
465 GdkEventExpose *event)
467 HildonColorChooser *sel = HILDON_COLOR_CHOOSER (widget);
468 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
472 GtkBorder graphical_border;
479 if(! GTK_WIDGET_REALIZED (widget)) {
483 get_border (widget, "graphic_border", &graphical_border);
485 if (event->area.width || event->area.height) {
487 gdk_draw_rectangle (widget->window,
488 widget->style->black_gc,
493 priv->hba.height + 3);
495 gdk_draw_rectangle (widget->window,
496 widget->style->black_gc,
501 priv->spa.height + 3);
504 if (priv->expose_info.expose_queued) {
505 if (GTK_WIDGET_SENSITIVE (widget)) {
506 inline_draw_hue_bar (widget, priv->hba.x, priv->hba.y, priv->hba.width, priv->hba.height, priv->hba.y, priv->hba.height);
508 inline_draw_sv_plane (sel, priv->spa.x, priv->spa.y, priv->spa.width, priv->spa.height);
510 inline_draw_hue_bar_dimmed (widget, priv->hba.x, priv->hba.y, priv->hba.width, priv->hba.height, priv->hba.y, priv->hba.height);
512 inline_draw_sv_plane_dimmed (sel, priv->spa.x, priv->spa.y, priv->spa.width, priv->spa.height);
515 priv->expose_info.expose_queued = 0;
517 g_get_current_time (&priv->expose_info.last_expose_time);
520 /* clip hue bar region */
521 area.x = event->area.x;
522 area.y = event->area.y;
523 area.w = event->area.width;
524 area.h = event->area.height;
526 inline_clip_to_alloc (&area, &priv->hba);
528 if(GTK_WIDGET_SENSITIVE (widget)) {
529 inline_draw_hue_bar (widget, area.x, area.y, area.w, area.h, priv->hba.y, priv->hba.height);
531 inline_draw_hue_bar_dimmed (widget, area.x, area.y, area.w, area.h, priv->hba.y, priv->hba.height);
534 area.x = event->area.x;
535 area.y = event->area.y;
536 area.w = event->area.width;
537 area.h = event->area.height;
539 inline_clip_to_alloc (&area, &priv->spa);
541 if (GTK_WIDGET_SENSITIVE (widget)) {
542 inline_draw_sv_plane (sel, area.x, area.y, area.w, area.h);
544 inline_draw_sv_plane_dimmed (sel, area.x, area.y, area.w, area.h);
553 inline_sub_times (GTimeVal *result,
557 result->tv_sec = greater->tv_sec - lesser->tv_sec;
558 result->tv_usec = greater->tv_usec - lesser->tv_usec;
560 if (result->tv_usec < 0) {
562 result->tv_usec += 1000000;
567 inline_limited_expose (HildonColorChooser *sel)
569 GTimeVal curr_time, result;
570 GdkEventExpose event;
571 HildonColorChooserPrivate *priv;
573 if (! GTK_WIDGET_REALIZED (GTK_WIDGET (sel))) {
577 priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (sel);
580 if(priv->currhue == priv->expose_info.last_expose_hue) {
581 return; /* no need to redraw */
584 priv->expose_info.last_expose_hue = priv->currhue;
586 g_get_current_time (&curr_time);
588 inline_sub_times (&result, &curr_time, &priv->expose_info.last_expose_time);
590 if(result.tv_sec != 0 || result.tv_usec >= EXPOSE_INTERVAL) {
592 priv->expose_info.expose_queued = 1;
594 event.type = GDK_EXPOSE;
595 event.area.width = 0;
596 event.area.height = 0;
597 event.window = GTK_WIDGET(sel)->window;
599 gtk_widget_send_expose(GTK_WIDGET(sel), (GdkEvent *)&event);
601 } else if(! priv->expose_info.expose_queued) {
602 priv->expose_info.expose_queued = 1;
603 g_timeout_add ((EXPOSE_INTERVAL - result.tv_usec) / 1000, hildon_color_chooser_expose_timer, sel);
608 hildon_color_chooser_button_press (GtkWidget *widget,
609 GdkEventButton *event)
611 HildonColorChooser *sel = HILDON_COLOR_CHOOSER (widget);
612 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
617 x = (int) event->x + widget->allocation.x;
618 y = (int) event->y + widget->allocation.y;
620 if (x >= priv->spa.x && x <= priv->spa.x + priv->spa.width &&
621 y >= priv->spa.y && y <= priv->spa.y + priv->spa.height) {
623 tmp = y - priv->spa.y;
624 priv->currsat = tmp * 0xffff / priv->spa.height;
625 tmp = x - priv->spa.x;
626 priv->currval = tmp * 0xffff / priv->spa.width;
628 g_signal_emit (sel, color_chooser_signals[COLOR_CHANGED], 0);
629 gtk_widget_queue_draw (widget);
631 priv->mousestate = 1;
632 priv->mousein = TRUE;
634 gtk_grab_add(widget);
636 } else if (x >= priv->hba.x && x <= priv->hba.x + priv->hba.width &&
637 y >= priv->hba.y && y <= priv->hba.y + priv->hba.height) {
639 tmp = y - priv->hba.y;
640 priv->currhue = tmp * 0xffff / priv->hba.height;
642 g_signal_emit (sel, color_chooser_signals[COLOR_CHANGED], 0);
643 inline_limited_expose (sel);
645 priv->mousestate = 2;
646 priv->mousein = TRUE;
648 gtk_grab_add (widget);
655 hildon_color_chooser_button_release (GtkWidget *widget,
656 GdkEventButton *event)
658 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
662 if (priv->mousestate) {
663 gtk_grab_remove (widget);
666 priv->mousestate = 0;
667 priv->mousein = FALSE;
673 hildon_color_chooser_pointer_motion (GtkWidget *widget,
674 GdkEventMotion *event)
676 HildonColorChooser *sel = HILDON_COLOR_CHOOSER (widget);
677 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
679 GdkModifierType mods;
684 if (event->is_hint || (event->window != widget->window))
685 gdk_window_get_pointer (widget->window, &x, &y, &mods);
687 if (priv->mousestate == 1) {
688 if (x >= priv->spa.x && x <= priv->spa.x + priv->spa.width &&
689 y >= priv->spa.y && y <= priv->spa.y + priv->spa.height) {
691 priv->currsat = (((long)(y - priv->spa.y)) * 0xffff) / priv->spa.height;
692 priv->currval = (((long)(x - priv->spa.x)) * 0xffff) / priv->spa.width;
694 g_signal_emit (sel, color_chooser_signals[COLOR_CHANGED], 0);
695 gtk_widget_queue_draw(widget);
697 } else if (priv->mousein == TRUE) {
700 } else if (priv->mousestate == 2) {
701 if (x >= priv->hba.x && x <= priv->hba.x + priv->hba.width &&
702 y >= priv->hba.y && y <= priv->hba.y + priv->hba.height) {
703 tmp = y - priv->hba.y;
705 tmp /= priv->hba.height;
707 if(tmp != priv->currhue) {
710 g_signal_emit (sel, color_chooser_signals[COLOR_CHANGED], 0);
711 inline_limited_expose (sel);
714 } else if (priv->mousein == TRUE) {
722 get_border (GtkWidget *w,
726 GtkBorder *tb = NULL;
728 gtk_widget_style_get (w, name, &tb, NULL);
732 gtk_border_free (tb);
742 init_borders (GtkWidget *w,
748 get_border (w, "outer_border", outer);
750 gtk_widget_style_get (w, "inner_size", &tb, NULL);
762 if (inner->left < 2) inner->left = 2;
763 if (inner->right < 2) inner->right = 2;
764 if (inner->top < 2) inner->top = 2;
768 * hildon_color_chooser_set_color:
769 * @chooser: a #HildonColorChooser
770 * @color: a color to be set
772 * Sets the color selected in the widget.
773 * Will move the crosshair pointer to indicate the passed color.
776 hildon_color_chooser_set_color (HildonColorChooser *chooser,
779 unsigned short hue, sat, val;
780 unsigned long min, max;
781 signed long tmp, diff;
782 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (chooser);
787 min = MIN (MIN (color->red, color->green), color->blue);
788 max = MAX (MAX (color->red, color->green), color->blue);
793 if (val > 0 && diff != 0) {
794 sat = (diff * 0x0000ffff) / max;
796 if (color->red == max) {
797 tmp = (signed) color->green - (signed) color->blue;
804 } else if (color->green == max) {
805 hue = (((signed long) color->blue - (signed long)color->red) * 10922 / diff) + 21844;
807 hue = (((signed long) color->red -(signed long) color->green) * 10922 / diff) + 43688;
818 inline_limited_expose (chooser);
819 g_signal_emit (chooser, color_chooser_signals[COLOR_CHANGED], 0);
823 inline_h2rgb (unsigned short hue,
826 unsigned short hue_rotation, hue_value;
828 hue_rotation = hue / 10922;
829 hue_value = hue % 10922;
831 switch (hue_rotation) {
836 rgb[1] = hue_value * 6*256;
841 rgb[0] = FULL_COLOR - (hue_value * 6*256);
849 rgb[2] = hue_value * 6*256;
854 rgb[1] = FULL_COLOR - (hue_value * 6*256);
859 rgb[0] = hue_value * 6*256;
867 rgb[2] = FULL_COLOR - (hue_value * 6*256);
879 intern_h2rgb8 (unsigned short hue,
882 unsigned short hue_rotation, hue_value;
885 hue_rotation = hue / 42;
886 hue_value = hue % 42;
888 switch (hue_rotation) {
891 rgb[0] = FULL_COLOR8;
892 rgb[1] = hue_value * 6;
897 rgb[0] = FULL_COLOR8 - (hue_value * 6);
898 rgb[1] = FULL_COLOR8;
904 rgb[1] = FULL_COLOR8;
905 rgb[2] = hue_value * 6;
910 rgb[1] = FULL_COLOR8 - (hue_value * 6);
911 rgb[2] = FULL_COLOR8;
915 rgb[0] = hue_value * 6;
917 rgb[2] = FULL_COLOR8;
921 rgb[0] = FULL_COLOR8;
923 rgb[2] = FULL_COLOR8 - (hue_value * 6);
934 /* optimization: do not ask hue for each round but have bilinear vectors */
935 /* rethink: benefits from handling data 8 bit? (no shift round) */
937 inline_draw_hue_bar (GtkWidget *widget,
945 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (widget);
947 unsigned short hvec, hcurr;
948 unsigned char *buf, *ptr, tmp[3];
952 if (w <= 0 || h <= 0) {
956 buf = (unsigned char *) g_malloc (w * h * 3);
959 hcurr = hvec * (y - sy);
963 for (i = 0; i < h; i++) {
964 intern_h2rgb8 (hcurr, tmp);
966 for (j = 0; j < w; j++) {
977 gdk_draw_rgb_image (widget->parent->window,
978 widget->style->fg_gc[0],
981 GDK_RGB_DITHER_NONE, buf, w * 3);
983 tmpy = priv->hba.y + (priv->currhue * priv->hba.height / 0xffff);
984 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);
986 if ((((priv->currhue * priv->hba.height) & 0xffff) > 0x8000) && (tmpy < (priv->hba.y + priv->hba.height))) {
987 gdk_draw_line (widget->parent->window, widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
988 priv->hba.x, tmpy+1, priv->hba.x + priv->hba.width - 1, tmpy+1);
989 } else if (tmpy > priv->hba.y) {
990 gdk_draw_line(widget->parent->window, widget->style->fg_gc[GTK_WIDGET_STATE(widget)], priv->hba.x,
991 tmpy-1, priv->hba.x + priv->hba.width - 1, tmpy-1);
998 inline_draw_hue_bar_dimmed (GtkWidget *widget,
1006 HildonColorChooser *sel = HILDON_COLOR_CHOOSER (widget);
1007 HildonColorChooserPrivate *priv;
1009 if (w <= 0 || h <= 0) {
1013 priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (sel);
1016 /* We need to create (and cache) the pixbuf if we don't
1018 if (priv->dimmed_bar == NULL) {
1020 unsigned short hvec, hcurr, avg;
1021 unsigned char *buf, *ptr, tmp[3];
1022 buf = (unsigned char *) g_malloc (w * h * 3);
1025 hcurr = hvec * (y - sy);
1028 for (i = 0; i < h; i++) {
1029 intern_h2rgb8 (hcurr, tmp);
1031 for(j = 0; j < w; j++) {
1032 avg = ((unsigned short) tmp[0]*3 + (unsigned short) tmp[1]*2 + (unsigned short) tmp[2])/6;
1033 ptr[0] = ((((i % 2) + j) % 2) == 0) ? MIN ((avg * 0.7) + 180, 255) : MIN ((avg * 0.7) + 120, 255);
1034 ptr[1] = ((((i % 2) + j) % 2) == 0) ? MIN ((avg * 0.7) + 180, 255) : MIN ((avg * 0.7) + 120, 255);
1035 ptr[2] = ((((i % 2) + j) % 2) == 0) ? MIN ((avg * 0.7) + 180, 255) : MIN ((avg * 0.7) + 120, 255);
1042 priv->dimmed_bar = gdk_pixbuf_new_from_data (buf, GDK_COLORSPACE_RGB, FALSE, 8, w, h, w * 3, (gpointer) g_free, buf);
1045 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);
1049 inline_draw_crosshair (unsigned char *buf,
1057 /* bad "clipping", clip the loop to save cpu */
1058 for(i = 0; i < 8; i++) {
1059 for(j = 0; j < 8; j++) {
1060 sx = j + x; sy = i + y;
1062 if (sx >= 0 && sx < w && sy >= 0 && sy < h) {
1063 if (crosshair[j + 8*i]) {
1064 if (crosshair[j + 8*i] & 0x1) {
1065 buf[(sx)*3+(sy)*w*3+0] = 255;
1066 buf[(sx)*3+(sy)*w*3+1] = 255;
1067 buf[(sx)*3+(sy)*w*3+2] = 255;
1069 buf[(sx)*3+(sy)*w*3+0] = 0;
1070 buf[(sx)*3+(sy)*w*3+1] = 0;
1071 buf[(sx)*3+(sy)*w*3+2] = 0;
1080 inline_draw_sv_plane (HildonColorChooser *sel,
1086 GtkWidget *widget = GTK_WIDGET (sel);
1087 unsigned char *buf, *ptr;
1088 unsigned long rgbx[3] = { 0x00ffffff, 0x00ffffff, 0x00ffffff }, rgbtmp[3];
1089 signed long rgby[3];
1090 HildonColorChooserPrivate *priv;
1094 if (w <= 0 || h <= 0) {
1098 priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (sel);
1100 tmp = priv->spa.width * priv->spa.height;
1102 buf = (unsigned char *) g_malloc (w * h * 3);
1105 inline_h2rgb (priv->currhue, rgbtmp);
1107 rgby[0] = rgbtmp[0] - rgbx[0];
1108 rgby[1] = rgbtmp[1] - rgbx[1];
1109 rgby[2] = rgbtmp[2] - rgbx[2];
1111 rgbx[0] /= priv->spa.width;
1112 rgbx[1] /= priv->spa.width;
1113 rgbx[2] /= priv->spa.width;
1119 rgbx[0] += (y - priv->spa.y) * rgby[0];
1120 rgbx[1] += (y - priv->spa.y) * rgby[1];
1121 rgbx[2] += (y - priv->spa.y) * rgby[2];
1123 for(i = 0; i < h; i++) {
1124 rgbtmp[0] = rgbx[0] * (x - priv->spa.x);
1125 rgbtmp[1] = rgbx[1] * (x - priv->spa.x);
1126 rgbtmp[2] = rgbx[2] * (x - priv->spa.x);
1128 for(j = 0; j < w; j++) {
1129 ptr[0] = rgbtmp[0] >> 16;
1130 ptr[1] = rgbtmp[1] >> 16;
1131 ptr[2] = rgbtmp[2] >> 16;
1132 rgbtmp[0] += rgbx[0];
1133 rgbtmp[1] += rgbx[1];
1134 rgbtmp[2] += rgbx[2];
1143 inline_draw_crosshair (buf,
1144 (priv->spa.width * priv->currval / 0xffff) - x + priv->spa.x - 4,
1145 (priv->spa.height * priv->currsat / 0xffff) - y + priv->spa.y - 4,
1148 gdk_draw_rgb_image (widget->parent->window, widget->style->fg_gc[0], x, y, w, h, GDK_RGB_DITHER_NONE, buf, w * 3);
1153 inline_draw_sv_plane_dimmed (HildonColorChooser *sel,
1159 GtkWidget *widget = GTK_WIDGET (sel);
1160 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (sel);
1164 if (w <= 0 || h <= 0) {
1168 /* We need to create (and cache) the pixbuf if we don't
1170 if (priv->dimmed_plane == NULL) {
1171 unsigned char *buf, *ptr;
1172 unsigned long rgbx[3] = { 0x00ffffff, 0x00ffffff, 0x00ffffff }, rgbtmp[3];
1174 signed long rgby[3];
1175 int tmp = priv->spa.width * priv->spa.height, i, j;
1177 buf = (unsigned char *) g_malloc (w * h * 3);
1181 /* possibe optimization: as we are drawing grayscale plane, there might
1182 be some simpler algorithm to do this*/
1183 rgbtmp[0] = 0x00ffffff;
1184 rgbtmp[1] = 0x00000000;
1185 rgbtmp[2] = 0x00000000;
1187 rgby[0] = rgbtmp[0] - rgbx[0];
1188 rgby[1] = rgbtmp[1] - rgbx[1];
1189 rgby[2] = rgbtmp[2] - rgbx[2];
1191 rgbx[0] /= priv->spa.width;
1192 rgbx[1] /= priv->spa.width;
1193 rgbx[2] /= priv->spa.width;
1199 rgbx[0] += (y - priv->spa.y) * rgby[0];
1200 rgbx[1] += (y - priv->spa.y) * rgby[1];
1201 rgbx[2] += (y - priv->spa.y) * rgby[2];
1203 for(i = 0; i < h; i++) {
1204 rgbtmp[0] = rgbx[0] * (x - priv->spa.x);
1205 rgbtmp[1] = rgbx[1] * (x - priv->spa.x);
1206 rgbtmp[2] = rgbx[2] * (x - priv->spa.x);
1208 for(j = 0; j < w; j++) {
1209 avg = (rgbtmp[0] + rgbtmp[1] + rgbtmp[2])/3;
1211 ptr[0] = ((((i % 2) + j) % 2) == 0) ? MIN ((avg * 0.7) + 180, 255) : MIN ((avg * 0.7) + 120, 255);
1212 ptr[1] = ((((i % 2) + j) % 2) == 0) ? MIN ((avg * 0.7) + 180, 255) : MIN ((avg * 0.7) + 120, 255);
1213 ptr[2] = ((((i % 2) + j) % 2) == 0) ? MIN ((avg * 0.7) + 180, 255) : MIN ((avg * 0.7) + 120, 255);
1214 rgbtmp[0] += rgbx[0];
1215 rgbtmp[1] += rgbx[1];
1216 rgbtmp[2] += rgbx[2];
1225 priv->dimmed_plane = gdk_pixbuf_new_from_data (buf, GDK_COLORSPACE_RGB, FALSE, 8, w, h, w * 3, (gpointer) g_free, buf);
1228 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);
1233 hildon_color_chooser_expose_timer (gpointer data)
1235 HildonColorChooser *sel = HILDON_COLOR_CHOOSER (data);
1236 HildonColorChooserPrivate *priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (sel);
1240 if (priv->expose_info.expose_queued) {
1241 gtk_widget_queue_draw (GTK_WIDGET (data));
1248 * hildon_color_chooser_get_color:
1249 * @chooser: a #HildonColorChooser
1250 * @color: a color structure to fill with the currently selected color
1252 * Retrives the currently selected color in the chooser.
1256 hildon_color_chooser_get_color (HildonColorChooser *chooser,
1259 HildonColorChooserPrivate *priv;
1260 GdkVisual *system_visual = gdk_visual_get_system ();
1261 unsigned long rgb[3], rgb2[3];
1263 g_return_if_fail (HILDON_IS_COLOR_CHOOSER (chooser));
1264 g_return_if_fail (color != NULL);
1266 priv = HILDON_COLOR_CHOOSER_GET_PRIVATE (chooser);
1269 inline_h2rgb (priv->currhue, rgb);
1271 rgb2[0] = 0xffffff - rgb[0];
1272 rgb2[1] = 0xffffff - rgb[1];
1273 rgb2[2] = 0xffffff - rgb[2];
1275 color->red = ((rgb[0] >> 8) + ((rgb2[0] >> 8) * (0xffff - priv->currsat) / 0xffff)) * priv->currval / 0xffff;
1276 color->green = ((rgb[1] >> 8) + ((rgb2[1] >> 8) * (0xffff - priv->currsat) / 0xffff)) * priv->currval / 0xffff;
1277 color->blue = ((rgb[2] >> 8) + ((rgb2[2] >> 8) * (0xffff - priv->currsat) / 0xffff)) * priv->currval / 0xffff;
1279 color->pixel = ((color->red >> (16 - system_visual->red_prec)) << system_visual->red_shift) |
1280 ((color->green >> (16 - system_visual->green_prec)) << system_visual->green_shift) |
1281 ((color->blue >> (16 - system_visual->blue_prec)) << system_visual->blue_shift);
1285 hildon_color_chooser_new (void)
1287 return (GtkWidget *) g_object_new (HILDON_TYPE_COLOR_CHOOSER, NULL);
1291 hildon_color_chooser_set_property (GObject *object,
1293 const GValue *value,
1296 g_return_if_fail (HILDON_IS_COLOR_CHOOSER (object));
1302 GdkColor *color = g_value_get_boxed (value);
1303 hildon_color_chooser_set_color ((HildonColorChooser *) object, color);
1307 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1313 hildon_color_chooser_get_property (GObject *object,
1318 g_return_if_fail (HILDON_IS_COLOR_CHOOSER (object));
1325 hildon_color_chooser_get_color ((HildonColorChooser *) object, &color);
1326 g_value_set_boxed (value, &color);
1330 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);