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