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, wd->er, wd->eg, wd->eb, wd->a);
418 _arrow_cb(void *data, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
420 Colorselector_Data *cp = data;
423 edje_object_part_drag_value_get(obj, "elm.arrow", &x, &y);
424 _draw_rects(data, x);
425 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
429 _colorbar_cb(void *data, Evas *e, Evas_Object *obj __UNUSED__, void *event_info)
431 Colorselector_Data *cp = data;
432 Evas_Event_Mouse_Down *ev = event_info;
433 Evas_Coord x, y, w, h;
434 double arrow_x = 0, arrow_y;
436 evas_object_geometry_get(cp->bar, &x, &y, &w, &h);
437 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow",
439 if (w > 0) arrow_x = (double)(ev->canvas.x - x) / (double)w;
440 if (arrow_x > 1) arrow_x = 1;
441 if (arrow_x < 0) arrow_x = 0;
442 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", arrow_x, arrow_y);
443 _draw_rects(data, arrow_x);
444 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
445 evas_event_feed_mouse_cancel(e, 0, NULL);
446 evas_event_feed_mouse_down(e, 1, EVAS_BUTTON_NONE, 0, NULL);
450 _left_button_clicked_cb(void *data, Evas_Object * obj __UNUSED__, void *event_info __UNUSED__)
452 Colorselector_Data *cp = data;
455 edje_object_signal_emit(cp->lbt, "elm,state,left,button,down",
457 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
459 switch(cp->color_type)
477 if (x < 0.0) x = 0.0;
479 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
480 _draw_rects(data, x);
481 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
485 _left_button_repeat_cb(void *data, Evas_Object * obj __UNUSED__, void *event_info __UNUSED__)
487 Colorselector_Data *cp = data;
490 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
491 x -= 1.0 / BASE_STEP;
492 if (x < 0.0) x = 0.0;
493 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
494 _draw_rects(data, x);
495 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
500 _right_button_clicked_cb(void *data, Evas_Object * obj __UNUSED__, void *event_info __UNUSED__)
502 Colorselector_Data *cp = data;
505 edje_object_signal_emit(cp->rbt, "elm,state,right,button,down",
507 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
509 switch(cp->color_type)
527 if (x > 1.0) x = 1.0;
529 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
530 _draw_rects(data, x);
531 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
535 _right_button_repeat_cb(void *data, Evas_Object * obj __UNUSED__, void *event_info __UNUSED__)
537 Colorselector_Data *cp = data;
540 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
541 x += 1.0 / BASE_STEP;
542 if (x > 1.0) x = 1.0;
543 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
544 _draw_rects(data, x);
545 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
549 _add_colorbar(Evas_Object *obj)
551 char colorbar_name[128];
552 char colorbar_s[128];
558 wd = elm_widget_data_get(obj);
561 e = evas_object_evas_get(obj);
563 for (i = 0; i < 4; i++)
565 wd->cp[i] = ELM_NEW(Colorselector_Data);
566 wd->cp[i]->parent = obj;
570 wd->cp[i]->color_type = HUE;
573 wd->cp[i]->color_type = SATURATION;
576 wd->cp[i]->color_type = LIGHTNESS;
579 wd->cp[i]->color_type = ALPHA;
584 /* load colorbar area */
585 wd->cp[i]->colorbar = edje_object_add(e);
586 _elm_theme_object_set(obj, wd->cp[i]->colorbar, "colorselector", "base",
587 elm_widget_style_get(obj));
588 snprintf(colorbar_name, sizeof(colorbar_name), "colorbar_%d", i);
589 snprintf(colorbar_s, sizeof(colorbar_s), "elm.colorbar_%d", i);
590 edje_object_signal_callback_add(wd->cp[i]->colorbar, "drag", "*",
591 _arrow_cb, wd->cp[i]);
592 edje_object_part_swallow(wd->base, colorbar_s, wd->cp[i]->colorbar);
593 elm_widget_sub_object_add(obj, wd->cp[i]->colorbar);
595 /* load colorbar image */
596 wd->cp[i]->bar = edje_object_add(e);
597 snprintf(buf, sizeof(buf), "%s/%s", colorbar_name, elm_widget_style_get(obj));
598 _elm_theme_object_set(obj, wd->cp[i]->bar, "colorselector", "image",
600 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar",
602 elm_widget_sub_object_add(obj, wd->cp[i]->bar);
604 /* provide expanded touch area */
605 wd->cp[i]->touch_area = evas_object_rectangle_add(e);
606 evas_object_color_set(wd->cp[i]->touch_area, 0, 0, 0, 0);
607 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.arrow_bg",
608 wd->cp[i]->touch_area);
609 evas_object_event_callback_add(wd->cp[i]->touch_area,
610 EVAS_CALLBACK_MOUSE_DOWN, _colorbar_cb,
612 elm_widget_sub_object_add(obj, wd->cp[i]->touch_area);
614 /* load background rectangle of the colorbar. used for
615 changing color of the opacity bar */
616 if ((i == 1) || (i == 2))
618 wd->cp[i]->bg_rect = evas_object_rectangle_add(e);
619 evas_object_color_set(wd->cp[i]->bg_rect, wd->er, wd->eg, wd->eb,
621 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar_bg",
624 elm_widget_sub_object_add(obj, wd->cp[i]->bg_rect);
628 wd->cp[i]->bg_rect = edje_object_add(e);
629 snprintf(buf, sizeof(buf), "%s/%s", colorbar_name, elm_widget_style_get(obj));
630 _elm_theme_object_set(obj, wd->cp[i]->bg_rect, "colorselector",
632 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar_bg",
634 elm_widget_sub_object_add(obj, wd->cp[i]->bg_rect);
635 evas_object_color_set(wd->cp[i]->bar, wd->er, wd->eg, wd->eb, 255);
637 /* load arrow image, pointing the colorbar */
638 wd->cp[i]->arrow = edje_object_add(e);
639 _elm_theme_object_set(obj, wd->cp[i]->arrow, "colorselector", "arrow",
640 elm_widget_style_get(obj));
641 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.arrow_icon",
643 elm_widget_sub_object_add(obj, wd->cp[i]->arrow);
645 evas_object_color_set(wd->cp[i]->arrow, 0, 0, 0, 255);
647 evas_object_color_set(wd->cp[i]->arrow, wd->er, wd->eg, wd->eb, 255);
649 /* load left button */
650 wd->cp[i]->lbt = elm_button_add(obj);
651 snprintf(buf, sizeof(buf), "colorselector/left/%s", elm_widget_style_get(obj));
652 elm_object_style_set(wd->cp[i]->lbt, buf);
653 elm_widget_sub_object_add(obj, wd->cp[i]->lbt);
654 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.l_button",
656 evas_object_smart_callback_add(wd->cp[i]->lbt, "clicked", _left_button_clicked_cb, wd->cp[i]);
657 elm_button_autorepeat_set(wd->cp[i]->lbt, EINA_TRUE);
658 elm_button_autorepeat_initial_timeout_set(wd->cp[i]->lbt, _elm_config->longpress_timeout);
659 elm_button_autorepeat_gap_timeout_set(wd->cp[i]->lbt, (1.0 / _elm_config->fps));
660 evas_object_smart_callback_add(wd->cp[i]->lbt, "repeated",_left_button_repeat_cb, wd->cp[i]);
662 /* load right button */
663 wd->cp[i]->rbt = elm_button_add(obj);
664 snprintf(buf, sizeof(buf), "colorselector/right/%s", elm_widget_style_get(obj));
665 elm_object_style_set(wd->cp[i]->rbt, buf);
666 elm_widget_sub_object_add(obj, wd->cp[i]->rbt);
667 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.r_button",
669 evas_object_smart_callback_add(wd->cp[i]->rbt, "clicked", _right_button_clicked_cb, wd->cp[i]);
670 elm_button_autorepeat_set(wd->cp[i]->rbt, EINA_TRUE);
671 elm_button_autorepeat_initial_timeout_set(wd->cp[i]->rbt, _elm_config->longpress_timeout);
672 elm_button_autorepeat_gap_timeout_set(wd->cp[i]->rbt, (1.0 / _elm_config->fps));
673 evas_object_smart_callback_add(wd->cp[i]->rbt, "repeated",_right_button_repeat_cb, wd->cp[i]);
678 _set_color(Evas_Object *obj, int r, int g, int b, int a)
680 Widget_Data *wd = elm_widget_data_get(obj);
690 edje_object_part_drag_value_get(wd->cp[0]->colorbar, "elm.arrow", &x, &y);
692 edje_object_part_drag_value_set(wd->cp[0]->colorbar, "elm.arrow", x, y);
693 _draw_rects(wd->cp[0], x);
695 edje_object_part_drag_value_get(wd->cp[1]->colorbar, "elm.arrow", &x, &y);
697 edje_object_part_drag_value_set(wd->cp[1]->colorbar, "elm.arrow", x, y);
698 _draw_rects(wd->cp[1], x);
700 edje_object_part_drag_value_get(wd->cp[2]->colorbar, "elm.arrow", &x, &y);
702 edje_object_part_drag_value_set(wd->cp[2]->colorbar, "elm.arrow", x, y);
703 _draw_rects(wd->cp[2], x);
705 edje_object_part_drag_value_get(wd->cp[3]->colorbar, "elm.arrow", &x, &y);
707 edje_object_part_drag_value_set(wd->cp[3]->colorbar, "elm.arrow", x, y);
708 _draw_rects(wd->cp[3], x);
712 elm_colorselector_add(Evas_Object *parent)
714 Evas_Object *obj = NULL;
715 Widget_Data *wd = NULL;
718 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
720 ELM_SET_WIDTYPE(widtype, "colorselector");
721 elm_widget_type_set(obj, "colorselector");
722 elm_widget_sub_object_add(parent, obj);
723 elm_widget_data_set(obj, wd);
724 elm_widget_del_hook_set(obj, _del_hook);
725 elm_widget_theme_hook_set(obj, _theme_hook);
727 /* load background edj */
728 wd->base = edje_object_add(e);
729 _elm_theme_object_set(obj, wd->base, "colorselector", "bg", "default");
730 elm_widget_resize_object_set(obj, wd->base);
744 evas_object_smart_callbacks_descriptions_set(obj, _signals);
749 elm_colorselector_color_set(Evas_Object *obj, int r, int g, int b, int a)
751 ELM_CHECK_WIDTYPE(obj, widtype);
752 _set_color(obj, r, g, b, a);
756 elm_colorselector_color_get(const Evas_Object *obj, int *r, int *g, int *b, int*a)
758 Widget_Data *wd = elm_widget_data_get(obj);
759 ELM_CHECK_WIDTYPE(obj, widtype);