1 #include <Elementary.h>
4 #define BASE_STEP 360.0
10 typedef enum _Color_Type
18 typedef struct _Colorselector_Data Colorselector_Data;
19 struct _Colorselector_Data
22 Evas_Object *colorbar;
28 Evas_Object *touch_area;
29 Color_Type color_type;
32 typedef struct _Widget_Data Widget_Data;
36 Colorselector_Data *cp[4];
37 Evas_Coord _x, _y, _w, _h;
45 static const char *widtype = NULL;
47 static void _del_hook(Evas_Object *obj);
48 static void _theme_hook(Evas_Object *obj);
49 static void _sizing_eval(Evas_Object *obj);
50 static void _rgb_to_hsl(void *data);
51 static void _hsl_to_rgb(void *data);
52 static void _color_with_saturation(void *data);
53 static void _color_with_lightness(void *data);
54 static void _draw_rects(void *data, double x);
55 static void _arrow_cb(void *data, Evas_Object *obj, const char *emission, const char *source);
56 static void _colorbar_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
57 static void _left_button_clicked_cb(void *data, Evas_Object * obj, void *event_info);
58 static void _left_button_repeat_cb(void *data, Evas_Object * obj, void *event_info);
59 static void _right_button_clicked_cb(void *data, Evas_Object * obj, void *event_info);
60 static void _right_button_repeat_cb(void *data, Evas_Object * obj, void *event_info);
61 static void _add_colorbar(Evas_Object *obj);
62 static void _set_color(Evas_Object *obj, int r, int g, int b, int a);
64 static const char SIG_CHANGED[] = "changed";
66 static const Evas_Smart_Cb_Description _signals[] =
73 _del_hook(Evas_Object *obj)
75 Widget_Data *wd = elm_widget_data_get(obj);
79 for (i = 0; i < 4; i++) free(wd->cp[i]);
84 _theme_hook(Evas_Object *obj)
86 Widget_Data *wd = elm_widget_data_get(obj);
89 if ((!wd) || (!wd->base)) return;
91 _elm_theme_object_set(obj, wd->base, "colorselector", "bg",
92 elm_widget_style_get(obj));
94 for (i = 0; i < 4; i++)
96 evas_object_del(wd->cp[i]->colorbar);
97 wd->cp[i]->colorbar = NULL;
98 evas_object_del(wd->cp[i]->bar);
99 wd->cp[i]->bar = NULL;
100 evas_object_del(wd->cp[i]->lbt);
101 wd->cp[i]->lbt = NULL;
102 evas_object_del(wd->cp[i]->rbt);
103 wd->cp[i]->rbt = NULL;
106 evas_object_del(wd->cp[i]->bg_rect);
107 wd->cp[i]->bg_rect = NULL;
109 evas_object_del(wd->cp[i]->arrow);
110 wd->cp[i]->arrow = NULL;
111 evas_object_del(wd->cp[i]->touch_area);
112 wd->cp[i]->touch_area = NULL;
116 elm_colorselector_color_set(obj, wd->r, wd->g, wd->b, wd->a);
121 _colorselector_set_size_hints(Evas_Object *obj, int timesw, int timesh)
123 Evas_Coord minw = -1, minh = -1;
125 elm_coords_finger_size_adjust(timesw, &minw, timesh, &minh);
126 edje_object_size_min_restricted_calc(obj, &minw, &minh,
128 evas_object_size_hint_min_set(obj, minw, minh);
129 evas_object_size_hint_max_set(obj, -1, -1);
133 _sizing_eval(Evas_Object *obj)
135 Widget_Data *wd = elm_widget_data_get(obj);
136 Evas_Coord minw = -1, minh = -1;
140 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
141 for (i = 0; i < 4; i++)
143 if (wd->cp[i]->bg_rect)
144 _colorselector_set_size_hints(wd->cp[i]->bg_rect, 1, 1);
145 _colorselector_set_size_hints(wd->cp[i]->bar, 1, 1);
146 _colorselector_set_size_hints(wd->cp[i]->rbt, 1, 1);
147 _colorselector_set_size_hints(wd->cp[i]->lbt, 1, 1);
149 _colorselector_set_size_hints(wd->cp[i]->colorbar, 4, 1);
152 elm_coords_finger_size_adjust(4, &minw, 4, &minh);
153 edje_object_size_min_restricted_calc(wd->base, &minw, &minh, minw, minh);
154 evas_object_size_hint_min_set(obj, minw, minh);
155 evas_object_size_hint_max_set(obj, -1, -1);
159 _rgb_to_hsl(void *data)
161 Widget_Data *wd = data;
184 wd->l = (m + v) / 2.0;
186 if (wd->l <= 0.0) return;
191 if (wd->s > 0.0) wd->s /= (wd->l <= 0.5) ? (v + m) : (2.0 - v - m);
198 if (r == v) wd->h = (g == m ? 5.0 + b2 : 1.0 - g2);
199 else if (g == v) wd->h = (b == m ? 1.0 + r2 : 3.0 - b2);
200 else wd->h = (r == m ? 3.0 + g2 : 5.0 - r2);
206 _hsl_to_rgb(void *data)
208 Widget_Data *wd = data;
209 double r = 0, g = 0, b = 0;
212 double sv, vsf, f, p, q, t, v;
218 if (_s == 0.0) r = g = b = _l;
221 if (_h == 360.0) _h = 0.0;
224 v = (_l <= 0.5) ? (_l * (1.0 + _s)) : (_l + _s - (_l * _s));
227 if (v) sv = (v - p) / v;
272 i = (int)(r * 255.0);
274 wd->r = (f <= 0.5) ? i : (i + 1);
276 i = (int)(g * 255.0);
278 wd->g = (f <= 0.5) ? i : (i + 1);
280 i = (int)(b * 255.0);
282 wd->b = (f <= 0.5) ? i : (i + 1);
286 _color_with_saturation(void *data)
288 Widget_Data *wd = data;
291 wd->sr = 127 + (int)((double)(wd->er - 127) * wd->s);
293 wd->sr = 127 - (int)((double)(127 - wd->er) * wd->s);
296 wd->sg = 127 + (int)((double)(wd->eg - 127) * wd->s);
298 wd->sg = 127 - (int)((double)(127 - wd->eg) * wd->s);
301 wd->sb = 127 + (int)((double)(wd->eb - 127) * wd->s);
303 wd->sb = 127 - (int)((double)(127 - wd->eb) * wd->s);
307 _color_with_lightness(void *data)
309 Widget_Data *wd = data;
313 wd->lr = wd->er + (int)((double)(255 - wd->er) * (wd->l - 0.5) * 2.0);
314 wd->lg = wd->eg + (int)((double)(255 - wd->eg) * (wd->l - 0.5) * 2.0);
315 wd->lb = wd->eb + (int)((double)(255 - wd->eb) * (wd->l - 0.5) * 2.0);
317 else if (wd->l < 0.5)
319 wd->lr = (double)wd->er * wd->l * 2.0;
320 wd->lg = (double)wd->eg * wd->l * 2.0;
321 wd->lb = (double)wd->eb * wd->l * 2.0;
332 _draw_rects(void *data, double x)
334 Colorselector_Data *cp = data;
335 Widget_Data *wd = elm_widget_data_get(cp->parent);
336 double one_six = 1.0 / 6.0;
338 switch (cp->color_type)
346 wd->eg = (255.0 * x * 6.0);
349 else if (x < 2 * one_six)
351 wd->er = 255 - (int)(255.0 * (x - one_six) * 6.0);
355 else if (x < 3 * one_six)
359 wd->eb = (int)(255.0 * (x - (2.0 * one_six)) * 6.0);
361 else if (x < 4 * one_six)
364 wd->eg = 255 - (int)(255.0 * (x - (3.0 * one_six)) * 6.0);
367 else if (x < 5 * one_six)
369 wd->er = 255.0 * (x - (4.0 * one_six)) * 6.0;
377 wd->eb = 255 - (int)(255.0 * (x - (5.0 * one_six)) * 6.0);
380 evas_object_color_set(wd->cp[0]->arrow, wd->er, wd->eg, wd->eb, 255);
381 evas_object_color_set(wd->cp[1]->bg_rect, wd->er, wd->eg, wd->eb, 255);
382 evas_object_color_set(wd->cp[2]->bg_rect, wd->er, wd->eg, wd->eb, 255);
383 evas_object_color_set(wd->cp[3]->bar, wd->er, wd->eg, wd->eb, 255);
385 _color_with_saturation(wd);
386 evas_object_color_set(wd->cp[1]->arrow, wd->sr, wd->sg, wd->sb, 255);
388 _color_with_lightness(wd);
389 evas_object_color_set(wd->cp[2]->arrow, wd->lr, wd->lg, wd->lb, 255);
391 evas_object_color_set(wd->cp[3]->arrow,
392 (wd->er * wd->a) / 255,
393 (wd->eg * wd->a) / 255,
394 (wd->eb * wd->a) / 255,
399 _color_with_saturation(wd);
400 evas_object_color_set(wd->cp[1]->arrow, wd->sr, wd->sg, wd->sb, 255);
404 _color_with_lightness(wd);
405 evas_object_color_set(wd->cp[2]->arrow, wd->lr, wd->lg, wd->lb, 255);
409 evas_object_color_set(wd->cp[3]->arrow,
410 (wd->er * wd->a) / 255,
411 (wd->eg * wd->a) / 255,
412 (wd->eb * wd->a) / 255,
422 _arrow_cb(void *data, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
424 Colorselector_Data *cp = data;
427 edje_object_part_drag_value_get(obj, "elm.arrow", &x, &y);
428 _draw_rects(data, x);
429 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
433 _colorbar_cb(void *data, Evas *e, Evas_Object *obj __UNUSED__, void *event_info)
435 Colorselector_Data *cp = data;
436 Evas_Event_Mouse_Down *ev = event_info;
437 Evas_Coord x, y, w, h;
438 double arrow_x = 0, arrow_y;
440 evas_object_geometry_get(cp->bar, &x, &y, &w, &h);
441 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow",
443 if (w > 0) arrow_x = (double)(ev->canvas.x - x) / (double)w;
444 if (arrow_x > 1) arrow_x = 1;
445 if (arrow_x < 0) arrow_x = 0;
446 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", arrow_x, arrow_y);
447 _draw_rects(data, arrow_x);
448 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
449 evas_event_feed_mouse_cancel(e, 0, NULL);
450 evas_event_feed_mouse_down(e, 1, EVAS_BUTTON_NONE, 0, NULL);
454 _left_button_clicked_cb(void *data, Evas_Object * obj __UNUSED__, void *event_info __UNUSED__)
456 Colorselector_Data *cp = data;
459 edje_object_signal_emit(cp->lbt, "elm,state,left,button,down",
461 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
463 switch(cp->color_type)
481 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);
489 _left_button_repeat_cb(void *data, Evas_Object * obj __UNUSED__, void *event_info __UNUSED__)
491 Colorselector_Data *cp = data;
494 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
495 x -= 1.0 / BASE_STEP;
496 if (x < 0.0) x = 0.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);
504 _right_button_clicked_cb(void *data, Evas_Object * obj __UNUSED__, void *event_info __UNUSED__)
506 Colorselector_Data *cp = data;
509 edje_object_signal_emit(cp->rbt, "elm,state,right,button,down",
511 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
513 switch(cp->color_type)
531 if (x > 1.0) x = 1.0;
533 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
534 _draw_rects(data, x);
535 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
539 _right_button_repeat_cb(void *data, Evas_Object * obj __UNUSED__, void *event_info __UNUSED__)
541 Colorselector_Data *cp = data;
544 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
545 x += 1.0 / BASE_STEP;
546 if (x > 1.0) x = 1.0;
547 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
548 _draw_rects(data, x);
549 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
553 _add_colorbar(Evas_Object *obj)
555 char colorbar_name[128];
556 char colorbar_s[128];
562 wd = elm_widget_data_get(obj);
565 e = evas_object_evas_get(obj);
567 for (i = 0; i < 4; i++)
569 wd->cp[i] = ELM_NEW(Colorselector_Data);
570 wd->cp[i]->parent = obj;
574 wd->cp[i]->color_type = HUE;
577 wd->cp[i]->color_type = SATURATION;
580 wd->cp[i]->color_type = LIGHTNESS;
583 wd->cp[i]->color_type = ALPHA;
588 /* load colorbar area */
589 wd->cp[i]->colorbar = edje_object_add(e);
590 _elm_theme_object_set(obj, wd->cp[i]->colorbar, "colorselector", "base",
591 elm_widget_style_get(obj));
592 snprintf(colorbar_name, sizeof(colorbar_name), "colorbar_%d", i);
593 snprintf(colorbar_s, sizeof(colorbar_s), "elm.colorbar_%d", i);
594 edje_object_signal_callback_add(wd->cp[i]->colorbar, "drag", "*",
595 _arrow_cb, wd->cp[i]);
596 edje_object_part_swallow(wd->base, colorbar_s, wd->cp[i]->colorbar);
597 elm_widget_sub_object_add(obj, wd->cp[i]->colorbar);
599 /* load colorbar image */
600 wd->cp[i]->bar = edje_object_add(e);
601 snprintf(buf, sizeof(buf), "%s/%s", colorbar_name, elm_widget_style_get(obj));
602 _elm_theme_object_set(obj, wd->cp[i]->bar, "colorselector", "image",
604 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar",
606 elm_widget_sub_object_add(obj, wd->cp[i]->bar);
608 /* provide expanded touch area */
609 wd->cp[i]->touch_area = evas_object_rectangle_add(e);
610 evas_object_color_set(wd->cp[i]->touch_area, 0, 0, 0, 0);
611 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.arrow_bg",
612 wd->cp[i]->touch_area);
613 evas_object_event_callback_add(wd->cp[i]->touch_area,
614 EVAS_CALLBACK_MOUSE_DOWN, _colorbar_cb,
616 elm_widget_sub_object_add(obj, wd->cp[i]->touch_area);
618 /* load background rectangle of the colorbar. used for
619 changing color of the opacity bar */
620 if ((i == 1) || (i == 2))
622 wd->cp[i]->bg_rect = evas_object_rectangle_add(e);
623 evas_object_color_set(wd->cp[i]->bg_rect, wd->er, wd->eg, wd->eb,
625 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar_bg",
628 elm_widget_sub_object_add(obj, wd->cp[i]->bg_rect);
632 wd->cp[i]->bg_rect = edje_object_add(e);
633 snprintf(buf, sizeof(buf), "%s/%s", colorbar_name, elm_widget_style_get(obj));
634 _elm_theme_object_set(obj, wd->cp[i]->bg_rect, "colorselector",
636 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar_bg",
638 elm_widget_sub_object_add(obj, wd->cp[i]->bg_rect);
639 evas_object_color_set(wd->cp[i]->bar, wd->er, wd->eg, wd->eb, 255);
641 /* load arrow image, pointing the colorbar */
642 wd->cp[i]->arrow = edje_object_add(e);
643 _elm_theme_object_set(obj, wd->cp[i]->arrow, "colorselector", "arrow",
644 elm_widget_style_get(obj));
645 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.arrow_icon",
647 elm_widget_sub_object_add(obj, wd->cp[i]->arrow);
649 evas_object_color_set(wd->cp[i]->arrow, 0, 0, 0, 255);
651 evas_object_color_set(wd->cp[i]->arrow, wd->er, wd->eg, wd->eb, 255);
653 /* load left button */
654 wd->cp[i]->lbt = elm_button_add(obj);
655 snprintf(buf, sizeof(buf), "colorselector/left/%s", elm_widget_style_get(obj));
656 elm_object_style_set(wd->cp[i]->lbt, buf);
657 elm_widget_sub_object_add(obj, wd->cp[i]->lbt);
658 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.l_button",
660 evas_object_smart_callback_add(wd->cp[i]->lbt, "clicked", _left_button_clicked_cb, wd->cp[i]);
661 elm_button_autorepeat_set(wd->cp[i]->lbt, EINA_TRUE);
662 elm_button_autorepeat_initial_timeout_set(wd->cp[i]->lbt, _elm_config->longpress_timeout);
663 elm_button_autorepeat_gap_timeout_set(wd->cp[i]->lbt, (1.0 / _elm_config->fps));
664 evas_object_smart_callback_add(wd->cp[i]->lbt, "repeated",_left_button_repeat_cb, wd->cp[i]);
666 /* load right button */
667 wd->cp[i]->rbt = elm_button_add(obj);
668 snprintf(buf, sizeof(buf), "colorselector/right/%s", elm_widget_style_get(obj));
669 elm_object_style_set(wd->cp[i]->rbt, buf);
670 elm_widget_sub_object_add(obj, wd->cp[i]->rbt);
671 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.r_button",
673 evas_object_smart_callback_add(wd->cp[i]->rbt, "clicked", _right_button_clicked_cb, wd->cp[i]);
674 elm_button_autorepeat_set(wd->cp[i]->rbt, EINA_TRUE);
675 elm_button_autorepeat_initial_timeout_set(wd->cp[i]->rbt, _elm_config->longpress_timeout);
676 elm_button_autorepeat_gap_timeout_set(wd->cp[i]->rbt, (1.0 / _elm_config->fps));
677 evas_object_smart_callback_add(wd->cp[i]->rbt, "repeated",_right_button_repeat_cb, wd->cp[i]);
682 _set_color(Evas_Object *obj, int r, int g, int b, int a)
684 Widget_Data *wd = elm_widget_data_get(obj);
694 edje_object_part_drag_value_get(wd->cp[0]->colorbar, "elm.arrow", &x, &y);
696 edje_object_part_drag_value_set(wd->cp[0]->colorbar, "elm.arrow", x, y);
697 _draw_rects(wd->cp[0], x);
699 edje_object_part_drag_value_get(wd->cp[1]->colorbar, "elm.arrow", &x, &y);
701 edje_object_part_drag_value_set(wd->cp[1]->colorbar, "elm.arrow", x, y);
702 _draw_rects(wd->cp[1], x);
704 edje_object_part_drag_value_get(wd->cp[2]->colorbar, "elm.arrow", &x, &y);
706 edje_object_part_drag_value_set(wd->cp[2]->colorbar, "elm.arrow", x, y);
707 _draw_rects(wd->cp[2], x);
709 edje_object_part_drag_value_get(wd->cp[3]->colorbar, "elm.arrow", &x, &y);
711 edje_object_part_drag_value_set(wd->cp[3]->colorbar, "elm.arrow", x, y);
712 _draw_rects(wd->cp[3], x);
716 elm_colorselector_add(Evas_Object *parent)
718 Evas_Object *obj = NULL;
719 Widget_Data *wd = NULL;
722 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
724 ELM_SET_WIDTYPE(widtype, "colorselector");
725 elm_widget_type_set(obj, "colorselector");
726 elm_widget_sub_object_add(parent, obj);
727 elm_widget_data_set(obj, wd);
728 elm_widget_del_hook_set(obj, _del_hook);
729 elm_widget_theme_hook_set(obj, _theme_hook);
731 /* load background edj */
732 wd->base = edje_object_add(e);
733 _elm_theme_object_set(obj, wd->base, "colorselector", "bg", "default");
734 elm_widget_resize_object_set(obj, wd->base);
748 evas_object_smart_callbacks_descriptions_set(obj, _signals);
753 elm_colorselector_color_set(Evas_Object *obj, int r, int g, int b, int a)
755 ELM_CHECK_WIDTYPE(obj, widtype);
756 _set_color(obj, r, g, b, a);
760 elm_colorselector_color_get(const Evas_Object *obj, int *r, int *g, int *b, int*a)
762 Widget_Data *wd = elm_widget_data_get(obj);
763 ELM_CHECK_WIDTYPE(obj, widtype);