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