Merge branch 'master' into svn_merge
[framework/uifw/elementary.git] / src / lib / elm_colorselector.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 #define BASE_STEP 360.0
5 #define HUE_STEP 360.0
6 #define SAT_STEP 128.0
7 #define LIG_STEP 256.0
8 #define ALP_STEP 256.0
9
10 typedef enum _Button_State
11 {
12    BUTTON_RELEASED,
13    L_BUTTON_PRESSED,
14    R_BUTTON_PRESSED
15 } Button_State;
16
17 typedef enum _Color_Type
18 {
19    HUE,
20    SATURATION,
21    LIGHTNESS,
22    ALPHA
23 } Color_Type;
24
25 typedef struct _Colorselector_Data Colorselector_Data;
26 struct _Colorselector_Data
27 {
28    Evas_Object *parent;
29    Evas_Object *colorbar;
30    Evas_Object *bar;
31    Evas_Object *lbt;
32    Evas_Object *rbt;
33    Evas_Object *bg_rect;
34    Evas_Object *arrow;
35    Evas_Object *touch_area;
36    Color_Type color_type;
37    Button_State button_state;
38 };
39
40 typedef struct _Widget_Data Widget_Data;
41 struct _Widget_Data
42 {
43    Evas_Object *base;
44    Colorselector_Data *cp[4];
45    Evas_Coord _x, _y, _w, _h;
46    int r, g, b, a;
47    int er, eg, eb;
48    int sr, sg, sb;
49    int lr, lg, lb;
50    double h, s, l;
51    Ecore_Timer *lp_timer;
52    Ecore_Timer *mv_timer;
53 };
54
55 static const char *widtype = NULL;
56
57 static void _del_hook(Evas_Object *obj);
58 static void _theme_hook(Evas_Object *obj);
59 static void _sizing_eval(Evas_Object *obj);
60 static void _rgb_to_hsl(void *data);
61 static void _hsl_to_rgb(void *data);
62 static void _color_with_saturation(void *data);
63 static void _color_with_lightness(void *data);
64 static void _draw_rects(void *data, double x);
65 static void _arrow_cb(void *data, Evas_Object *obj, const char *emission, const char *source);
66 static void _colorbar_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
67 static Eina_Bool _mv_timer(void *data);
68 static Eina_Bool _long_press_timer(void *data);
69 static void _left_button_down_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
70 static void _right_button_down_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
71 static void _left_button_up_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
72 static void _right_button_up_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
73 static void _add_colorbar(Evas_Object *obj);
74 static void _set_color(Evas_Object *obj, int r, int g, int b, int a);
75
76 static const char SIG_CHANGED[] = "changed";
77
78 static const Evas_Smart_Cb_Description _signals[] =
79 {
80      {SIG_CHANGED, ""},
81      {NULL, NULL}
82 };
83
84 static void
85 _del_hook(Evas_Object *obj)
86 {
87    Widget_Data *wd = elm_widget_data_get(obj);
88    int i = 0;
89
90    if (!wd) return;
91    if (wd->lp_timer) ecore_timer_del(wd->lp_timer);
92    if (wd->mv_timer) ecore_timer_del(wd->mv_timer);
93    for (i = 0; i < 4; i++) free(wd->cp[i]);
94    free(wd);
95 }
96
97 static void
98 _theme_hook(Evas_Object *obj)
99 {
100    Widget_Data *wd = elm_widget_data_get(obj);
101    int i;
102
103    if ((!wd) || (!wd->base)) return;
104
105    _elm_theme_object_set(obj, wd->base, "colorselector", "bg",
106                          elm_widget_style_get(obj));
107
108    for (i = 0; i < 4; i++)
109      {
110         evas_object_del(wd->cp[i]->colorbar);
111         wd->cp[i]->colorbar = NULL;
112         evas_object_del(wd->cp[i]->bar);
113         wd->cp[i]->bar = NULL;
114         evas_object_del(wd->cp[i]->lbt);
115         wd->cp[i]->lbt = NULL;
116         evas_object_del(wd->cp[i]->rbt);
117         wd->cp[i]->rbt = NULL;
118         if (i != 0)
119           {
120              evas_object_del(wd->cp[i]->bg_rect);
121              wd->cp[i]->bg_rect = NULL;
122           }
123         evas_object_del(wd->cp[i]->arrow);
124         wd->cp[i]->arrow = NULL;
125         evas_object_del(wd->cp[i]->touch_area);
126         wd->cp[i]->touch_area = NULL;
127      }
128
129    _add_colorbar(obj);
130    _sizing_eval(obj);
131 }
132
133 static void
134 _colorselector_set_size_hints(Evas_Object *obj, int timesw, int timesh)
135 {
136    Evas_Coord minw = -1, minh = -1;
137
138    elm_coords_finger_size_adjust(timesw, &minw, timesh, &minh);
139    edje_object_size_min_restricted_calc(obj, &minw, &minh,
140                                         minw, minh);
141    evas_object_size_hint_min_set(obj, minw, minh);
142    evas_object_size_hint_max_set(obj, -1, -1);
143 }
144
145 static void
146 _sizing_eval(Evas_Object *obj)
147 {
148    Widget_Data *wd = elm_widget_data_get(obj);
149    Evas_Coord minw = -1, minh = -1;
150    int i;
151
152    if (!wd) return;
153    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
154    for (i = 0; i < 4; i++)
155      {
156         if (wd->cp[i]->bg_rect)
157           _colorselector_set_size_hints(wd->cp[i]->bg_rect, 1, 1);
158         _colorselector_set_size_hints(wd->cp[i]->bar, 1, 1);
159         _colorselector_set_size_hints(wd->cp[i]->rbt, 1, 1);
160         _colorselector_set_size_hints(wd->cp[i]->lbt, 1, 1);
161
162         _colorselector_set_size_hints(wd->cp[i]->colorbar, 4, 1);
163      }
164
165    elm_coords_finger_size_adjust(4, &minw, 4, &minh);
166    edje_object_size_min_restricted_calc(wd->base, &minw, &minh, minw, minh);
167    evas_object_size_hint_min_set(obj, minw, minh);
168    evas_object_size_hint_max_set(obj, -1, -1);
169 }
170
171 static void
172 _rgb_to_hsl(void *data)
173 {
174    Widget_Data *wd = data;
175    double r, g, b;
176    double v, m, vm;
177    double r2, g2, b2;
178
179    r = wd->r;
180    g = wd->g;
181    b = wd->b;
182
183    r /= 255.0;
184    g /= 255.0;
185    b /= 255.0;
186
187    v = (r > g) ? r : g;
188    v = (v > b) ? v : b;
189
190    m = (r < g) ? r : g;
191    m = (m < b) ? m : b;
192
193    wd->h = 0.0;
194    wd->s = 0.0;
195    wd->l = 0.0;
196
197    wd->l = (m + v) / 2.0;
198
199    if (wd->l <= 0.0) return;
200
201    vm = v - m;
202    wd->s = vm;
203
204    if (wd->s > 0.0) wd->s /= (wd->l <= 0.5) ? (v + m) : (2.0 - v - m);
205    else return;
206
207    r2 = (v - r) / vm;
208    g2 = (v - g) / vm;
209    b2 = (v - b) / vm;
210
211    if (r == v) wd->h = (g == m ? 5.0 + b2 : 1.0 - g2);
212    else if (g == v) wd->h = (b == m ? 1.0 + r2 : 3.0 - b2);
213    else wd->h = (r == m ? 3.0 + g2 : 5.0 - r2);
214
215    wd->h *= 60.0;
216 }
217
218 static void
219 _hsl_to_rgb(void *data)
220 {
221    Widget_Data *wd = data;
222    double r = 0, g = 0, b = 0;
223    double _h, _s, _l;
224    int i = 0;
225    double sv, vsf, f, p, q, t, v;
226
227    _h = wd->h;
228    _s = wd->s;
229    _l = wd->l;
230
231    if (_s == 0.0) r = g = b = _l;
232    else
233      {
234         if (_h == 360.0) _h = 0.0;
235         _h /= 60.0;
236
237         v = (_l <= 0.5) ? (_l * (1.0 + _s)) : (_l + _s - (_l * _s));
238         p = _l + _l - v;
239
240         if (v) sv = (v - p) / v;
241         else sv = 0;
242
243         i = (int)_h;
244         f = _h - i;
245
246         vsf = v * sv * f;
247
248         t = p + vsf;
249         q = v - vsf;
250
251         switch (i)
252           {
253            case 0:
254               r = v;
255               g = t;
256               b = p;
257               break;
258            case 1:
259               r = q;
260               g = v;
261               b = p;
262               break;
263            case 2:
264               r = p;
265               g = v;
266               b = t;
267               break;
268            case 3:
269               r = p;
270               g = q;
271               b = v;
272               break;
273            case 4:
274               r = t;
275               g = p;
276               b = v;
277               break;
278            case 5:
279               r = v;
280               g = p;
281               b = q;
282               break;
283           }
284      }
285    i = (int)(r * 255.0);
286    f = (r * 255.0) - i;
287    wd->r = (f <= 0.5) ? i : (i + 1);
288
289    i = (int)(g * 255.0);
290    f = (g * 255.0) - i;
291    wd->g = (f <= 0.5) ? i : (i + 1);
292
293    i = (int)(b * 255.0);
294    f = (b * 255.0) - i;
295    wd->b = (f <= 0.5) ? i : (i + 1);
296 }
297
298 static void
299 _color_with_saturation(void *data)
300 {
301    Widget_Data *wd = data;
302
303    if (wd->er > 127)
304      wd->sr = 127 + (int)((double)(wd->er - 127) * wd->s);
305    else
306      wd->sr = 127 - (int)((double)(127 - wd->er) * wd->s);
307
308    if (wd->eg > 127)
309      wd->sg = 127 + (int)((double)(wd->eg - 127) * wd->s);
310    else
311      wd->sg = 127 - (int)((double)(127 - wd->eg) * wd->s);
312
313    if (wd->eb > 127)
314      wd->sb = 127 + (int)((double)(wd->eb - 127) * wd->s);
315    else
316      wd->sb = 127 - (int)((double)(127 - wd->eb) * wd->s);
317 }
318
319 static void
320 _color_with_lightness(void *data)
321 {
322    Widget_Data *wd = data;
323
324    if (wd->l > 0.5)
325      {
326         wd->lr = wd->er + (int)((double)(255 - wd->er) * (wd->l - 0.5) * 2.0);
327         wd->lg = wd->eg + (int)((double)(255 - wd->eg) * (wd->l - 0.5) * 2.0);
328         wd->lb = wd->eb + (int)((double)(255 - wd->eb) * (wd->l - 0.5) * 2.0);
329      }
330    else if (wd->l < 0.5)
331      {
332         wd->lr = (double)wd->er * wd->l * 2.0;
333         wd->lg = (double)wd->eg * wd->l * 2.0;
334         wd->lb = (double)wd->eb * wd->l * 2.0;
335      }
336    else
337      {
338         wd->lr = wd->er;
339         wd->lg = wd->eg;
340         wd->lb = wd->eb;
341      }
342 }
343
344 static void
345 _draw_rects(void *data, double x)
346 {
347    Colorselector_Data *cp = data;
348    Widget_Data *wd = elm_widget_data_get(cp->parent);
349    double one_six = 1.0 / 6.0;
350
351    switch (cp->color_type)
352      {
353       case HUE:
354          wd->h = 360.0 * x;
355
356          if (x < one_six)
357            {
358               wd->er = 255;
359               wd->eg = (255.0 * x * 6.0);
360               wd->eb = 0;
361            }
362          else if (x < 2 * one_six)
363            {
364               wd->er = 255 - (int)(255.0 * (x - one_six) * 6.0);
365               wd->eg = 255;
366               wd->eb = 0;
367            }
368          else if (x < 3 * one_six)
369            {
370               wd->er = 0;
371               wd->eg = 255;
372               wd->eb = (int)(255.0 * (x - (2.0 * one_six)) * 6.0);
373            }
374          else if (x < 4 * one_six)
375            {
376               wd->er = 0;
377               wd->eg = 255 - (int)(255.0 * (x - (3.0 * one_six)) * 6.0);
378               wd->eb = 255;
379            }
380          else if (x < 5 * one_six)
381            {
382               wd->er = 255.0 * (x - (4.0 * one_six)) * 6.0;
383               wd->eg = 0;
384               wd->eb = 255;
385            }
386          else
387            {
388               wd->er = 255;
389               wd->eg = 0;
390               wd->eb = 255 - (int)(255.0 * (x - (5.0 * one_six)) * 6.0);
391            }
392
393          evas_object_color_set(wd->cp[0]->arrow, wd->er, wd->eg, wd->eb, 255);
394          evas_object_color_set(wd->cp[1]->bg_rect, wd->er, wd->eg, wd->eb, 255);
395          evas_object_color_set(wd->cp[2]->bg_rect, wd->er, wd->eg, wd->eb, 255);
396          evas_object_color_set(wd->cp[3]->bar, wd->er, wd->eg, wd->eb, 255);
397
398          _color_with_saturation(wd);
399          evas_object_color_set(wd->cp[1]->arrow, wd->sr, wd->sg, wd->sb, 255);
400
401          _color_with_lightness(wd);
402          evas_object_color_set(wd->cp[2]->arrow, wd->lr, wd->lg, wd->lb, 255);
403
404          evas_object_color_set(wd->cp[3]->arrow,
405                                (wd->er * wd->a) / 255,
406                                (wd->eg * wd->a) / 255,
407                                (wd->eb * wd->a) / 255,
408                                wd->a);
409          break;
410       case SATURATION:
411          wd->s = 1.0 - x;
412          _color_with_saturation(wd);
413          evas_object_color_set(wd->cp[1]->arrow, wd->sr, wd->sg, wd->sb, 255);
414          break;
415       case LIGHTNESS:
416          wd->l = x;
417          _color_with_lightness(wd);
418          evas_object_color_set(wd->cp[2]->arrow, wd->lr, wd->lg, wd->lb, 255);
419          break;
420       case ALPHA:
421          wd->a = 255.0 * x;
422          evas_object_color_set(wd->cp[3]->arrow, wd->er, wd->eg, wd->eb, wd->a);
423          break;
424       default:
425          break;
426      }
427    _hsl_to_rgb(wd);
428 }
429
430 static void
431 _arrow_cb(void *data, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
432 {
433    Colorselector_Data *cp = data;
434    double x, y;
435
436    edje_object_part_drag_value_get(obj, "elm.arrow", &x, &y);
437    _draw_rects(data, x);
438    evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
439 }
440
441 static void
442 _colorbar_cb(void *data, Evas *e, Evas_Object *obj __UNUSED__, void *event_info)
443 {
444    Colorselector_Data *cp = data;
445    Evas_Event_Mouse_Down *ev = event_info;
446    Evas_Coord x, y, w, h;
447    double arrow_x = 0, arrow_y;
448
449    evas_object_geometry_get(cp->bar, &x, &y, &w, &h);
450    edje_object_part_drag_value_get(cp->colorbar, "elm.arrow",
451                                    &arrow_x, &arrow_y);
452    if (w > 0) arrow_x = (double)(ev->canvas.x - x) / (double)w;
453    if (arrow_x > 1) arrow_x = 1;
454    if (arrow_x < 0) arrow_x = 0;
455    edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", arrow_x, arrow_y);
456    _draw_rects(data, arrow_x);
457    evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
458    evas_event_feed_mouse_cancel(e, 0, NULL);
459    evas_event_feed_mouse_down(e, 1, EVAS_BUTTON_NONE, 0, NULL);
460 }
461
462 static Eina_Bool
463 _mv_timer(void *data)
464 {
465    Colorselector_Data *cp = data;
466    Widget_Data *wd = elm_widget_data_get(cp->parent);
467    double x, y;
468
469    if (!wd) return EINA_FALSE;
470
471    edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
472    if (cp->button_state == L_BUTTON_PRESSED)
473      {
474         x -= 1.0 / BASE_STEP;
475         if (x < 0.0) x = 0.0;
476         edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
477         _draw_rects(data, x);
478         evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
479         return EINA_TRUE;
480      }
481    else if (cp->button_state == R_BUTTON_PRESSED)
482      {
483         x += 1.0 / BASE_STEP;
484         if (x > 1.0) x = 1.0;
485         edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
486         _draw_rects(data, x);
487         evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
488         return EINA_TRUE;
489      }
490    wd->mv_timer = NULL;
491    return EINA_FALSE;
492 }
493
494 static Eina_Bool
495 _long_press_timer(void *data)
496 {
497    Colorselector_Data *cp = data;
498    Widget_Data *wd = elm_widget_data_get(cp->parent);
499
500    if (wd->mv_timer) ecore_timer_del(wd->mv_timer);
501    wd->mv_timer = ecore_timer_add(0.01, _mv_timer, cp);
502
503    wd->lp_timer = NULL;
504    return EINA_FALSE;
505 }
506
507 static void
508 _left_button_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
509 {
510    Colorselector_Data *cp = data;
511    Widget_Data *wd = elm_widget_data_get(cp->parent);
512    double x, y;
513
514    edje_object_signal_emit(cp->lbt, "elm,state,left,button,down",
515                            "left_button");
516    edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
517
518    switch(cp->color_type)
519      {
520       case HUE :
521          x -= 1.0 / HUE_STEP;
522          break;
523       case SATURATION :
524          x -= 1.0 / SAT_STEP;
525          break;
526       case LIGHTNESS :
527          x -= 1.0 / LIG_STEP;
528          break;
529       case ALPHA :
530          x -= 1.0 / ALP_STEP;
531          break;
532       default :
533          break;
534      }
535
536    if (x < 0.0) x = 0.0;
537
538    edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
539    _draw_rects(data, x);
540    evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
541    cp->button_state = L_BUTTON_PRESSED;
542    if (wd->lp_timer) ecore_timer_del(wd->lp_timer);
543    wd->lp_timer = ecore_timer_add(_elm_config->longpress_timeout, _long_press_timer, cp);
544 }
545
546 static void
547 _right_button_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
548 {
549    Colorselector_Data *cp = data;
550    Widget_Data *wd = elm_widget_data_get(cp->parent);
551    double x, y;
552
553    edje_object_signal_emit(cp->rbt, "elm,state,right,button,down",
554                            "right_button");
555    edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
556
557    switch(cp->color_type)
558      {
559       case HUE :
560          x += 1.0 / HUE_STEP;
561          break;
562       case SATURATION :
563          x += 1.0 / SAT_STEP;
564          break;
565       case LIGHTNESS :
566          x += 1.0 / LIG_STEP;
567          break;
568       case ALPHA :
569          x += 1.0 / ALP_STEP;
570          break;
571       default :
572          break;
573      }
574
575    if (x > 1.0) x = 1.0;
576
577    edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
578    _draw_rects(data, x);
579    evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
580    cp->button_state = R_BUTTON_PRESSED;
581    wd->lp_timer = ecore_timer_add(_elm_config->longpress_timeout, _long_press_timer, cp);
582 }
583
584 static void
585 _left_button_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
586 {
587    Colorselector_Data *cp = data;
588    Widget_Data *wd = elm_widget_data_get(cp->parent);
589
590    if (wd->lp_timer)
591      {
592         ecore_timer_del(wd->lp_timer);
593         wd->lp_timer = NULL;
594      }
595    if (wd->mv_timer)
596      {
597         ecore_timer_del(wd->mv_timer);
598         wd->mv_timer = NULL;
599      }
600
601    cp->button_state = BUTTON_RELEASED;
602    edje_object_signal_emit(cp->lbt, "elm,state,left,button,up", "left_button");
603 }
604
605 static void
606 _right_button_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
607 {
608    Colorselector_Data *cp = data;
609    Widget_Data *wd = elm_widget_data_get(cp->parent);
610
611    if (wd->lp_timer)
612      {
613         ecore_timer_del(wd->lp_timer);
614         wd->lp_timer = NULL;
615      }
616    if (wd->mv_timer)
617      {
618         ecore_timer_del(wd->mv_timer);
619         wd->mv_timer = NULL;
620      }
621
622    cp->button_state = BUTTON_RELEASED;
623    edje_object_signal_emit(cp->rbt, "elm,state,right,button,up",
624                            "right_button");
625 }
626
627 static void
628 _add_colorbar(Evas_Object *obj)
629 {
630    char colorbar_name[128];
631    char colorbar_s[128];
632    Widget_Data *wd;
633    Evas *e;
634    int i = 0;
635
636    wd = elm_widget_data_get(obj);
637    if (!wd) return;
638
639    e = evas_object_evas_get(obj);
640
641    for (i = 0; i < 4; i++)
642      {
643         wd->cp[i] = ELM_NEW(Colorselector_Data);
644         wd->cp[i]->parent = obj;
645         switch(i)
646           {
647            case 0 :
648               wd->cp[i]->color_type = HUE;
649               break;
650            case 1 :
651               wd->cp[i]->color_type = SATURATION;
652               break;
653            case 2 :
654               wd->cp[i]->color_type = LIGHTNESS;
655               break;
656            case 3 :
657               wd->cp[i]->color_type = ALPHA;
658               break;
659            default :
660               break;
661           }
662         /* load colorbar area */
663         wd->cp[i]->colorbar = edje_object_add(e);
664         _elm_theme_object_set(obj, wd->cp[i]->colorbar, "colorselector", "base",
665                               "default");
666         snprintf(colorbar_name, sizeof(colorbar_name), "colorbar_%d", i);
667         snprintf(colorbar_s, sizeof(colorbar_s), "elm.colorbar_%d", i);
668         edje_object_signal_callback_add(wd->cp[i]->colorbar, "drag", "*",
669                                         _arrow_cb, wd->cp[i]);
670         edje_object_part_swallow(wd->base, colorbar_s, wd->cp[i]->colorbar);
671         elm_widget_sub_object_add(obj, wd->cp[i]->colorbar);
672
673         /* load colorbar image */
674         wd->cp[i]->bar = edje_object_add(e);
675         _elm_theme_object_set(obj, wd->cp[i]->bar, "colorselector", "image",
676                               colorbar_name);
677         edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar",
678                                  wd->cp[i]->bar);
679         elm_widget_sub_object_add(obj, wd->cp[i]->bar);
680
681         /* provide expanded touch area */
682         wd->cp[i]->touch_area = evas_object_rectangle_add(e);
683         evas_object_color_set(wd->cp[i]->touch_area, 0, 0, 0, 0);
684         edje_object_part_swallow(wd->cp[i]->colorbar, "elm.arrow_bg",
685                                  wd->cp[i]->touch_area);
686         evas_object_event_callback_add(wd->cp[i]->touch_area,
687                                        EVAS_CALLBACK_MOUSE_DOWN, _colorbar_cb,
688                                        wd->cp[i]);
689         elm_widget_sub_object_add(obj, wd->cp[i]->touch_area);
690
691         /* load background rectangle of the colorbar. used for
692            changing color of the opacity bar */
693         if ((i == 1) || (i == 2))
694           {
695              wd->cp[i]->bg_rect = evas_object_rectangle_add(e);
696              evas_object_color_set(wd->cp[i]->bg_rect, wd->er, wd->eg, wd->eb,
697                                    255);
698              edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar_bg",
699                                       wd->cp[i]->bg_rect);
700
701              elm_widget_sub_object_add(obj, wd->cp[i]->bg_rect);
702           }
703         if (i == 3)
704           {
705              wd->cp[i]->bg_rect = edje_object_add(e);
706              _elm_theme_object_set(obj, wd->cp[i]->bg_rect, "colorselector",
707                                    "bg_image", colorbar_name);
708              edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar_bg",
709                                       wd->cp[i]->bg_rect);
710              elm_widget_sub_object_add(obj, wd->cp[i]->bg_rect);
711              evas_object_color_set(wd->cp[i]->bar, wd->er, wd->eg, wd->eb, 255);
712           }
713         /* load arrow image, pointing the colorbar */
714         wd->cp[i]->arrow = edje_object_add(e);
715         _elm_theme_object_set(obj, wd->cp[i]->arrow, "colorselector", "image",
716                               "updown");
717         edje_object_part_swallow(wd->cp[i]->colorbar, "elm.arrow_icon",
718                                  wd->cp[i]->arrow);
719         elm_widget_sub_object_add(obj, wd->cp[i]->arrow);
720         if (i == 2)
721           evas_object_color_set(wd->cp[i]->arrow, 0, 0, 0, 255);
722         else
723           evas_object_color_set(wd->cp[i]->arrow, wd->er, wd->eg, wd->eb, 255);
724
725         /* load left button */
726         wd->cp[i]->lbt = edje_object_add(e);
727         _elm_theme_object_set(obj, wd->cp[i]->lbt, "colorselector", "button",
728                               "left");
729         evas_object_event_callback_add(wd->cp[i]->lbt, EVAS_CALLBACK_MOUSE_DOWN,
730                                        _left_button_down_cb, wd->cp[i]);
731         evas_object_event_callback_add(wd->cp[i]->lbt, EVAS_CALLBACK_MOUSE_UP,
732                                        _left_button_up_cb, wd->cp[i]);
733         edje_object_part_swallow(wd->cp[i]->colorbar, "elm.l_button",
734                                  wd->cp[i]->lbt);
735         elm_widget_sub_object_add(obj, wd->cp[i]->lbt);
736
737         /* load right button */
738         wd->cp[i]->rbt = edje_object_add(e);
739         _elm_theme_object_set(obj, wd->cp[i]->rbt, "colorselector", "button",
740                               "right");
741         evas_object_event_callback_add(wd->cp[i]->rbt, EVAS_CALLBACK_MOUSE_DOWN,
742                                        _right_button_down_cb, wd->cp[i]);
743         evas_object_event_callback_add(wd->cp[i]->rbt, EVAS_CALLBACK_MOUSE_UP,
744                                        _right_button_up_cb, wd->cp[i]);
745         edje_object_part_swallow(wd->cp[i]->colorbar, "elm.r_button",
746                                  wd->cp[i]->rbt);
747         elm_widget_sub_object_add(obj, wd->cp[i]->rbt);
748      }
749 }
750
751 static void
752 _set_color(Evas_Object *obj, int r, int g, int b, int a)
753 {
754    Widget_Data *wd = elm_widget_data_get(obj);
755    double x, y;
756
757    wd->r = r;
758    wd->g = g;
759    wd->b = b;
760    wd->a = a;
761
762    _rgb_to_hsl(wd);
763
764    edje_object_part_drag_value_get(wd->cp[0]->colorbar, "elm.arrow", &x, &y);
765    x = wd->h / 360.0;
766    edje_object_part_drag_value_set(wd->cp[0]->colorbar, "elm.arrow", x, y);
767    _draw_rects(wd->cp[0], x);
768
769    edje_object_part_drag_value_get(wd->cp[1]->colorbar, "elm.arrow", &x, &y);
770    x = 1.0 - wd->s;
771    edje_object_part_drag_value_set(wd->cp[1]->colorbar, "elm.arrow", x, y);
772    _draw_rects(wd->cp[1], x);
773
774    edje_object_part_drag_value_get(wd->cp[2]->colorbar, "elm.arrow", &x, &y);
775    x = wd->l;
776    edje_object_part_drag_value_set(wd->cp[2]->colorbar, "elm.arrow", x, y);
777    _draw_rects(wd->cp[2], x);
778
779    edje_object_part_drag_value_get(wd->cp[3]->colorbar, "elm.arrow", &x, &y);
780    x = wd->a / 255.0;
781    edje_object_part_drag_value_set(wd->cp[3]->colorbar, "elm.arrow", x, y);
782    _draw_rects(wd->cp[3], x);
783 }
784
785 EAPI Evas_Object *
786 elm_colorselector_add(Evas_Object *parent)
787 {
788    Evas_Object *obj = NULL;
789    Widget_Data *wd = NULL;
790    Evas *e;
791
792    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
793
794    ELM_SET_WIDTYPE(widtype, "colorselector");
795    elm_widget_type_set(obj, "colorselector");
796    elm_widget_sub_object_add(parent, obj);
797    elm_widget_data_set(obj, wd);
798    elm_widget_del_hook_set(obj, _del_hook);
799    elm_widget_theme_hook_set(obj, _theme_hook);
800
801    /* load background edj */
802    wd->base = edje_object_add(e);
803    _elm_theme_object_set(obj, wd->base, "colorselector", "bg", "default");
804    elm_widget_resize_object_set(obj, wd->base);
805
806    wd->er = 255;
807    wd->eg = 0;
808    wd->eb = 0;
809    wd->h = 0.0;
810    wd->s = 1.0;
811    wd->l = 0.0;
812    wd->a = 255;
813
814    _hsl_to_rgb(wd);
815    _add_colorbar(obj);
816    _sizing_eval(obj);
817
818    evas_object_smart_callbacks_descriptions_set(obj, _signals);
819    return obj;
820 }
821
822 EAPI void
823 elm_colorselector_color_set(Evas_Object *obj, int r, int g, int b, int a)
824 {
825    ELM_CHECK_WIDTYPE(obj, widtype);
826    _set_color(obj, r, g, b, a);
827 }
828
829 EAPI void
830 elm_colorselector_color_get(const Evas_Object *obj, int *r, int *g, int *b, int*a)
831 {
832    Widget_Data *wd = elm_widget_data_get(obj);
833    ELM_CHECK_WIDTYPE(obj, widtype);
834
835    if (r) *r = wd->r;
836    if (g) *g = wd->g;
837    if (b) *b = wd->b;
838    if (a) *a = wd->a;
839 }