1 #include <Elementary.h>
5 * @defgroup Colorselector Colorselector
8 * By using colorselector, you can select a color.
9 * Colorselector made a color using HSV/HSB mode.
11 * Smart Callbacks that you can add are :
13 * changed - This signal is emitted when a color change occurs.
17 #define BASE_STEP 360.0
18 #define HUE_STEP 360.0
19 #define SAT_STEP 128.0
20 #define LIG_STEP 256.0
21 #define ALP_STEP 256.0
23 typedef enum _Button_State
30 typedef enum _Color_Type
38 typedef struct _Colorselector_Data Colorselector_Data;
39 struct _Colorselector_Data
42 Evas_Object *colorbar;
48 Evas_Object *touch_area;
49 Color_Type color_type;
50 Button_State button_state;
53 typedef struct _Widget_Data Widget_Data;
57 Colorselector_Data *cp[4];
58 Evas_Coord _x, _y, _w, _h;
64 Ecore_Timer *lp_timer;
65 Ecore_Timer *mv_timer;
68 static const char *widtype = NULL;
70 static void _del_hook(Evas_Object *obj);
71 static void _theme_hook(Evas_Object *obj);
72 static void _sizing_eval(Evas_Object *obj);
73 static void _rgb_to_hsl(void *data);
74 static void _hsl_to_rgb(void *data);
75 static void _color_with_saturation(void *data);
76 static void _color_with_lightness(void *data);
77 static void _draw_rects(void *data, double x);
78 static void _arrow_cb(void *data, Evas_Object *obj, const char *emission, const char *source);
79 static void _colorbar_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
80 static Eina_Bool _mv_timer(void *data);
81 static Eina_Bool _long_press_timer(void *data);
82 static void _left_button_down_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
83 static void _right_button_down_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
84 static void _left_button_up_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
85 static void _right_button_up_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
86 static void _add_colorbar(Evas_Object *obj);
87 static void _set_color(Evas_Object *obj, int r, int g, int b, int a);
89 #define SIG_CHANGED "changed"
91 static const Evas_Smart_Cb_Description _signals[] =
98 _del_hook(Evas_Object *obj)
100 Widget_Data *wd = elm_widget_data_get(obj);
104 if (wd->lp_timer) ecore_timer_del(wd->lp_timer);
105 if (wd->mv_timer) ecore_timer_del(wd->mv_timer);
106 for (i = 0; i < 4; i++) free(wd->cp[i]);
111 _theme_hook(Evas_Object *obj)
113 Widget_Data *wd = elm_widget_data_get(obj);
116 if ((!wd) || (!wd->base)) return;
118 _elm_theme_object_set(obj, wd->base, "colorselector", "bg",
119 elm_widget_style_get(obj));
121 for (i = 0; i < 4; i++)
123 evas_object_del(wd->cp[i]->colorbar);
124 wd->cp[i]->colorbar = NULL;
125 evas_object_del(wd->cp[i]->bar);
126 wd->cp[i]->bar = NULL;
127 evas_object_del(wd->cp[i]->lbt);
128 wd->cp[i]->lbt = NULL;
129 evas_object_del(wd->cp[i]->rbt);
130 wd->cp[i]->rbt = NULL;
133 evas_object_del(wd->cp[i]->bg_rect);
134 wd->cp[i]->bg_rect = NULL;
136 evas_object_del(wd->cp[i]->arrow);
137 wd->cp[i]->arrow = NULL;
138 evas_object_del(wd->cp[i]->touch_area);
139 wd->cp[i]->touch_area = NULL;
147 _colorselector_set_size_hints(Evas_Object *obj, int timesw, int timesh)
149 Evas_Coord minw = -1, minh = -1;
151 elm_coords_finger_size_adjust(timesw, &minw, timesh, &minh);
152 edje_object_size_min_restricted_calc(obj, &minw, &minh,
154 evas_object_size_hint_min_set(obj, minw, minh);
155 evas_object_size_hint_max_set(obj, -1, -1);
159 _sizing_eval(Evas_Object *obj)
161 Widget_Data *wd = elm_widget_data_get(obj);
162 Evas_Coord minw = -1, minh = -1;
166 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
167 for (i = 0; i < 4; i++)
169 if (wd->cp[i]->bg_rect)
170 _colorselector_set_size_hints(wd->cp[i]->bg_rect, 1, 1);
171 _colorselector_set_size_hints(wd->cp[i]->bar, 1, 1);
172 _colorselector_set_size_hints(wd->cp[i]->rbt, 1, 1);
173 _colorselector_set_size_hints(wd->cp[i]->lbt, 1, 1);
175 _colorselector_set_size_hints(wd->cp[i]->colorbar, 4, 1);
179 elm_coords_finger_size_adjust(4, &minw, 4, &minh);
180 edje_object_size_min_restricted_calc(wd->base, &minw, &minh, minw, minh);
181 evas_object_size_hint_min_set(obj, minw, minh);
182 evas_object_size_hint_max_set(obj, -1, -1);
186 _rgb_to_hsl(void *data)
188 Widget_Data *wd = data;
211 wd->l = (m + v) / 2.0;
213 if (wd->l <= 0.0) return;
218 if (wd->s > 0.0) wd->s /= (wd->l <= 0.5) ? (v + m) : (2.0 - v - m);
225 if (r == v) wd->h = (g == m ? 5.0 + b2 : 1.0 - g2);
226 else if (g == v) wd->h = (b == m ? 1.0 + r2 : 3.0 - b2);
227 else wd->h = (r == m ? 3.0 + g2 : 5.0 - r2);
233 _hsl_to_rgb(void *data)
235 Widget_Data *wd = data;
236 double r = 0, g = 0, b = 0;
239 double sv, vsf, f, p, q, t, v;
245 if (_s == 0.0) r = g = b = _l;
248 if (_h == 360.0) _h = 0.0;
251 v = (_l <= 0.5) ? (_l * (1.0 + _s)) : (_l + _s - (_l * _s));
254 if (v) sv = (v - p) / v;
299 i = (int)(r * 255.0);
301 wd->r = (f <= 0.5) ? i : (i + 1);
303 i = (int)(g * 255.0);
305 wd->g = (f <= 0.5) ? i : (i + 1);
307 i = (int)(b * 255.0);
309 wd->b = (f <= 0.5) ? i : (i + 1);
313 _color_with_saturation(void *data)
315 Widget_Data *wd = data;
317 if (wd->er > 127) wd->sr = 127 + (int)((double)(wd->er - 127) * wd->s);
318 else wd->sr = 127 - (int)((double)(127 - wd->er) * wd->s);
319 if (wd->eg > 127) wd->sg = 127 + (int)((double)(wd->eg - 127) * wd->s);
320 else wd->sg = 127 - (int)((double)(127 - wd->eg) * wd->s);
321 if (wd->eb > 127) wd->sb = 127 + (int)((double)(wd->eb - 127) * wd->s);
322 else wd->sb = 127 - (int)((double)(127 - wd->eb) * wd->s);
326 _color_with_lightness(void *data)
328 Widget_Data *wd = data;
332 wd->lr = wd->er + (int)((double)(255 - wd->er) * (wd->l - 0.5) * 2.0);
333 wd->lg = wd->eg + (int)((double)(255 - wd->eg) * (wd->l - 0.5) * 2.0);
334 wd->lb = wd->eb + (int)((double)(255 - wd->eb) * (wd->l - 0.5) * 2.0);
336 else if (wd->l < 0.5)
338 wd->lr = (double)wd->er * wd->l * 2.0;
339 wd->lg = (double)wd->eg * wd->l * 2.0;
340 wd->lb = (double)wd->eb * wd->l * 2.0;
351 _draw_rects(void *data, double x)
353 Colorselector_Data *cp = data;
354 Widget_Data *wd = elm_widget_data_get(cp->parent);
355 double one_six = 1.0 / 6.0;
357 switch (cp->color_type)
365 wd->eg = (255.0 * x * 6.0);
368 else if (x < 2 * one_six)
370 wd->er = 255 - (int)(255.0 * (x - one_six) * 6.0);
374 else if (x < 3 * one_six)
378 wd->eb = (int)(255.0 * (x - (2.0 * one_six)) * 6.0);
380 else if (x < 4 * one_six)
383 wd->eg = 255 - (int)(255.0 * (x - (3.0 * one_six)) * 6.0);
386 else if (x < 5 * one_six)
388 wd->er = 255.0 * (x - (4.0 * one_six)) * 6.0;
396 wd->eb = 255 - (int)(255.0 * (x - (5.0 * one_six)) * 6.0);
399 evas_object_color_set(wd->cp[0]->arrow, wd->er, wd->eg, wd->eb, 255);
400 evas_object_color_set(wd->cp[1]->bg_rect, wd->er, wd->eg, wd->eb, 255);
401 evas_object_color_set(wd->cp[2]->bg_rect, wd->er, wd->eg, wd->eb, 255);
402 evas_object_color_set(wd->cp[3]->bar, wd->er, wd->eg, wd->eb, 255);
404 _color_with_saturation(wd);
405 evas_object_color_set(wd->cp[1]->arrow, wd->sr, wd->sg, wd->sb, 255);
407 _color_with_lightness(wd);
408 evas_object_color_set(wd->cp[2]->arrow, wd->lr, wd->lg, wd->lb, 255);
410 evas_object_color_set(wd->cp[3]->arrow,
411 (wd->er * wd->a) / 255,
412 (wd->eg * wd->a) / 255,
413 (wd->eb * wd->a) / 255,
419 _color_with_saturation(wd);
420 evas_object_color_set(wd->cp[1]->arrow, wd->sr, wd->sg, wd->sb, 255);
425 _color_with_lightness(wd);
426 evas_object_color_set(wd->cp[2]->arrow, wd->lr, wd->lg, wd->lb, 255);
431 evas_object_color_set(wd->cp[3]->arrow, wd->er, wd->eg, wd->eb, wd->a);
441 _arrow_cb(void *data, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
443 Colorselector_Data *cp = data;
446 edje_object_part_drag_value_get(obj, "elm.arrow", &x, &y);
447 _draw_rects(data, x);
448 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
452 _colorbar_cb(void *data, Evas *e, Evas_Object *obj __UNUSED__, void *event_info)
454 Colorselector_Data *cp = data;
455 Evas_Event_Mouse_Down *ev = event_info;
456 Evas_Coord x, y, w, h;
457 double arrow_x = 0, arrow_y;
459 evas_object_geometry_get(cp->bar, &x, &y, &w, &h);
460 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow",
462 if (w > 0) arrow_x = (double)(ev->output.x - x) / (double)w;
463 if (arrow_x > 1) arrow_x = 1;
464 if (arrow_x < 0) arrow_x = 0;
465 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", arrow_x, arrow_y);
466 _draw_rects(data, arrow_x);
467 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
468 evas_event_feed_mouse_cancel(e, 0, NULL);
469 evas_event_feed_mouse_down(e, 1, EVAS_BUTTON_NONE, 0, NULL);
473 _mv_timer(void *data)
475 Colorselector_Data *cp = data;
476 Widget_Data *wd = elm_widget_data_get(cp->parent);
479 if (!wd) return EINA_FALSE;
481 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
482 if (cp->button_state == L_BUTTON_PRESSED)
484 x -= 1.0 / BASE_STEP;
485 if (x < 0.0) x = 0.0;
486 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
487 _draw_rects(data, x);
488 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
491 else if (cp->button_state == R_BUTTON_PRESSED)
493 x += 1.0 / BASE_STEP;
494 if (x > 1.0) x = 1.0;
495 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
496 _draw_rects(data, x);
497 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
505 _long_press_timer(void *data)
507 Colorselector_Data *cp = data;
508 Widget_Data *wd = elm_widget_data_get(cp->parent);
510 if (wd->mv_timer) ecore_timer_del(wd->mv_timer);
511 wd->mv_timer = ecore_timer_add(0.01, _mv_timer, cp);
518 _left_button_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
520 Colorselector_Data *cp = data;
521 Widget_Data *wd = elm_widget_data_get(cp->parent);
524 edje_object_signal_emit(cp->lbt, "elm,state,left,button,down",
526 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
528 switch(cp->color_type)
546 if (x < 0.0) x = 0.0;
548 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
549 _draw_rects(data, x);
550 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
551 cp->button_state = L_BUTTON_PRESSED;
552 if (wd->lp_timer) ecore_timer_del(wd->lp_timer);
553 wd->lp_timer = ecore_timer_add(_elm_config->longpress_timeout, _long_press_timer, cp);
557 _right_button_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
559 Colorselector_Data *cp = data;
560 Widget_Data *wd = elm_widget_data_get(cp->parent);
563 edje_object_signal_emit(cp->rbt, "elm,state,right,button,down",
565 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
567 switch(cp->color_type)
585 if (x > 1.0) x = 1.0;
587 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
588 _draw_rects(data, x);
589 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
590 cp->button_state = R_BUTTON_PRESSED;
591 wd->lp_timer = ecore_timer_add(_elm_config->longpress_timeout, _long_press_timer, cp);
595 _left_button_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
597 Colorselector_Data *cp = data;
598 Widget_Data *wd = elm_widget_data_get(cp->parent);
602 ecore_timer_del(wd->lp_timer);
607 ecore_timer_del(wd->mv_timer);
611 cp->button_state = BUTTON_RELEASED;
612 edje_object_signal_emit(cp->lbt, "elm,state,left,button,up", "left_button");
616 _right_button_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
618 Colorselector_Data *cp = data;
619 Widget_Data *wd = elm_widget_data_get(cp->parent);
623 ecore_timer_del(wd->lp_timer);
628 ecore_timer_del(wd->mv_timer);
632 cp->button_state = BUTTON_RELEASED;
633 edje_object_signal_emit(cp->rbt, "elm,state,right,button,up",
638 _add_colorbar(Evas_Object *obj)
640 char colorbar_name[128];
641 char colorbar_s[128];
646 wd = elm_widget_data_get(obj);
649 e = evas_object_evas_get(obj);
651 for (i = 0; i < 4; i++)
653 wd->cp[i] = ELM_NEW(Colorselector_Data);
654 wd->cp[i]->parent = obj;
658 wd->cp[i]->color_type = HUE;
661 wd->cp[i]->color_type = SATURATION;
664 wd->cp[i]->color_type = LIGHTNESS;
667 wd->cp[i]->color_type = ALPHA;
672 /* load colorbar area */
673 wd->cp[i]->colorbar = edje_object_add(e);
674 _elm_theme_object_set(obj, wd->cp[i]->colorbar, "colorselector", "base",
676 snprintf(colorbar_name, sizeof(colorbar_name), "colorbar_%d", i);
677 snprintf(colorbar_s, sizeof(colorbar_s), "elm.colorbar_%d", i);
678 edje_object_signal_callback_add(wd->cp[i]->colorbar, "drag", "*",
679 _arrow_cb, wd->cp[i]);
680 edje_object_part_swallow(wd->base, colorbar_s, wd->cp[i]->colorbar);
681 elm_widget_sub_object_add(obj, wd->cp[i]->colorbar);
683 /* load colorbar image */
684 wd->cp[i]->bar = edje_object_add(e);
685 _elm_theme_object_set(obj, wd->cp[i]->bar, "colorselector", "image",
687 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar",
689 elm_widget_sub_object_add(obj, wd->cp[i]->bar);
691 /* provide expanded touch area */
692 wd->cp[i]->touch_area = evas_object_rectangle_add(e);
693 evas_object_color_set(wd->cp[i]->touch_area, 0, 0, 0, 0);
694 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.arrow_bg",
695 wd->cp[i]->touch_area);
696 evas_object_event_callback_add(wd->cp[i]->touch_area,
697 EVAS_CALLBACK_MOUSE_DOWN, _colorbar_cb,
699 elm_widget_sub_object_add(obj, wd->cp[i]->touch_area);
701 /* load background rectangle of the colorbar. used for
702 changing color of the opacity bar */
703 if ((i == 1) || (i == 2))
705 wd->cp[i]->bg_rect = evas_object_rectangle_add(e);
706 evas_object_color_set(wd->cp[i]->bg_rect, wd->er, wd->eg, wd->eb,
708 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar_bg",
711 elm_widget_sub_object_add(obj, wd->cp[i]->bg_rect);
715 wd->cp[i]->bg_rect = edje_object_add(e);
716 _elm_theme_object_set(obj, wd->cp[i]->bg_rect, "colorselector",
717 "bg_image", colorbar_name);
718 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar_bg",
720 elm_widget_sub_object_add(obj, wd->cp[i]->bg_rect);
721 evas_object_color_set(wd->cp[i]->bar, wd->er, wd->eg, wd->eb, 255);
723 /* load arrow image, pointing the colorbar */
724 wd->cp[i]->arrow = edje_object_add(e);
725 _elm_theme_object_set(obj, wd->cp[i]->arrow, "colorselector", "image",
727 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.arrow_icon",
729 elm_widget_sub_object_add(obj, wd->cp[i]->arrow);
731 evas_object_color_set(wd->cp[i]->arrow, 0, 0, 0, 255);
733 evas_object_color_set(wd->cp[i]->arrow, wd->er, wd->eg, wd->eb, 255);
735 /* load left button */
736 wd->cp[i]->lbt = edje_object_add(e);
737 _elm_theme_object_set(obj, wd->cp[i]->lbt, "colorselector", "button",
739 evas_object_event_callback_add(wd->cp[i]->lbt, EVAS_CALLBACK_MOUSE_DOWN,
740 _left_button_down_cb, wd->cp[i]);
741 evas_object_event_callback_add(wd->cp[i]->lbt, EVAS_CALLBACK_MOUSE_UP,
742 _left_button_up_cb, wd->cp[i]);
743 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.l_button",
745 elm_widget_sub_object_add(obj, wd->cp[i]->lbt);
747 /* load right button */
748 wd->cp[i]->rbt = edje_object_add(e);
749 _elm_theme_object_set(obj, wd->cp[i]->rbt, "colorselector", "button",
751 evas_object_event_callback_add(wd->cp[i]->rbt, EVAS_CALLBACK_MOUSE_DOWN,
752 _right_button_down_cb, wd->cp[i]);
753 evas_object_event_callback_add(wd->cp[i]->rbt, EVAS_CALLBACK_MOUSE_UP,
754 _right_button_up_cb, wd->cp[i]);
755 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.r_button",
757 elm_widget_sub_object_add(obj, wd->cp[i]->rbt);
763 _set_color(Evas_Object *obj, int r, int g, int b, int a)
765 Widget_Data *wd = elm_widget_data_get(obj);
775 edje_object_part_drag_value_get(wd->cp[0]->colorbar, "elm.arrow", &x, &y);
777 edje_object_part_drag_value_set(wd->cp[0]->colorbar, "elm.arrow", x, y);
778 _draw_rects(wd->cp[0], x);
780 edje_object_part_drag_value_get(wd->cp[1]->colorbar, "elm.arrow", &x, &y);
782 edje_object_part_drag_value_set(wd->cp[1]->colorbar, "elm.arrow", x, y);
783 _draw_rects(wd->cp[1], x);
785 edje_object_part_drag_value_get(wd->cp[2]->colorbar, "elm.arrow", &x, &y);
787 edje_object_part_drag_value_set(wd->cp[2]->colorbar, "elm.arrow", x, y);
788 _draw_rects(wd->cp[2], x);
790 edje_object_part_drag_value_get(wd->cp[3]->colorbar, "elm.arrow", &x, &y);
792 edje_object_part_drag_value_set(wd->cp[3]->colorbar, "elm.arrow", x, y);
793 _draw_rects(wd->cp[3], x);
797 * Add a new colorselector to the parent
799 * @param parent The parent object
800 * @return The new object or NULL if it cannot be created
802 * @ingroup Colorselector
805 elm_colorselector_add(Evas_Object *parent)
807 Evas_Object *obj = NULL;
808 Widget_Data *wd = NULL;
811 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
813 wd = ELM_NEW(Widget_Data);
814 e = evas_object_evas_get(parent);
816 obj = elm_widget_add(e);
817 ELM_SET_WIDTYPE(widtype, "colorselector");
818 elm_widget_type_set(obj, "colorselector");
819 elm_widget_sub_object_add(parent, obj);
820 elm_widget_data_set(obj, wd);
821 elm_widget_del_hook_set(obj, _del_hook);
822 elm_widget_theme_hook_set(obj, _theme_hook);
824 /* load background edj */
825 wd->base = edje_object_add(e);
826 _elm_theme_object_set(obj, wd->base, "colorselector", "bg", "default");
827 elm_widget_resize_object_set(obj, wd->base);
841 evas_object_smart_callbacks_descriptions_set(obj, _signals);
846 * Set a color for the colorselector
848 * @param obj Colorselector object
849 * @param r r-value of color
850 * @param g g-value of color
851 * @param b b-value of color
852 * @param a a-value of color
854 * @ingroup Colorselector
857 elm_colorselector_color_set(Evas_Object *obj, int r, int g, int b, int a)
859 ELM_CHECK_WIDTYPE(obj, widtype);
860 _set_color(obj, r, g, b, a);
864 * Get a color from the colorselector
866 * @param obj Colorselector object
867 * @param r integer pointer for r-value of color
868 * @param g integer pointer for g-value of color
869 * @param b integer pointer for b-value of color
870 * @param a integer pointer for a-value of color
872 * @ingroup Colorselector
875 elm_colorselector_color_get(const Evas_Object *obj, int *r, int *g, int *b, int*a)
877 Widget_Data *wd = elm_widget_data_get(obj);
878 ELM_CHECK_WIDTYPE(obj, widtype);