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 * Signals that you can add callbacks for are:
13 * "changed" - when the color value changes
16 #define BASE_STEP 360.0
17 #define HUE_STEP 360.0
18 #define SAT_STEP 128.0
19 #define LIG_STEP 256.0
20 #define ALP_STEP 256.0
22 typedef enum _Button_State
29 typedef enum _Color_Type
37 typedef struct _Colorselector_Data Colorselector_Data;
38 struct _Colorselector_Data
41 Evas_Object *colorbar;
47 Evas_Object *touch_area;
48 Color_Type color_type;
49 Button_State button_state;
52 typedef struct _Widget_Data Widget_Data;
56 Colorselector_Data *cp[4];
57 Evas_Coord _x, _y, _w, _h;
63 Ecore_Timer *lp_timer;
64 Ecore_Timer *mv_timer;
67 static const char *widtype = NULL;
69 static void _del_hook(Evas_Object *obj);
70 static void _theme_hook(Evas_Object *obj);
71 static void _sizing_eval(Evas_Object *obj);
72 static void _rgb_to_hsl(void *data);
73 static void _hsl_to_rgb(void *data);
74 static void _color_with_saturation(void *data);
75 static void _color_with_lightness(void *data);
76 static void _draw_rects(void *data, double x);
77 static void _arrow_cb(void *data, Evas_Object *obj, const char *emission, const char *source);
78 static void _colorbar_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
79 static Eina_Bool _mv_timer(void *data);
80 static Eina_Bool _long_press_timer(void *data);
81 static void _left_button_down_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
82 static void _right_button_down_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
83 static void _left_button_up_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
84 static void _right_button_up_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
85 static void _add_colorbar(Evas_Object *obj);
86 static void _set_color(Evas_Object *obj, int r, int g, int b, int a);
88 #define SIG_CHANGED "changed"
90 static const Evas_Smart_Cb_Description _signals[] =
97 _del_hook(Evas_Object *obj)
99 Widget_Data *wd = elm_widget_data_get(obj);
103 if (wd->lp_timer) ecore_timer_del(wd->lp_timer);
104 if (wd->mv_timer) ecore_timer_del(wd->mv_timer);
105 for (i = 0; i < 4; i++) free(wd->cp[i]);
110 _theme_hook(Evas_Object *obj)
112 Widget_Data *wd = elm_widget_data_get(obj);
115 if ((!wd) || (!wd->base)) return;
117 _elm_theme_object_set(obj, wd->base, "colorselector", "bg",
118 elm_widget_style_get(obj));
120 for (i = 0; i < 4; i++)
122 evas_object_del(wd->cp[i]->colorbar);
123 wd->cp[i]->colorbar = NULL;
124 evas_object_del(wd->cp[i]->bar);
125 wd->cp[i]->bar = NULL;
126 evas_object_del(wd->cp[i]->lbt);
127 wd->cp[i]->lbt = NULL;
128 evas_object_del(wd->cp[i]->rbt);
129 wd->cp[i]->rbt = NULL;
132 evas_object_del(wd->cp[i]->bg_rect);
133 wd->cp[i]->bg_rect = NULL;
135 evas_object_del(wd->cp[i]->arrow);
136 wd->cp[i]->arrow = NULL;
137 evas_object_del(wd->cp[i]->touch_area);
138 wd->cp[i]->touch_area = NULL;
146 _colorselector_set_size_hints(Evas_Object *obj, int timesw, int timesh)
148 Evas_Coord minw = -1, minh = -1;
150 elm_coords_finger_size_adjust(timesw, &minw, timesh, &minh);
151 edje_object_size_min_restricted_calc(obj, &minw, &minh,
153 evas_object_size_hint_min_set(obj, minw, minh);
154 evas_object_size_hint_max_set(obj, -1, -1);
158 _sizing_eval(Evas_Object *obj)
160 Widget_Data *wd = elm_widget_data_get(obj);
161 Evas_Coord minw = -1, minh = -1;
165 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
166 for (i = 0; i < 4; i++)
168 if (wd->cp[i]->bg_rect)
169 _colorselector_set_size_hints(wd->cp[i]->bg_rect, 1, 1);
170 _colorselector_set_size_hints(wd->cp[i]->bar, 1, 1);
171 _colorselector_set_size_hints(wd->cp[i]->rbt, 1, 1);
172 _colorselector_set_size_hints(wd->cp[i]->lbt, 1, 1);
174 _colorselector_set_size_hints(wd->cp[i]->colorbar, 4, 1);
177 elm_coords_finger_size_adjust(4, &minw, 4, &minh);
178 edje_object_size_min_restricted_calc(wd->base, &minw, &minh, minw, minh);
179 evas_object_size_hint_min_set(obj, minw, minh);
180 evas_object_size_hint_max_set(obj, -1, -1);
184 _rgb_to_hsl(void *data)
186 Widget_Data *wd = data;
209 wd->l = (m + v) / 2.0;
211 if (wd->l <= 0.0) return;
216 if (wd->s > 0.0) wd->s /= (wd->l <= 0.5) ? (v + m) : (2.0 - v - m);
223 if (r == v) wd->h = (g == m ? 5.0 + b2 : 1.0 - g2);
224 else if (g == v) wd->h = (b == m ? 1.0 + r2 : 3.0 - b2);
225 else wd->h = (r == m ? 3.0 + g2 : 5.0 - r2);
231 _hsl_to_rgb(void *data)
233 Widget_Data *wd = data;
234 double r = 0, g = 0, b = 0;
237 double sv, vsf, f, p, q, t, v;
243 if (_s == 0.0) r = g = b = _l;
246 if (_h == 360.0) _h = 0.0;
249 v = (_l <= 0.5) ? (_l * (1.0 + _s)) : (_l + _s - (_l * _s));
252 if (v) sv = (v - p) / v;
297 i = (int)(r * 255.0);
299 wd->r = (f <= 0.5) ? i : (i + 1);
301 i = (int)(g * 255.0);
303 wd->g = (f <= 0.5) ? i : (i + 1);
305 i = (int)(b * 255.0);
307 wd->b = (f <= 0.5) ? i : (i + 1);
311 _color_with_saturation(void *data)
313 Widget_Data *wd = data;
316 wd->sr = 127 + (int)((double)(wd->er - 127) * wd->s);
318 wd->sr = 127 - (int)((double)(127 - wd->er) * wd->s);
321 wd->sg = 127 + (int)((double)(wd->eg - 127) * wd->s);
323 wd->sg = 127 - (int)((double)(127 - wd->eg) * wd->s);
326 wd->sb = 127 + (int)((double)(wd->eb - 127) * wd->s);
328 wd->sb = 127 - (int)((double)(127 - wd->eb) * wd->s);
332 _color_with_lightness(void *data)
334 Widget_Data *wd = data;
338 wd->lr = wd->er + (int)((double)(255 - wd->er) * (wd->l - 0.5) * 2.0);
339 wd->lg = wd->eg + (int)((double)(255 - wd->eg) * (wd->l - 0.5) * 2.0);
340 wd->lb = wd->eb + (int)((double)(255 - wd->eb) * (wd->l - 0.5) * 2.0);
342 else if (wd->l < 0.5)
344 wd->lr = (double)wd->er * wd->l * 2.0;
345 wd->lg = (double)wd->eg * wd->l * 2.0;
346 wd->lb = (double)wd->eb * wd->l * 2.0;
357 _draw_rects(void *data, double x)
359 Colorselector_Data *cp = data;
360 Widget_Data *wd = elm_widget_data_get(cp->parent);
361 double one_six = 1.0 / 6.0;
363 switch (cp->color_type)
371 wd->eg = (255.0 * x * 6.0);
374 else if (x < 2 * one_six)
376 wd->er = 255 - (int)(255.0 * (x - one_six) * 6.0);
380 else if (x < 3 * one_six)
384 wd->eb = (int)(255.0 * (x - (2.0 * one_six)) * 6.0);
386 else if (x < 4 * one_six)
389 wd->eg = 255 - (int)(255.0 * (x - (3.0 * one_six)) * 6.0);
392 else if (x < 5 * one_six)
394 wd->er = 255.0 * (x - (4.0 * one_six)) * 6.0;
402 wd->eb = 255 - (int)(255.0 * (x - (5.0 * one_six)) * 6.0);
405 evas_object_color_set(wd->cp[0]->arrow, wd->er, wd->eg, wd->eb, 255);
406 evas_object_color_set(wd->cp[1]->bg_rect, wd->er, wd->eg, wd->eb, 255);
407 evas_object_color_set(wd->cp[2]->bg_rect, wd->er, wd->eg, wd->eb, 255);
408 evas_object_color_set(wd->cp[3]->bar, wd->er, wd->eg, wd->eb, 255);
410 _color_with_saturation(wd);
411 evas_object_color_set(wd->cp[1]->arrow, wd->sr, wd->sg, wd->sb, 255);
413 _color_with_lightness(wd);
414 evas_object_color_set(wd->cp[2]->arrow, wd->lr, wd->lg, wd->lb, 255);
416 evas_object_color_set(wd->cp[3]->arrow,
417 (wd->er * wd->a) / 255,
418 (wd->eg * wd->a) / 255,
419 (wd->eb * wd->a) / 255,
424 _color_with_saturation(wd);
425 evas_object_color_set(wd->cp[1]->arrow, wd->sr, wd->sg, wd->sb, 255);
429 _color_with_lightness(wd);
430 evas_object_color_set(wd->cp[2]->arrow, wd->lr, wd->lg, wd->lb, 255);
434 evas_object_color_set(wd->cp[3]->arrow, wd->er, wd->eg, wd->eb, wd->a);
443 _arrow_cb(void *data, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
445 Colorselector_Data *cp = data;
448 edje_object_part_drag_value_get(obj, "elm.arrow", &x, &y);
449 _draw_rects(data, x);
450 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
454 _colorbar_cb(void *data, Evas *e, Evas_Object *obj __UNUSED__, void *event_info)
456 Colorselector_Data *cp = data;
457 Evas_Event_Mouse_Down *ev = event_info;
458 Evas_Coord x, y, w, h;
459 double arrow_x = 0, arrow_y;
461 evas_object_geometry_get(cp->bar, &x, &y, &w, &h);
462 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow",
464 if (w > 0) arrow_x = (double)(ev->output.x - x) / (double)w;
465 if (arrow_x > 1) arrow_x = 1;
466 if (arrow_x < 0) arrow_x = 0;
467 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", arrow_x, arrow_y);
468 _draw_rects(data, arrow_x);
469 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
470 evas_event_feed_mouse_cancel(e, 0, NULL);
471 evas_event_feed_mouse_down(e, 1, EVAS_BUTTON_NONE, 0, NULL);
475 _mv_timer(void *data)
477 Colorselector_Data *cp = data;
478 Widget_Data *wd = elm_widget_data_get(cp->parent);
481 if (!wd) return EINA_FALSE;
483 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
484 if (cp->button_state == L_BUTTON_PRESSED)
486 x -= 1.0 / BASE_STEP;
487 if (x < 0.0) x = 0.0;
488 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
489 _draw_rects(data, x);
490 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
493 else if (cp->button_state == R_BUTTON_PRESSED)
495 x += 1.0 / BASE_STEP;
496 if (x > 1.0) x = 1.0;
497 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
498 _draw_rects(data, x);
499 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
507 _long_press_timer(void *data)
509 Colorselector_Data *cp = data;
510 Widget_Data *wd = elm_widget_data_get(cp->parent);
512 if (wd->mv_timer) ecore_timer_del(wd->mv_timer);
513 wd->mv_timer = ecore_timer_add(0.01, _mv_timer, cp);
520 _left_button_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
522 Colorselector_Data *cp = data;
523 Widget_Data *wd = elm_widget_data_get(cp->parent);
526 edje_object_signal_emit(cp->lbt, "elm,state,left,button,down",
528 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
530 switch(cp->color_type)
548 if (x < 0.0) x = 0.0;
550 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
551 _draw_rects(data, x);
552 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
553 cp->button_state = L_BUTTON_PRESSED;
554 if (wd->lp_timer) ecore_timer_del(wd->lp_timer);
555 wd->lp_timer = ecore_timer_add(_elm_config->longpress_timeout, _long_press_timer, cp);
559 _right_button_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
561 Colorselector_Data *cp = data;
562 Widget_Data *wd = elm_widget_data_get(cp->parent);
565 edje_object_signal_emit(cp->rbt, "elm,state,right,button,down",
567 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
569 switch(cp->color_type)
587 if (x > 1.0) x = 1.0;
589 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
590 _draw_rects(data, x);
591 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
592 cp->button_state = R_BUTTON_PRESSED;
593 wd->lp_timer = ecore_timer_add(_elm_config->longpress_timeout, _long_press_timer, cp);
597 _left_button_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
599 Colorselector_Data *cp = data;
600 Widget_Data *wd = elm_widget_data_get(cp->parent);
604 ecore_timer_del(wd->lp_timer);
609 ecore_timer_del(wd->mv_timer);
613 cp->button_state = BUTTON_RELEASED;
614 edje_object_signal_emit(cp->lbt, "elm,state,left,button,up", "left_button");
618 _right_button_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
620 Colorselector_Data *cp = data;
621 Widget_Data *wd = elm_widget_data_get(cp->parent);
625 ecore_timer_del(wd->lp_timer);
630 ecore_timer_del(wd->mv_timer);
634 cp->button_state = BUTTON_RELEASED;
635 edje_object_signal_emit(cp->rbt, "elm,state,right,button,up",
640 _add_colorbar(Evas_Object *obj)
642 char colorbar_name[128];
643 char colorbar_s[128];
648 wd = elm_widget_data_get(obj);
651 e = evas_object_evas_get(obj);
653 for (i = 0; i < 4; i++)
655 wd->cp[i] = ELM_NEW(Colorselector_Data);
656 wd->cp[i]->parent = obj;
660 wd->cp[i]->color_type = HUE;
663 wd->cp[i]->color_type = SATURATION;
666 wd->cp[i]->color_type = LIGHTNESS;
669 wd->cp[i]->color_type = ALPHA;
674 /* load colorbar area */
675 wd->cp[i]->colorbar = edje_object_add(e);
676 _elm_theme_object_set(obj, wd->cp[i]->colorbar, "colorselector", "base",
678 snprintf(colorbar_name, sizeof(colorbar_name), "colorbar_%d", i);
679 snprintf(colorbar_s, sizeof(colorbar_s), "elm.colorbar_%d", i);
680 edje_object_signal_callback_add(wd->cp[i]->colorbar, "drag", "*",
681 _arrow_cb, wd->cp[i]);
682 edje_object_part_swallow(wd->base, colorbar_s, wd->cp[i]->colorbar);
683 elm_widget_sub_object_add(obj, wd->cp[i]->colorbar);
685 /* load colorbar image */
686 wd->cp[i]->bar = edje_object_add(e);
687 _elm_theme_object_set(obj, wd->cp[i]->bar, "colorselector", "image",
689 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar",
691 elm_widget_sub_object_add(obj, wd->cp[i]->bar);
693 /* provide expanded touch area */
694 wd->cp[i]->touch_area = evas_object_rectangle_add(e);
695 evas_object_color_set(wd->cp[i]->touch_area, 0, 0, 0, 0);
696 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.arrow_bg",
697 wd->cp[i]->touch_area);
698 evas_object_event_callback_add(wd->cp[i]->touch_area,
699 EVAS_CALLBACK_MOUSE_DOWN, _colorbar_cb,
701 elm_widget_sub_object_add(obj, wd->cp[i]->touch_area);
703 /* load background rectangle of the colorbar. used for
704 changing color of the opacity bar */
705 if ((i == 1) || (i == 2))
707 wd->cp[i]->bg_rect = evas_object_rectangle_add(e);
708 evas_object_color_set(wd->cp[i]->bg_rect, wd->er, wd->eg, wd->eb,
710 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar_bg",
713 elm_widget_sub_object_add(obj, wd->cp[i]->bg_rect);
717 wd->cp[i]->bg_rect = edje_object_add(e);
718 _elm_theme_object_set(obj, wd->cp[i]->bg_rect, "colorselector",
719 "bg_image", colorbar_name);
720 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar_bg",
722 elm_widget_sub_object_add(obj, wd->cp[i]->bg_rect);
723 evas_object_color_set(wd->cp[i]->bar, wd->er, wd->eg, wd->eb, 255);
725 /* load arrow image, pointing the colorbar */
726 wd->cp[i]->arrow = edje_object_add(e);
727 _elm_theme_object_set(obj, wd->cp[i]->arrow, "colorselector", "image",
729 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.arrow_icon",
731 elm_widget_sub_object_add(obj, wd->cp[i]->arrow);
733 evas_object_color_set(wd->cp[i]->arrow, 0, 0, 0, 255);
735 evas_object_color_set(wd->cp[i]->arrow, wd->er, wd->eg, wd->eb, 255);
737 /* load left button */
738 wd->cp[i]->lbt = edje_object_add(e);
739 _elm_theme_object_set(obj, wd->cp[i]->lbt, "colorselector", "button",
741 evas_object_event_callback_add(wd->cp[i]->lbt, EVAS_CALLBACK_MOUSE_DOWN,
742 _left_button_down_cb, wd->cp[i]);
743 evas_object_event_callback_add(wd->cp[i]->lbt, EVAS_CALLBACK_MOUSE_UP,
744 _left_button_up_cb, wd->cp[i]);
745 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.l_button",
747 elm_widget_sub_object_add(obj, wd->cp[i]->lbt);
749 /* load right button */
750 wd->cp[i]->rbt = edje_object_add(e);
751 _elm_theme_object_set(obj, wd->cp[i]->rbt, "colorselector", "button",
753 evas_object_event_callback_add(wd->cp[i]->rbt, EVAS_CALLBACK_MOUSE_DOWN,
754 _right_button_down_cb, wd->cp[i]);
755 evas_object_event_callback_add(wd->cp[i]->rbt, EVAS_CALLBACK_MOUSE_UP,
756 _right_button_up_cb, wd->cp[i]);
757 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.r_button",
759 elm_widget_sub_object_add(obj, wd->cp[i]->rbt);
764 _set_color(Evas_Object *obj, int r, int g, int b, int a)
766 Widget_Data *wd = elm_widget_data_get(obj);
776 edje_object_part_drag_value_get(wd->cp[0]->colorbar, "elm.arrow", &x, &y);
778 edje_object_part_drag_value_set(wd->cp[0]->colorbar, "elm.arrow", x, y);
779 _draw_rects(wd->cp[0], x);
781 edje_object_part_drag_value_get(wd->cp[1]->colorbar, "elm.arrow", &x, &y);
783 edje_object_part_drag_value_set(wd->cp[1]->colorbar, "elm.arrow", x, y);
784 _draw_rects(wd->cp[1], x);
786 edje_object_part_drag_value_get(wd->cp[2]->colorbar, "elm.arrow", &x, &y);
788 edje_object_part_drag_value_set(wd->cp[2]->colorbar, "elm.arrow", x, y);
789 _draw_rects(wd->cp[2], x);
791 edje_object_part_drag_value_get(wd->cp[3]->colorbar, "elm.arrow", &x, &y);
793 edje_object_part_drag_value_set(wd->cp[3]->colorbar, "elm.arrow", x, y);
794 _draw_rects(wd->cp[3], x);
798 * Add a new colorselector to the parent
800 * @param parent The parent object
801 * @return The new object or NULL if it cannot be created
803 * @ingroup Colorselector
806 elm_colorselector_add(Evas_Object *parent)
808 Evas_Object *obj = NULL;
809 Widget_Data *wd = NULL;
812 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
814 ELM_SET_WIDTYPE(widtype, "colorselector");
815 elm_widget_type_set(obj, "colorselector");
816 elm_widget_sub_object_add(parent, obj);
817 elm_widget_data_set(obj, wd);
818 elm_widget_del_hook_set(obj, _del_hook);
819 elm_widget_theme_hook_set(obj, _theme_hook);
821 /* load background edj */
822 wd->base = edje_object_add(e);
823 _elm_theme_object_set(obj, wd->base, "colorselector", "bg", "default");
824 elm_widget_resize_object_set(obj, wd->base);
838 evas_object_smart_callbacks_descriptions_set(obj, _signals);
843 * Set a color for the colorselector
845 * @param obj Colorselector object
846 * @param r r-value of color
847 * @param g g-value of color
848 * @param b b-value of color
849 * @param a a-value of color
851 * @ingroup Colorselector
854 elm_colorselector_color_set(Evas_Object *obj, int r, int g, int b, int a)
856 ELM_CHECK_WIDTYPE(obj, widtype);
857 _set_color(obj, r, g, b, a);
861 * Get a color from the colorselector
863 * @param obj Colorselector object
864 * @param r integer pointer for r-value of color
865 * @param g integer pointer for g-value of color
866 * @param b integer pointer for b-value of color
867 * @param a integer pointer for a-value of color
869 * @ingroup Colorselector
872 elm_colorselector_color_get(const Evas_Object *obj, int *r, int *g, int *b, int*a)
874 Widget_Data *wd = elm_widget_data_get(obj);
875 ELM_CHECK_WIDTYPE(obj, widtype);