1 #include <Elementary.h>
4 #define BASE_STEP 360.0
10 typedef enum _Button_State
17 typedef enum _Color_Type
25 typedef struct _Colorselector_Data Colorselector_Data;
26 struct _Colorselector_Data
29 Evas_Object *colorbar;
35 Evas_Object *touch_area;
36 Color_Type color_type;
37 Button_State button_state;
40 typedef struct _Widget_Data Widget_Data;
44 Colorselector_Data *cp[4];
45 Evas_Coord _x, _y, _w, _h;
51 Ecore_Timer *lp_timer;
52 Ecore_Timer *mv_timer;
55 static const char *widtype = NULL;
57 static void _del_hook(Evas_Object *obj);
58 static void _theme_hook(Evas_Object *obj);
59 static void _sizing_eval(Evas_Object *obj);
60 static void _rgb_to_hsl(void *data);
61 static void _hsl_to_rgb(void *data);
62 static void _color_with_saturation(void *data);
63 static void _color_with_lightness(void *data);
64 static void _draw_rects(void *data, double x);
65 static void _arrow_cb(void *data, Evas_Object *obj, const char *emission, const char *source);
66 static void _colorbar_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
67 static Eina_Bool _mv_timer(void *data);
68 static Eina_Bool _long_press_timer(void *data);
69 static void _left_button_down_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
70 static void _right_button_down_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
71 static void _left_button_up_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
72 static void _right_button_up_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
73 static void _add_colorbar(Evas_Object *obj);
74 static void _set_color(Evas_Object *obj, int r, int g, int b, int a);
76 static const char SIG_CHANGED[] = "changed";
78 static const Evas_Smart_Cb_Description _signals[] =
85 _del_hook(Evas_Object *obj)
87 Widget_Data *wd = elm_widget_data_get(obj);
91 if (wd->lp_timer) ecore_timer_del(wd->lp_timer);
92 if (wd->mv_timer) ecore_timer_del(wd->mv_timer);
93 for (i = 0; i < 4; i++) free(wd->cp[i]);
98 _theme_hook(Evas_Object *obj)
100 Widget_Data *wd = elm_widget_data_get(obj);
103 if ((!wd) || (!wd->base)) return;
105 _elm_theme_object_set(obj, wd->base, "colorselector", "bg",
106 elm_widget_style_get(obj));
108 for (i = 0; i < 4; i++)
110 evas_object_del(wd->cp[i]->colorbar);
111 wd->cp[i]->colorbar = NULL;
112 evas_object_del(wd->cp[i]->bar);
113 wd->cp[i]->bar = NULL;
114 evas_object_del(wd->cp[i]->lbt);
115 wd->cp[i]->lbt = NULL;
116 evas_object_del(wd->cp[i]->rbt);
117 wd->cp[i]->rbt = NULL;
120 evas_object_del(wd->cp[i]->bg_rect);
121 wd->cp[i]->bg_rect = NULL;
123 evas_object_del(wd->cp[i]->arrow);
124 wd->cp[i]->arrow = NULL;
125 evas_object_del(wd->cp[i]->touch_area);
126 wd->cp[i]->touch_area = NULL;
134 _colorselector_set_size_hints(Evas_Object *obj, int timesw, int timesh)
136 Evas_Coord minw = -1, minh = -1;
138 elm_coords_finger_size_adjust(timesw, &minw, timesh, &minh);
139 edje_object_size_min_restricted_calc(obj, &minw, &minh,
141 evas_object_size_hint_min_set(obj, minw, minh);
142 evas_object_size_hint_max_set(obj, -1, -1);
146 _sizing_eval(Evas_Object *obj)
148 Widget_Data *wd = elm_widget_data_get(obj);
149 Evas_Coord minw = -1, minh = -1;
153 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
154 for (i = 0; i < 4; i++)
156 if (wd->cp[i]->bg_rect)
157 _colorselector_set_size_hints(wd->cp[i]->bg_rect, 1, 1);
158 _colorselector_set_size_hints(wd->cp[i]->bar, 1, 1);
159 _colorselector_set_size_hints(wd->cp[i]->rbt, 1, 1);
160 _colorselector_set_size_hints(wd->cp[i]->lbt, 1, 1);
162 _colorselector_set_size_hints(wd->cp[i]->colorbar, 4, 1);
165 elm_coords_finger_size_adjust(4, &minw, 4, &minh);
166 edje_object_size_min_restricted_calc(wd->base, &minw, &minh, minw, minh);
167 evas_object_size_hint_min_set(obj, minw, minh);
168 evas_object_size_hint_max_set(obj, -1, -1);
172 _rgb_to_hsl(void *data)
174 Widget_Data *wd = data;
197 wd->l = (m + v) / 2.0;
199 if (wd->l <= 0.0) return;
204 if (wd->s > 0.0) wd->s /= (wd->l <= 0.5) ? (v + m) : (2.0 - v - m);
211 if (r == v) wd->h = (g == m ? 5.0 + b2 : 1.0 - g2);
212 else if (g == v) wd->h = (b == m ? 1.0 + r2 : 3.0 - b2);
213 else wd->h = (r == m ? 3.0 + g2 : 5.0 - r2);
219 _hsl_to_rgb(void *data)
221 Widget_Data *wd = data;
222 double r = 0, g = 0, b = 0;
225 double sv, vsf, f, p, q, t, v;
231 if (_s == 0.0) r = g = b = _l;
234 if (_h == 360.0) _h = 0.0;
237 v = (_l <= 0.5) ? (_l * (1.0 + _s)) : (_l + _s - (_l * _s));
240 if (v) sv = (v - p) / v;
285 i = (int)(r * 255.0);
287 wd->r = (f <= 0.5) ? i : (i + 1);
289 i = (int)(g * 255.0);
291 wd->g = (f <= 0.5) ? i : (i + 1);
293 i = (int)(b * 255.0);
295 wd->b = (f <= 0.5) ? i : (i + 1);
299 _color_with_saturation(void *data)
301 Widget_Data *wd = data;
304 wd->sr = 127 + (int)((double)(wd->er - 127) * wd->s);
306 wd->sr = 127 - (int)((double)(127 - wd->er) * wd->s);
309 wd->sg = 127 + (int)((double)(wd->eg - 127) * wd->s);
311 wd->sg = 127 - (int)((double)(127 - wd->eg) * wd->s);
314 wd->sb = 127 + (int)((double)(wd->eb - 127) * wd->s);
316 wd->sb = 127 - (int)((double)(127 - wd->eb) * wd->s);
320 _color_with_lightness(void *data)
322 Widget_Data *wd = data;
326 wd->lr = wd->er + (int)((double)(255 - wd->er) * (wd->l - 0.5) * 2.0);
327 wd->lg = wd->eg + (int)((double)(255 - wd->eg) * (wd->l - 0.5) * 2.0);
328 wd->lb = wd->eb + (int)((double)(255 - wd->eb) * (wd->l - 0.5) * 2.0);
330 else if (wd->l < 0.5)
332 wd->lr = (double)wd->er * wd->l * 2.0;
333 wd->lg = (double)wd->eg * wd->l * 2.0;
334 wd->lb = (double)wd->eb * wd->l * 2.0;
345 _draw_rects(void *data, double x)
347 Colorselector_Data *cp = data;
348 Widget_Data *wd = elm_widget_data_get(cp->parent);
349 double one_six = 1.0 / 6.0;
351 switch (cp->color_type)
359 wd->eg = (255.0 * x * 6.0);
362 else if (x < 2 * one_six)
364 wd->er = 255 - (int)(255.0 * (x - one_six) * 6.0);
368 else if (x < 3 * one_six)
372 wd->eb = (int)(255.0 * (x - (2.0 * one_six)) * 6.0);
374 else if (x < 4 * one_six)
377 wd->eg = 255 - (int)(255.0 * (x - (3.0 * one_six)) * 6.0);
380 else if (x < 5 * one_six)
382 wd->er = 255.0 * (x - (4.0 * one_six)) * 6.0;
390 wd->eb = 255 - (int)(255.0 * (x - (5.0 * one_six)) * 6.0);
393 evas_object_color_set(wd->cp[0]->arrow, wd->er, wd->eg, wd->eb, 255);
394 evas_object_color_set(wd->cp[1]->bg_rect, wd->er, wd->eg, wd->eb, 255);
395 evas_object_color_set(wd->cp[2]->bg_rect, wd->er, wd->eg, wd->eb, 255);
396 evas_object_color_set(wd->cp[3]->bar, wd->er, wd->eg, wd->eb, 255);
398 _color_with_saturation(wd);
399 evas_object_color_set(wd->cp[1]->arrow, wd->sr, wd->sg, wd->sb, 255);
401 _color_with_lightness(wd);
402 evas_object_color_set(wd->cp[2]->arrow, wd->lr, wd->lg, wd->lb, 255);
404 evas_object_color_set(wd->cp[3]->arrow,
405 (wd->er * wd->a) / 255,
406 (wd->eg * wd->a) / 255,
407 (wd->eb * wd->a) / 255,
412 _color_with_saturation(wd);
413 evas_object_color_set(wd->cp[1]->arrow, wd->sr, wd->sg, wd->sb, 255);
417 _color_with_lightness(wd);
418 evas_object_color_set(wd->cp[2]->arrow, wd->lr, wd->lg, wd->lb, 255);
422 evas_object_color_set(wd->cp[3]->arrow,
423 (wd->er * wd->a) / 255,
424 (wd->eg * wd->a) / 255,
425 (wd->eb * wd->a) / 255,
435 _arrow_cb(void *data, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
437 Colorselector_Data *cp = data;
440 edje_object_part_drag_value_get(obj, "elm.arrow", &x, &y);
441 _draw_rects(data, x);
442 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
446 _colorbar_cb(void *data, Evas *e, Evas_Object *obj __UNUSED__, void *event_info)
448 Colorselector_Data *cp = data;
449 Evas_Event_Mouse_Down *ev = event_info;
450 Evas_Coord x, y, w, h;
451 double arrow_x = 0, arrow_y;
453 evas_object_geometry_get(cp->bar, &x, &y, &w, &h);
454 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow",
456 if (w > 0) arrow_x = (double)(ev->canvas.x - x) / (double)w;
457 if (arrow_x > 1) arrow_x = 1;
458 if (arrow_x < 0) arrow_x = 0;
459 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", arrow_x, arrow_y);
460 _draw_rects(data, arrow_x);
461 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
462 evas_event_feed_mouse_cancel(e, 0, NULL);
463 evas_event_feed_mouse_down(e, 1, EVAS_BUTTON_NONE, 0, NULL);
467 _mv_timer(void *data)
469 Colorselector_Data *cp = data;
470 Widget_Data *wd = elm_widget_data_get(cp->parent);
473 if (!wd) return EINA_FALSE;
475 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
476 if (cp->button_state == L_BUTTON_PRESSED)
478 x -= 1.0 / BASE_STEP;
479 if (x < 0.0) x = 0.0;
480 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
481 _draw_rects(data, x);
482 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
485 else if (cp->button_state == R_BUTTON_PRESSED)
487 x += 1.0 / BASE_STEP;
488 if (x > 1.0) x = 1.0;
489 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
490 _draw_rects(data, x);
491 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
499 _long_press_timer(void *data)
501 Colorselector_Data *cp = data;
502 Widget_Data *wd = elm_widget_data_get(cp->parent);
504 if (wd->mv_timer) ecore_timer_del(wd->mv_timer);
505 wd->mv_timer = ecore_timer_add(0.01, _mv_timer, cp);
512 _left_button_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
514 Colorselector_Data *cp = data;
515 Widget_Data *wd = elm_widget_data_get(cp->parent);
518 edje_object_signal_emit(cp->lbt, "elm,state,left,button,down",
520 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
522 switch(cp->color_type)
540 if (x < 0.0) x = 0.0;
542 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
543 _draw_rects(data, x);
544 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
545 cp->button_state = L_BUTTON_PRESSED;
546 if (wd->lp_timer) ecore_timer_del(wd->lp_timer);
547 wd->lp_timer = ecore_timer_add(_elm_config->longpress_timeout, _long_press_timer, cp);
551 _right_button_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
553 Colorselector_Data *cp = data;
554 Widget_Data *wd = elm_widget_data_get(cp->parent);
557 edje_object_signal_emit(cp->rbt, "elm,state,right,button,down",
559 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
561 switch(cp->color_type)
579 if (x > 1.0) x = 1.0;
581 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
582 _draw_rects(data, x);
583 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
584 cp->button_state = R_BUTTON_PRESSED;
585 wd->lp_timer = ecore_timer_add(_elm_config->longpress_timeout, _long_press_timer, cp);
589 _left_button_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
591 Colorselector_Data *cp = data;
592 Widget_Data *wd = elm_widget_data_get(cp->parent);
596 ecore_timer_del(wd->lp_timer);
601 ecore_timer_del(wd->mv_timer);
605 cp->button_state = BUTTON_RELEASED;
606 edje_object_signal_emit(cp->lbt, "elm,state,left,button,up", "left_button");
610 _right_button_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
612 Colorselector_Data *cp = data;
613 Widget_Data *wd = elm_widget_data_get(cp->parent);
617 ecore_timer_del(wd->lp_timer);
622 ecore_timer_del(wd->mv_timer);
626 cp->button_state = BUTTON_RELEASED;
627 edje_object_signal_emit(cp->rbt, "elm,state,right,button,up",
632 _add_colorbar(Evas_Object *obj)
634 char colorbar_name[128];
635 char colorbar_s[128];
640 wd = elm_widget_data_get(obj);
643 e = evas_object_evas_get(obj);
645 for (i = 0; i < 4; i++)
647 wd->cp[i] = ELM_NEW(Colorselector_Data);
648 wd->cp[i]->parent = obj;
652 wd->cp[i]->color_type = HUE;
655 wd->cp[i]->color_type = SATURATION;
658 wd->cp[i]->color_type = LIGHTNESS;
661 wd->cp[i]->color_type = ALPHA;
666 /* load colorbar area */
667 wd->cp[i]->colorbar = edje_object_add(e);
668 _elm_theme_object_set(obj, wd->cp[i]->colorbar, "colorselector", "base",
670 snprintf(colorbar_name, sizeof(colorbar_name), "colorbar_%d", i);
671 snprintf(colorbar_s, sizeof(colorbar_s), "elm.colorbar_%d", i);
672 edje_object_signal_callback_add(wd->cp[i]->colorbar, "drag", "*",
673 _arrow_cb, wd->cp[i]);
674 edje_object_part_swallow(wd->base, colorbar_s, wd->cp[i]->colorbar);
675 elm_widget_sub_object_add(obj, wd->cp[i]->colorbar);
677 /* load colorbar image */
678 wd->cp[i]->bar = edje_object_add(e);
679 _elm_theme_object_set(obj, wd->cp[i]->bar, "colorselector", "image",
681 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar",
683 elm_widget_sub_object_add(obj, wd->cp[i]->bar);
685 /* provide expanded touch area */
686 wd->cp[i]->touch_area = evas_object_rectangle_add(e);
687 evas_object_color_set(wd->cp[i]->touch_area, 0, 0, 0, 0);
688 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.arrow_bg",
689 wd->cp[i]->touch_area);
690 evas_object_event_callback_add(wd->cp[i]->touch_area,
691 EVAS_CALLBACK_MOUSE_DOWN, _colorbar_cb,
693 elm_widget_sub_object_add(obj, wd->cp[i]->touch_area);
695 /* load background rectangle of the colorbar. used for
696 changing color of the opacity bar */
697 if ((i == 1) || (i == 2))
699 wd->cp[i]->bg_rect = evas_object_rectangle_add(e);
700 evas_object_color_set(wd->cp[i]->bg_rect, wd->er, wd->eg, wd->eb,
702 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar_bg",
705 elm_widget_sub_object_add(obj, wd->cp[i]->bg_rect);
709 wd->cp[i]->bg_rect = edje_object_add(e);
710 _elm_theme_object_set(obj, wd->cp[i]->bg_rect, "colorselector",
711 "bg_image", colorbar_name);
712 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar_bg",
714 elm_widget_sub_object_add(obj, wd->cp[i]->bg_rect);
715 evas_object_color_set(wd->cp[i]->bar, wd->er, wd->eg, wd->eb, 255);
717 /* load arrow image, pointing the colorbar */
718 wd->cp[i]->arrow = edje_object_add(e);
719 _elm_theme_object_set(obj, wd->cp[i]->arrow, "colorselector", "image",
721 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.arrow_icon",
723 elm_widget_sub_object_add(obj, wd->cp[i]->arrow);
725 evas_object_color_set(wd->cp[i]->arrow, 0, 0, 0, 255);
727 evas_object_color_set(wd->cp[i]->arrow, wd->er, wd->eg, wd->eb, 255);
729 /* load left button */
730 wd->cp[i]->lbt = edje_object_add(e);
731 _elm_theme_object_set(obj, wd->cp[i]->lbt, "colorselector", "button",
733 evas_object_event_callback_add(wd->cp[i]->lbt, EVAS_CALLBACK_MOUSE_DOWN,
734 _left_button_down_cb, wd->cp[i]);
735 evas_object_event_callback_add(wd->cp[i]->lbt, EVAS_CALLBACK_MOUSE_UP,
736 _left_button_up_cb, wd->cp[i]);
737 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.l_button",
739 elm_widget_sub_object_add(obj, wd->cp[i]->lbt);
741 /* load right button */
742 wd->cp[i]->rbt = edje_object_add(e);
743 _elm_theme_object_set(obj, wd->cp[i]->rbt, "colorselector", "button",
745 evas_object_event_callback_add(wd->cp[i]->rbt, EVAS_CALLBACK_MOUSE_DOWN,
746 _right_button_down_cb, wd->cp[i]);
747 evas_object_event_callback_add(wd->cp[i]->rbt, EVAS_CALLBACK_MOUSE_UP,
748 _right_button_up_cb, wd->cp[i]);
749 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.r_button",
751 elm_widget_sub_object_add(obj, wd->cp[i]->rbt);
756 _set_color(Evas_Object *obj, int r, int g, int b, int a)
758 Widget_Data *wd = elm_widget_data_get(obj);
768 edje_object_part_drag_value_get(wd->cp[0]->colorbar, "elm.arrow", &x, &y);
770 edje_object_part_drag_value_set(wd->cp[0]->colorbar, "elm.arrow", x, y);
771 _draw_rects(wd->cp[0], x);
773 edje_object_part_drag_value_get(wd->cp[1]->colorbar, "elm.arrow", &x, &y);
775 edje_object_part_drag_value_set(wd->cp[1]->colorbar, "elm.arrow", x, y);
776 _draw_rects(wd->cp[1], x);
778 edje_object_part_drag_value_get(wd->cp[2]->colorbar, "elm.arrow", &x, &y);
780 edje_object_part_drag_value_set(wd->cp[2]->colorbar, "elm.arrow", x, y);
781 _draw_rects(wd->cp[2], x);
783 edje_object_part_drag_value_get(wd->cp[3]->colorbar, "elm.arrow", &x, &y);
785 edje_object_part_drag_value_set(wd->cp[3]->colorbar, "elm.arrow", x, y);
786 _draw_rects(wd->cp[3], x);
790 elm_colorselector_add(Evas_Object *parent)
792 Evas_Object *obj = NULL;
793 Widget_Data *wd = NULL;
796 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
798 ELM_SET_WIDTYPE(widtype, "colorselector");
799 elm_widget_type_set(obj, "colorselector");
800 elm_widget_sub_object_add(parent, obj);
801 elm_widget_data_set(obj, wd);
802 elm_widget_del_hook_set(obj, _del_hook);
803 elm_widget_theme_hook_set(obj, _theme_hook);
805 /* load background edj */
806 wd->base = edje_object_add(e);
807 _elm_theme_object_set(obj, wd->base, "colorselector", "bg", "default");
808 elm_widget_resize_object_set(obj, wd->base);
822 evas_object_smart_callbacks_descriptions_set(obj, _signals);
827 elm_colorselector_color_set(Evas_Object *obj, int r, int g, int b, int a)
829 ELM_CHECK_WIDTYPE(obj, widtype);
830 _set_color(obj, r, g, b, a);
834 elm_colorselector_color_get(const Evas_Object *obj, int *r, int *g, int *b, int*a)
836 Widget_Data *wd = elm_widget_data_get(obj);
837 ELM_CHECK_WIDTYPE(obj, widtype);