1 #include <Elementary.h>
5 * @addtogroup Colorselector Colorselector
7 * By using colorselector, you can select a color.
8 * Colorselector made a color using HSV/HSB mode.
10 * Signals that you can add callbacks for are:
12 * "changed" - when the color value changes
15 #define BASE_STEP 360.0
16 #define HUE_STEP 360.0
17 #define SAT_STEP 128.0
18 #define LIG_STEP 256.0
19 #define ALP_STEP 256.0
21 typedef enum _Button_State
28 typedef enum _Color_Type
36 typedef struct _Colorselector_Data Colorselector_Data;
37 struct _Colorselector_Data
40 Evas_Object *colorbar;
46 Evas_Object *touch_area;
47 Color_Type color_type;
48 Button_State button_state;
51 typedef struct _Widget_Data Widget_Data;
55 Colorselector_Data *cp[4];
56 Evas_Coord _x, _y, _w, _h;
62 Ecore_Timer *lp_timer;
63 Ecore_Timer *mv_timer;
66 static const char *widtype = NULL;
68 static void _del_hook(Evas_Object *obj);
69 static void _theme_hook(Evas_Object *obj);
70 static void _sizing_eval(Evas_Object *obj);
71 static void _rgb_to_hsl(void *data);
72 static void _hsl_to_rgb(void *data);
73 static void _color_with_saturation(void *data);
74 static void _color_with_lightness(void *data);
75 static void _draw_rects(void *data, double x);
76 static void _arrow_cb(void *data, Evas_Object *obj, const char *emission, const char *source);
77 static void _colorbar_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
78 static Eina_Bool _mv_timer(void *data);
79 static Eina_Bool _long_press_timer(void *data);
80 static void _left_button_down_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
81 static void _right_button_down_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
82 static void _left_button_up_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
83 static void _right_button_up_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
84 static void _add_colorbar(Evas_Object *obj);
85 static void _set_color(Evas_Object *obj, int r, int g, int b, int a);
87 static const char SIG_CHANGED[] = "changed";
89 static const Evas_Smart_Cb_Description _signals[] =
96 _del_hook(Evas_Object *obj)
98 Widget_Data *wd = elm_widget_data_get(obj);
102 if (wd->lp_timer) ecore_timer_del(wd->lp_timer);
103 if (wd->mv_timer) ecore_timer_del(wd->mv_timer);
104 for (i = 0; i < 4; i++) free(wd->cp[i]);
109 _theme_hook(Evas_Object *obj)
111 Widget_Data *wd = elm_widget_data_get(obj);
114 if ((!wd) || (!wd->base)) return;
116 _elm_theme_object_set(obj, wd->base, "colorselector", "bg",
117 elm_widget_style_get(obj));
119 for (i = 0; i < 4; i++)
121 evas_object_del(wd->cp[i]->colorbar);
122 wd->cp[i]->colorbar = NULL;
123 evas_object_del(wd->cp[i]->bar);
124 wd->cp[i]->bar = NULL;
125 evas_object_del(wd->cp[i]->lbt);
126 wd->cp[i]->lbt = NULL;
127 evas_object_del(wd->cp[i]->rbt);
128 wd->cp[i]->rbt = NULL;
131 evas_object_del(wd->cp[i]->bg_rect);
132 wd->cp[i]->bg_rect = NULL;
134 evas_object_del(wd->cp[i]->arrow);
135 wd->cp[i]->arrow = NULL;
136 evas_object_del(wd->cp[i]->touch_area);
137 wd->cp[i]->touch_area = NULL;
145 _colorselector_set_size_hints(Evas_Object *obj, int timesw, int timesh)
147 Evas_Coord minw = -1, minh = -1;
149 elm_coords_finger_size_adjust(timesw, &minw, timesh, &minh);
150 edje_object_size_min_restricted_calc(obj, &minw, &minh,
152 evas_object_size_hint_min_set(obj, minw, minh);
153 evas_object_size_hint_max_set(obj, -1, -1);
157 _sizing_eval(Evas_Object *obj)
159 Widget_Data *wd = elm_widget_data_get(obj);
160 Evas_Coord minw = -1, minh = -1;
164 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
165 for (i = 0; i < 4; i++)
167 if (wd->cp[i]->bg_rect)
168 _colorselector_set_size_hints(wd->cp[i]->bg_rect, 1, 1);
169 _colorselector_set_size_hints(wd->cp[i]->bar, 1, 1);
170 _colorselector_set_size_hints(wd->cp[i]->rbt, 1, 1);
171 _colorselector_set_size_hints(wd->cp[i]->lbt, 1, 1);
173 _colorselector_set_size_hints(wd->cp[i]->colorbar, 4, 1);
176 elm_coords_finger_size_adjust(4, &minw, 4, &minh);
177 edje_object_size_min_restricted_calc(wd->base, &minw, &minh, minw, minh);
178 evas_object_size_hint_min_set(obj, minw, minh);
179 evas_object_size_hint_max_set(obj, -1, -1);
183 _rgb_to_hsl(void *data)
185 Widget_Data *wd = data;
208 wd->l = (m + v) / 2.0;
210 if (wd->l <= 0.0) return;
215 if (wd->s > 0.0) wd->s /= (wd->l <= 0.5) ? (v + m) : (2.0 - v - m);
222 if (r == v) wd->h = (g == m ? 5.0 + b2 : 1.0 - g2);
223 else if (g == v) wd->h = (b == m ? 1.0 + r2 : 3.0 - b2);
224 else wd->h = (r == m ? 3.0 + g2 : 5.0 - r2);
230 _hsl_to_rgb(void *data)
232 Widget_Data *wd = data;
233 double r = 0, g = 0, b = 0;
236 double sv, vsf, f, p, q, t, v;
242 if (_s == 0.0) r = g = b = _l;
245 if (_h == 360.0) _h = 0.0;
248 v = (_l <= 0.5) ? (_l * (1.0 + _s)) : (_l + _s - (_l * _s));
251 if (v) sv = (v - p) / v;
296 i = (int)(r * 255.0);
298 wd->r = (f <= 0.5) ? i : (i + 1);
300 i = (int)(g * 255.0);
302 wd->g = (f <= 0.5) ? i : (i + 1);
304 i = (int)(b * 255.0);
306 wd->b = (f <= 0.5) ? i : (i + 1);
310 _color_with_saturation(void *data)
312 Widget_Data *wd = data;
315 wd->sr = 127 + (int)((double)(wd->er - 127) * wd->s);
317 wd->sr = 127 - (int)((double)(127 - wd->er) * wd->s);
320 wd->sg = 127 + (int)((double)(wd->eg - 127) * wd->s);
322 wd->sg = 127 - (int)((double)(127 - wd->eg) * wd->s);
325 wd->sb = 127 + (int)((double)(wd->eb - 127) * wd->s);
327 wd->sb = 127 - (int)((double)(127 - wd->eb) * wd->s);
331 _color_with_lightness(void *data)
333 Widget_Data *wd = data;
337 wd->lr = wd->er + (int)((double)(255 - wd->er) * (wd->l - 0.5) * 2.0);
338 wd->lg = wd->eg + (int)((double)(255 - wd->eg) * (wd->l - 0.5) * 2.0);
339 wd->lb = wd->eb + (int)((double)(255 - wd->eb) * (wd->l - 0.5) * 2.0);
341 else if (wd->l < 0.5)
343 wd->lr = (double)wd->er * wd->l * 2.0;
344 wd->lg = (double)wd->eg * wd->l * 2.0;
345 wd->lb = (double)wd->eb * wd->l * 2.0;
356 _draw_rects(void *data, double x)
358 Colorselector_Data *cp = data;
359 Widget_Data *wd = elm_widget_data_get(cp->parent);
360 double one_six = 1.0 / 6.0;
362 switch (cp->color_type)
370 wd->eg = (255.0 * x * 6.0);
373 else if (x < 2 * one_six)
375 wd->er = 255 - (int)(255.0 * (x - one_six) * 6.0);
379 else if (x < 3 * one_six)
383 wd->eb = (int)(255.0 * (x - (2.0 * one_six)) * 6.0);
385 else if (x < 4 * one_six)
388 wd->eg = 255 - (int)(255.0 * (x - (3.0 * one_six)) * 6.0);
391 else if (x < 5 * one_six)
393 wd->er = 255.0 * (x - (4.0 * one_six)) * 6.0;
401 wd->eb = 255 - (int)(255.0 * (x - (5.0 * one_six)) * 6.0);
404 evas_object_color_set(wd->cp[0]->arrow, wd->er, wd->eg, wd->eb, 255);
405 evas_object_color_set(wd->cp[1]->bg_rect, wd->er, wd->eg, wd->eb, 255);
406 evas_object_color_set(wd->cp[2]->bg_rect, wd->er, wd->eg, wd->eb, 255);
407 evas_object_color_set(wd->cp[3]->bar, wd->er, wd->eg, wd->eb, 255);
409 _color_with_saturation(wd);
410 evas_object_color_set(wd->cp[1]->arrow, wd->sr, wd->sg, wd->sb, 255);
412 _color_with_lightness(wd);
413 evas_object_color_set(wd->cp[2]->arrow, wd->lr, wd->lg, wd->lb, 255);
415 evas_object_color_set(wd->cp[3]->arrow,
416 (wd->er * wd->a) / 255,
417 (wd->eg * wd->a) / 255,
418 (wd->eb * wd->a) / 255,
423 _color_with_saturation(wd);
424 evas_object_color_set(wd->cp[1]->arrow, wd->sr, wd->sg, wd->sb, 255);
428 _color_with_lightness(wd);
429 evas_object_color_set(wd->cp[2]->arrow, wd->lr, wd->lg, wd->lb, 255);
433 evas_object_color_set(wd->cp[3]->arrow, wd->er, wd->eg, wd->eb, wd->a);
442 _arrow_cb(void *data, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
444 Colorselector_Data *cp = data;
447 edje_object_part_drag_value_get(obj, "elm.arrow", &x, &y);
448 _draw_rects(data, x);
449 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
453 _colorbar_cb(void *data, Evas *e, Evas_Object *obj __UNUSED__, void *event_info)
455 Colorselector_Data *cp = data;
456 Evas_Event_Mouse_Down *ev = event_info;
457 Evas_Coord x, y, w, h;
458 double arrow_x = 0, arrow_y;
460 evas_object_geometry_get(cp->bar, &x, &y, &w, &h);
461 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow",
463 if (w > 0) arrow_x = (double)(ev->output.x - x) / (double)w;
464 if (arrow_x > 1) arrow_x = 1;
465 if (arrow_x < 0) arrow_x = 0;
466 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", arrow_x, arrow_y);
467 _draw_rects(data, arrow_x);
468 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
469 evas_event_feed_mouse_cancel(e, 0, NULL);
470 evas_event_feed_mouse_down(e, 1, EVAS_BUTTON_NONE, 0, NULL);
474 _mv_timer(void *data)
476 Colorselector_Data *cp = data;
477 Widget_Data *wd = elm_widget_data_get(cp->parent);
480 if (!wd) return EINA_FALSE;
482 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
483 if (cp->button_state == L_BUTTON_PRESSED)
485 x -= 1.0 / BASE_STEP;
486 if (x < 0.0) x = 0.0;
487 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
488 _draw_rects(data, x);
489 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
492 else if (cp->button_state == R_BUTTON_PRESSED)
494 x += 1.0 / BASE_STEP;
495 if (x > 1.0) x = 1.0;
496 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
497 _draw_rects(data, x);
498 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
506 _long_press_timer(void *data)
508 Colorselector_Data *cp = data;
509 Widget_Data *wd = elm_widget_data_get(cp->parent);
511 if (wd->mv_timer) ecore_timer_del(wd->mv_timer);
512 wd->mv_timer = ecore_timer_add(0.01, _mv_timer, cp);
519 _left_button_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
521 Colorselector_Data *cp = data;
522 Widget_Data *wd = elm_widget_data_get(cp->parent);
525 edje_object_signal_emit(cp->lbt, "elm,state,left,button,down",
527 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
529 switch(cp->color_type)
547 if (x < 0.0) x = 0.0;
549 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
550 _draw_rects(data, x);
551 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
552 cp->button_state = L_BUTTON_PRESSED;
553 if (wd->lp_timer) ecore_timer_del(wd->lp_timer);
554 wd->lp_timer = ecore_timer_add(_elm_config->longpress_timeout, _long_press_timer, cp);
558 _right_button_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
560 Colorselector_Data *cp = data;
561 Widget_Data *wd = elm_widget_data_get(cp->parent);
564 edje_object_signal_emit(cp->rbt, "elm,state,right,button,down",
566 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
568 switch(cp->color_type)
586 if (x > 1.0) x = 1.0;
588 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
589 _draw_rects(data, x);
590 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
591 cp->button_state = R_BUTTON_PRESSED;
592 wd->lp_timer = ecore_timer_add(_elm_config->longpress_timeout, _long_press_timer, cp);
596 _left_button_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
598 Colorselector_Data *cp = data;
599 Widget_Data *wd = elm_widget_data_get(cp->parent);
603 ecore_timer_del(wd->lp_timer);
608 ecore_timer_del(wd->mv_timer);
612 cp->button_state = BUTTON_RELEASED;
613 edje_object_signal_emit(cp->lbt, "elm,state,left,button,up", "left_button");
617 _right_button_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
619 Colorselector_Data *cp = data;
620 Widget_Data *wd = elm_widget_data_get(cp->parent);
624 ecore_timer_del(wd->lp_timer);
629 ecore_timer_del(wd->mv_timer);
633 cp->button_state = BUTTON_RELEASED;
634 edje_object_signal_emit(cp->rbt, "elm,state,right,button,up",
639 _add_colorbar(Evas_Object *obj)
641 char colorbar_name[128];
642 char colorbar_s[128];
647 wd = elm_widget_data_get(obj);
650 e = evas_object_evas_get(obj);
652 for (i = 0; i < 4; i++)
654 wd->cp[i] = ELM_NEW(Colorselector_Data);
655 wd->cp[i]->parent = obj;
659 wd->cp[i]->color_type = HUE;
662 wd->cp[i]->color_type = SATURATION;
665 wd->cp[i]->color_type = LIGHTNESS;
668 wd->cp[i]->color_type = ALPHA;
673 /* load colorbar area */
674 wd->cp[i]->colorbar = edje_object_add(e);
675 _elm_theme_object_set(obj, wd->cp[i]->colorbar, "colorselector", "base",
677 snprintf(colorbar_name, sizeof(colorbar_name), "colorbar_%d", i);
678 snprintf(colorbar_s, sizeof(colorbar_s), "elm.colorbar_%d", i);
679 edje_object_signal_callback_add(wd->cp[i]->colorbar, "drag", "*",
680 _arrow_cb, wd->cp[i]);
681 edje_object_part_swallow(wd->base, colorbar_s, wd->cp[i]->colorbar);
682 elm_widget_sub_object_add(obj, wd->cp[i]->colorbar);
684 /* load colorbar image */
685 wd->cp[i]->bar = edje_object_add(e);
686 _elm_theme_object_set(obj, wd->cp[i]->bar, "colorselector", "image",
688 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar",
690 elm_widget_sub_object_add(obj, wd->cp[i]->bar);
692 /* provide expanded touch area */
693 wd->cp[i]->touch_area = evas_object_rectangle_add(e);
694 evas_object_color_set(wd->cp[i]->touch_area, 0, 0, 0, 0);
695 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.arrow_bg",
696 wd->cp[i]->touch_area);
697 evas_object_event_callback_add(wd->cp[i]->touch_area,
698 EVAS_CALLBACK_MOUSE_DOWN, _colorbar_cb,
700 elm_widget_sub_object_add(obj, wd->cp[i]->touch_area);
702 /* load background rectangle of the colorbar. used for
703 changing color of the opacity bar */
704 if ((i == 1) || (i == 2))
706 wd->cp[i]->bg_rect = evas_object_rectangle_add(e);
707 evas_object_color_set(wd->cp[i]->bg_rect, wd->er, wd->eg, wd->eb,
709 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar_bg",
712 elm_widget_sub_object_add(obj, wd->cp[i]->bg_rect);
716 wd->cp[i]->bg_rect = edje_object_add(e);
717 _elm_theme_object_set(obj, wd->cp[i]->bg_rect, "colorselector",
718 "bg_image", colorbar_name);
719 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar_bg",
721 elm_widget_sub_object_add(obj, wd->cp[i]->bg_rect);
722 evas_object_color_set(wd->cp[i]->bar, wd->er, wd->eg, wd->eb, 255);
724 /* load arrow image, pointing the colorbar */
725 wd->cp[i]->arrow = edje_object_add(e);
726 _elm_theme_object_set(obj, wd->cp[i]->arrow, "colorselector", "image",
728 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.arrow_icon",
730 elm_widget_sub_object_add(obj, wd->cp[i]->arrow);
732 evas_object_color_set(wd->cp[i]->arrow, 0, 0, 0, 255);
734 evas_object_color_set(wd->cp[i]->arrow, wd->er, wd->eg, wd->eb, 255);
736 /* load left button */
737 wd->cp[i]->lbt = edje_object_add(e);
738 _elm_theme_object_set(obj, wd->cp[i]->lbt, "colorselector", "button",
740 evas_object_event_callback_add(wd->cp[i]->lbt, EVAS_CALLBACK_MOUSE_DOWN,
741 _left_button_down_cb, wd->cp[i]);
742 evas_object_event_callback_add(wd->cp[i]->lbt, EVAS_CALLBACK_MOUSE_UP,
743 _left_button_up_cb, wd->cp[i]);
744 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.l_button",
746 elm_widget_sub_object_add(obj, wd->cp[i]->lbt);
748 /* load right button */
749 wd->cp[i]->rbt = edje_object_add(e);
750 _elm_theme_object_set(obj, wd->cp[i]->rbt, "colorselector", "button",
752 evas_object_event_callback_add(wd->cp[i]->rbt, EVAS_CALLBACK_MOUSE_DOWN,
753 _right_button_down_cb, wd->cp[i]);
754 evas_object_event_callback_add(wd->cp[i]->rbt, EVAS_CALLBACK_MOUSE_UP,
755 _right_button_up_cb, wd->cp[i]);
756 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.r_button",
758 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 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
813 ELM_SET_WIDTYPE(widtype, "colorselector");
814 elm_widget_type_set(obj, "colorselector");
815 elm_widget_sub_object_add(parent, obj);
816 elm_widget_data_set(obj, wd);
817 elm_widget_del_hook_set(obj, _del_hook);
818 elm_widget_theme_hook_set(obj, _theme_hook);
820 /* load background edj */
821 wd->base = edje_object_add(e);
822 _elm_theme_object_set(obj, wd->base, "colorselector", "bg", "default");
823 elm_widget_resize_object_set(obj, wd->base);
837 evas_object_smart_callbacks_descriptions_set(obj, _signals);
842 * Set a color for the colorselector
844 * @param obj Colorselector object
845 * @param r r-value of color
846 * @param g g-value of color
847 * @param b b-value of color
848 * @param a a-value of color
850 * @ingroup Colorselector
853 elm_colorselector_color_set(Evas_Object *obj, int r, int g, int b, int a)
855 ELM_CHECK_WIDTYPE(obj, widtype);
856 _set_color(obj, r, g, b, a);
860 * Get a color from the colorselector
862 * @param obj Colorselector object
863 * @param r integer pointer for r-value of color
864 * @param g integer pointer for g-value of color
865 * @param b integer pointer for b-value of color
866 * @param a integer pointer for a-value of color
868 * @ingroup Colorselector
871 elm_colorselector_color_get(const Evas_Object *obj, int *r, int *g, int *b, int*a)
873 Widget_Data *wd = elm_widget_data_get(obj);
874 ELM_CHECK_WIDTYPE(obj, widtype);