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