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.
11 #define BASE_STEP 360.0
12 #define HUE_STEP 360.0
13 #define SAT_STEP 128.0
14 #define LIG_STEP 256.0
15 #define ALP_STEP 256.0
17 typedef enum _Button_State
24 typedef enum _Color_Type
32 typedef struct _Colorselector_Data Colorselector_Data;
33 struct _Colorselector_Data
36 Evas_Object *colorbar;
42 Evas_Object *touch_area;
43 Color_Type color_type;
44 Button_State button_state;
47 typedef struct _Widget_Data Widget_Data;
51 Colorselector_Data *cp[4];
52 Evas_Coord _x, _y, _w, _h;
58 Ecore_Timer *lp_timer;
59 Ecore_Timer *mv_timer;
62 static const char *widtype = NULL;
64 static void _del_hook(Evas_Object *obj);
65 static void _theme_hook(Evas_Object *obj);
66 static void _sizing_eval(Evas_Object *obj);
67 static void _rgb_to_hsl(void *data);
68 static void _hsl_to_rgb(void *data);
69 static void _color_with_saturation(void *data);
70 static void _color_with_lightness(void *data);
71 static void _draw_rects(void *data, double x);
72 static void _arrow_cb(void *data, Evas_Object *obj, const char *emission, const char *source);
73 static void _colorbar_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
74 static Eina_Bool _mv_timer(void *data);
75 static Eina_Bool _long_press_timer(void *data);
76 static void _left_button_down_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
77 static void _right_button_down_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
78 static void _left_button_up_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
79 static void _right_button_up_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
80 static void _add_colorbar(Evas_Object *obj);
81 static void _set_color(Evas_Object *obj, int r, int g, int b, int a);
83 #define SIG_CHANGED "changed"
85 static const Evas_Smart_Cb_Description _signals[] =
92 _del_hook(Evas_Object *obj)
94 Widget_Data *wd = elm_widget_data_get(obj);
98 if (wd->lp_timer) ecore_timer_del(wd->lp_timer);
99 if (wd->mv_timer) ecore_timer_del(wd->mv_timer);
100 for (i = 0; i < 4; i++) free(wd->cp[i]);
105 _theme_hook(Evas_Object *obj)
107 Widget_Data *wd = elm_widget_data_get(obj);
110 if ((!wd) || (!wd->base)) return;
112 _elm_theme_object_set(obj, wd->base, "colorselector", "bg",
113 elm_widget_style_get(obj));
115 for (i = 0; i < 4; i++)
117 evas_object_del(wd->cp[i]->colorbar);
118 wd->cp[i]->colorbar = NULL;
119 evas_object_del(wd->cp[i]->bar);
120 wd->cp[i]->bar = NULL;
121 evas_object_del(wd->cp[i]->lbt);
122 wd->cp[i]->lbt = NULL;
123 evas_object_del(wd->cp[i]->rbt);
124 wd->cp[i]->rbt = NULL;
127 evas_object_del(wd->cp[i]->bg_rect);
128 wd->cp[i]->bg_rect = NULL;
130 evas_object_del(wd->cp[i]->arrow);
131 wd->cp[i]->arrow = NULL;
132 evas_object_del(wd->cp[i]->touch_area);
133 wd->cp[i]->touch_area = NULL;
141 _colorselector_set_size_hints(Evas_Object *obj, int timesw, int timesh)
143 Evas_Coord minw = -1, minh = -1;
145 elm_coords_finger_size_adjust(timesw, &minw, timesh, &minh);
146 edje_object_size_min_restricted_calc(obj, &minw, &minh,
148 evas_object_size_hint_min_set(obj, minw, minh);
149 evas_object_size_hint_max_set(obj, -1, -1);
153 _sizing_eval(Evas_Object *obj)
155 Widget_Data *wd = elm_widget_data_get(obj);
156 Evas_Coord minw = -1, minh = -1;
160 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
161 for (i = 0; i < 4; i++)
163 if (wd->cp[i]->bg_rect)
164 _colorselector_set_size_hints(wd->cp[i]->bg_rect, 1, 1);
165 _colorselector_set_size_hints(wd->cp[i]->bar, 1, 1);
166 _colorselector_set_size_hints(wd->cp[i]->rbt, 1, 1);
167 _colorselector_set_size_hints(wd->cp[i]->lbt, 1, 1);
169 _colorselector_set_size_hints(wd->cp[i]->colorbar, 4, 1);
172 elm_coords_finger_size_adjust(4, &minw, 4, &minh);
173 edje_object_size_min_restricted_calc(wd->base, &minw, &minh, minw, minh);
174 evas_object_size_hint_min_set(obj, minw, minh);
175 evas_object_size_hint_max_set(obj, -1, -1);
179 _rgb_to_hsl(void *data)
181 Widget_Data *wd = data;
204 wd->l = (m + v) / 2.0;
206 if (wd->l <= 0.0) return;
211 if (wd->s > 0.0) wd->s /= (wd->l <= 0.5) ? (v + m) : (2.0 - v - m);
218 if (r == v) wd->h = (g == m ? 5.0 + b2 : 1.0 - g2);
219 else if (g == v) wd->h = (b == m ? 1.0 + r2 : 3.0 - b2);
220 else wd->h = (r == m ? 3.0 + g2 : 5.0 - r2);
226 _hsl_to_rgb(void *data)
228 Widget_Data *wd = data;
229 double r = 0, g = 0, b = 0;
232 double sv, vsf, f, p, q, t, v;
238 if (_s == 0.0) r = g = b = _l;
241 if (_h == 360.0) _h = 0.0;
244 v = (_l <= 0.5) ? (_l * (1.0 + _s)) : (_l + _s - (_l * _s));
247 if (v) sv = (v - p) / v;
292 i = (int)(r * 255.0);
294 wd->r = (f <= 0.5) ? i : (i + 1);
296 i = (int)(g * 255.0);
298 wd->g = (f <= 0.5) ? i : (i + 1);
300 i = (int)(b * 255.0);
302 wd->b = (f <= 0.5) ? i : (i + 1);
306 _color_with_saturation(void *data)
308 Widget_Data *wd = data;
311 wd->sr = 127 + (int)((double)(wd->er - 127) * wd->s);
313 wd->sr = 127 - (int)((double)(127 - wd->er) * wd->s);
316 wd->sg = 127 + (int)((double)(wd->eg - 127) * wd->s);
318 wd->sg = 127 - (int)((double)(127 - wd->eg) * wd->s);
321 wd->sb = 127 + (int)((double)(wd->eb - 127) * wd->s);
323 wd->sb = 127 - (int)((double)(127 - wd->eb) * wd->s);
327 _color_with_lightness(void *data)
329 Widget_Data *wd = data;
333 wd->lr = wd->er + (int)((double)(255 - wd->er) * (wd->l - 0.5) * 2.0);
334 wd->lg = wd->eg + (int)((double)(255 - wd->eg) * (wd->l - 0.5) * 2.0);
335 wd->lb = wd->eb + (int)((double)(255 - wd->eb) * (wd->l - 0.5) * 2.0);
337 else if (wd->l < 0.5)
339 wd->lr = (double)wd->er * wd->l * 2.0;
340 wd->lg = (double)wd->eg * wd->l * 2.0;
341 wd->lb = (double)wd->eb * wd->l * 2.0;
352 _draw_rects(void *data, double x)
354 Colorselector_Data *cp = data;
355 Widget_Data *wd = elm_widget_data_get(cp->parent);
356 double one_six = 1.0 / 6.0;
358 switch (cp->color_type)
366 wd->eg = (255.0 * x * 6.0);
369 else if (x < 2 * one_six)
371 wd->er = 255 - (int)(255.0 * (x - one_six) * 6.0);
375 else if (x < 3 * one_six)
379 wd->eb = (int)(255.0 * (x - (2.0 * one_six)) * 6.0);
381 else if (x < 4 * one_six)
384 wd->eg = 255 - (int)(255.0 * (x - (3.0 * one_six)) * 6.0);
387 else if (x < 5 * one_six)
389 wd->er = 255.0 * (x - (4.0 * one_six)) * 6.0;
397 wd->eb = 255 - (int)(255.0 * (x - (5.0 * one_six)) * 6.0);
400 evas_object_color_set(wd->cp[0]->arrow, wd->er, wd->eg, wd->eb, 255);
401 evas_object_color_set(wd->cp[1]->bg_rect, wd->er, wd->eg, wd->eb, 255);
402 evas_object_color_set(wd->cp[2]->bg_rect, wd->er, wd->eg, wd->eb, 255);
403 evas_object_color_set(wd->cp[3]->bar, wd->er, wd->eg, wd->eb, 255);
405 _color_with_saturation(wd);
406 evas_object_color_set(wd->cp[1]->arrow, wd->sr, wd->sg, wd->sb, 255);
408 _color_with_lightness(wd);
409 evas_object_color_set(wd->cp[2]->arrow, wd->lr, wd->lg, wd->lb, 255);
411 evas_object_color_set(wd->cp[3]->arrow,
412 (wd->er * wd->a) / 255,
413 (wd->eg * wd->a) / 255,
414 (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);
424 _color_with_lightness(wd);
425 evas_object_color_set(wd->cp[2]->arrow, wd->lr, wd->lg, wd->lb, 255);
429 evas_object_color_set(wd->cp[3]->arrow, wd->er, wd->eg, wd->eb, wd->a);
438 _arrow_cb(void *data, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
440 Colorselector_Data *cp = data;
443 edje_object_part_drag_value_get(obj, "elm.arrow", &x, &y);
444 _draw_rects(data, x);
445 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
449 _colorbar_cb(void *data, Evas *e, Evas_Object *obj __UNUSED__, void *event_info)
451 Colorselector_Data *cp = data;
452 Evas_Event_Mouse_Down *ev = event_info;
453 Evas_Coord x, y, w, h;
454 double arrow_x = 0, arrow_y;
456 evas_object_geometry_get(cp->bar, &x, &y, &w, &h);
457 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow",
459 if (w > 0) arrow_x = (double)(ev->output.x - x) / (double)w;
460 if (arrow_x > 1) arrow_x = 1;
461 if (arrow_x < 0) arrow_x = 0;
462 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", arrow_x, arrow_y);
463 _draw_rects(data, arrow_x);
464 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
465 evas_event_feed_mouse_cancel(e, 0, NULL);
466 evas_event_feed_mouse_down(e, 1, EVAS_BUTTON_NONE, 0, NULL);
470 _mv_timer(void *data)
472 Colorselector_Data *cp = data;
473 Widget_Data *wd = elm_widget_data_get(cp->parent);
476 if (!wd) return EINA_FALSE;
478 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
479 if (cp->button_state == L_BUTTON_PRESSED)
481 x -= 1.0 / BASE_STEP;
482 if (x < 0.0) x = 0.0;
483 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
484 _draw_rects(data, x);
485 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
488 else if (cp->button_state == R_BUTTON_PRESSED)
490 x += 1.0 / BASE_STEP;
491 if (x > 1.0) x = 1.0;
492 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
493 _draw_rects(data, x);
494 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
502 _long_press_timer(void *data)
504 Colorselector_Data *cp = data;
505 Widget_Data *wd = elm_widget_data_get(cp->parent);
507 if (wd->mv_timer) ecore_timer_del(wd->mv_timer);
508 wd->mv_timer = ecore_timer_add(0.01, _mv_timer, cp);
515 _left_button_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
517 Colorselector_Data *cp = data;
518 Widget_Data *wd = elm_widget_data_get(cp->parent);
521 edje_object_signal_emit(cp->lbt, "elm,state,left,button,down",
523 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
525 switch(cp->color_type)
543 if (x < 0.0) x = 0.0;
545 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
546 _draw_rects(data, x);
547 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
548 cp->button_state = L_BUTTON_PRESSED;
549 if (wd->lp_timer) ecore_timer_del(wd->lp_timer);
550 wd->lp_timer = ecore_timer_add(_elm_config->longpress_timeout, _long_press_timer, cp);
554 _right_button_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
556 Colorselector_Data *cp = data;
557 Widget_Data *wd = elm_widget_data_get(cp->parent);
560 edje_object_signal_emit(cp->rbt, "elm,state,right,button,down",
562 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
564 switch(cp->color_type)
582 if (x > 1.0) x = 1.0;
584 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
585 _draw_rects(data, x);
586 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
587 cp->button_state = R_BUTTON_PRESSED;
588 wd->lp_timer = ecore_timer_add(_elm_config->longpress_timeout, _long_press_timer, cp);
592 _left_button_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
594 Colorselector_Data *cp = data;
595 Widget_Data *wd = elm_widget_data_get(cp->parent);
599 ecore_timer_del(wd->lp_timer);
604 ecore_timer_del(wd->mv_timer);
608 cp->button_state = BUTTON_RELEASED;
609 edje_object_signal_emit(cp->lbt, "elm,state,left,button,up", "left_button");
613 _right_button_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
615 Colorselector_Data *cp = data;
616 Widget_Data *wd = elm_widget_data_get(cp->parent);
620 ecore_timer_del(wd->lp_timer);
625 ecore_timer_del(wd->mv_timer);
629 cp->button_state = BUTTON_RELEASED;
630 edje_object_signal_emit(cp->rbt, "elm,state,right,button,up",
635 _add_colorbar(Evas_Object *obj)
637 char colorbar_name[128];
638 char colorbar_s[128];
643 wd = elm_widget_data_get(obj);
646 e = evas_object_evas_get(obj);
648 for (i = 0; i < 4; i++)
650 wd->cp[i] = ELM_NEW(Colorselector_Data);
651 wd->cp[i]->parent = obj;
655 wd->cp[i]->color_type = HUE;
658 wd->cp[i]->color_type = SATURATION;
661 wd->cp[i]->color_type = LIGHTNESS;
664 wd->cp[i]->color_type = ALPHA;
669 /* load colorbar area */
670 wd->cp[i]->colorbar = edje_object_add(e);
671 _elm_theme_object_set(obj, wd->cp[i]->colorbar, "colorselector", "base",
673 snprintf(colorbar_name, sizeof(colorbar_name), "colorbar_%d", i);
674 snprintf(colorbar_s, sizeof(colorbar_s), "elm.colorbar_%d", i);
675 edje_object_signal_callback_add(wd->cp[i]->colorbar, "drag", "*",
676 _arrow_cb, wd->cp[i]);
677 edje_object_part_swallow(wd->base, colorbar_s, wd->cp[i]->colorbar);
678 elm_widget_sub_object_add(obj, wd->cp[i]->colorbar);
680 /* load colorbar image */
681 wd->cp[i]->bar = edje_object_add(e);
682 _elm_theme_object_set(obj, wd->cp[i]->bar, "colorselector", "image",
684 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar",
686 elm_widget_sub_object_add(obj, wd->cp[i]->bar);
688 /* provide expanded touch area */
689 wd->cp[i]->touch_area = evas_object_rectangle_add(e);
690 evas_object_color_set(wd->cp[i]->touch_area, 0, 0, 0, 0);
691 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.arrow_bg",
692 wd->cp[i]->touch_area);
693 evas_object_event_callback_add(wd->cp[i]->touch_area,
694 EVAS_CALLBACK_MOUSE_DOWN, _colorbar_cb,
696 elm_widget_sub_object_add(obj, wd->cp[i]->touch_area);
698 /* load background rectangle of the colorbar. used for
699 changing color of the opacity bar */
700 if ((i == 1) || (i == 2))
702 wd->cp[i]->bg_rect = evas_object_rectangle_add(e);
703 evas_object_color_set(wd->cp[i]->bg_rect, wd->er, wd->eg, wd->eb,
705 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar_bg",
708 elm_widget_sub_object_add(obj, wd->cp[i]->bg_rect);
712 wd->cp[i]->bg_rect = edje_object_add(e);
713 _elm_theme_object_set(obj, wd->cp[i]->bg_rect, "colorselector",
714 "bg_image", colorbar_name);
715 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar_bg",
717 elm_widget_sub_object_add(obj, wd->cp[i]->bg_rect);
718 evas_object_color_set(wd->cp[i]->bar, wd->er, wd->eg, wd->eb, 255);
720 /* load arrow image, pointing the colorbar */
721 wd->cp[i]->arrow = edje_object_add(e);
722 _elm_theme_object_set(obj, wd->cp[i]->arrow, "colorselector", "image",
724 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.arrow_icon",
726 elm_widget_sub_object_add(obj, wd->cp[i]->arrow);
728 evas_object_color_set(wd->cp[i]->arrow, 0, 0, 0, 255);
730 evas_object_color_set(wd->cp[i]->arrow, wd->er, wd->eg, wd->eb, 255);
732 /* load left button */
733 wd->cp[i]->lbt = edje_object_add(e);
734 _elm_theme_object_set(obj, wd->cp[i]->lbt, "colorselector", "button",
736 evas_object_event_callback_add(wd->cp[i]->lbt, EVAS_CALLBACK_MOUSE_DOWN,
737 _left_button_down_cb, wd->cp[i]);
738 evas_object_event_callback_add(wd->cp[i]->lbt, EVAS_CALLBACK_MOUSE_UP,
739 _left_button_up_cb, wd->cp[i]);
740 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.l_button",
742 elm_widget_sub_object_add(obj, wd->cp[i]->lbt);
744 /* load right button */
745 wd->cp[i]->rbt = edje_object_add(e);
746 _elm_theme_object_set(obj, wd->cp[i]->rbt, "colorselector", "button",
748 evas_object_event_callback_add(wd->cp[i]->rbt, EVAS_CALLBACK_MOUSE_DOWN,
749 _right_button_down_cb, wd->cp[i]);
750 evas_object_event_callback_add(wd->cp[i]->rbt, EVAS_CALLBACK_MOUSE_UP,
751 _right_button_up_cb, wd->cp[i]);
752 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.r_button",
754 elm_widget_sub_object_add(obj, wd->cp[i]->rbt);
759 _set_color(Evas_Object *obj, int r, int g, int b, int a)
761 Widget_Data *wd = elm_widget_data_get(obj);
771 edje_object_part_drag_value_get(wd->cp[0]->colorbar, "elm.arrow", &x, &y);
773 edje_object_part_drag_value_set(wd->cp[0]->colorbar, "elm.arrow", x, y);
774 _draw_rects(wd->cp[0], x);
776 edje_object_part_drag_value_get(wd->cp[1]->colorbar, "elm.arrow", &x, &y);
778 edje_object_part_drag_value_set(wd->cp[1]->colorbar, "elm.arrow", x, y);
779 _draw_rects(wd->cp[1], x);
781 edje_object_part_drag_value_get(wd->cp[2]->colorbar, "elm.arrow", &x, &y);
783 edje_object_part_drag_value_set(wd->cp[2]->colorbar, "elm.arrow", x, y);
784 _draw_rects(wd->cp[2], x);
786 edje_object_part_drag_value_get(wd->cp[3]->colorbar, "elm.arrow", &x, &y);
788 edje_object_part_drag_value_set(wd->cp[3]->colorbar, "elm.arrow", x, y);
789 _draw_rects(wd->cp[3], x);
793 * Add a new colorselector to the parent
795 * @param parent The parent object
796 * @return The new object or NULL if it cannot be created
798 * @ingroup Colorselector
801 elm_colorselector_add(Evas_Object *parent)
803 Evas_Object *obj = NULL;
804 Widget_Data *wd = NULL;
807 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
809 ELM_SET_WIDTYPE(widtype, "colorselector");
810 elm_widget_type_set(obj, "colorselector");
811 elm_widget_sub_object_add(parent, obj);
812 elm_widget_data_set(obj, wd);
813 elm_widget_del_hook_set(obj, _del_hook);
814 elm_widget_theme_hook_set(obj, _theme_hook);
816 /* load background edj */
817 wd->base = edje_object_add(e);
818 _elm_theme_object_set(obj, wd->base, "colorselector", "bg", "default");
819 elm_widget_resize_object_set(obj, wd->base);
833 evas_object_smart_callbacks_descriptions_set(obj, _signals);
838 * Set a color for the colorselector
840 * @param obj Colorselector object
841 * @param r r-value of color
842 * @param g g-value of color
843 * @param b b-value of color
844 * @param a a-value of color
846 * @ingroup Colorselector
849 elm_colorselector_color_set(Evas_Object *obj, int r, int g, int b, int a)
851 ELM_CHECK_WIDTYPE(obj, widtype);
852 _set_color(obj, r, g, b, a);
856 * Get a color from the colorselector
858 * @param obj Colorselector object
859 * @param r integer pointer for r-value of color
860 * @param g integer pointer for g-value of color
861 * @param b integer pointer for b-value of color
862 * @param a integer pointer for a-value of color
864 * @ingroup Colorselector
867 elm_colorselector_color_get(const Evas_Object *obj, int *r, int *g, int *b, int*a)
869 Widget_Data *wd = elm_widget_data_get(obj);
870 ELM_CHECK_WIDTYPE(obj, widtype);