Color selector example

This example shows how to change the color of a rectangle using a color selector. We aren't going to explain a lot of the code since it's the usual setup code:

//Compile with:
//gcc -g colorselector_example_01.c -o colorselector_example_01 `pkg-config --cflags --libs elementary`
#include <Elementary.h>
static void _change_color(void *data, Evas_Object *obj, void *event_info);
static void _colorpalette_clicked_cb(void *data, Evas_Object *obj, void *event_info);
static void _colorpalette_longpressed_cb(void *data, Evas_Object *obj, void *event_info);
EAPI_MAIN int
elm_main(int argc, char **argv)
{
Evas_Object *win, *cs, *rect, *bx, *fr;
elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
win = elm_win_util_standard_add("color selector", "Color selector");
elm_win_autodel_set(win, EINA_TRUE);
bx = elm_box_add(win);
evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_show(bx);
fr = elm_frame_add(win);
evas_object_size_hint_weight_set(fr, 1.0, 0.5);
evas_object_size_hint_align_set(fr, EVAS_HINT_FILL, EVAS_HINT_FILL);
elm_object_text_set(fr, "Color View");
evas_object_show(fr);
rect = evas_object_rectangle_add(evas_object_evas_get(win));
elm_object_content_set(fr, rect);
evas_object_color_set(rect, 255, 90, 18, 255);
evas_object_show(rect);

Now that we have a window with background and a rectangle we can create our color_selector

fr = elm_frame_add(win);
evas_object_size_hint_weight_set(fr, 1.0, 0.5);
evas_object_size_hint_align_set(fr, EVAS_HINT_FILL, EVAS_HINT_FILL);
elm_object_text_set(fr, "Color Selector");
evas_object_show(fr);

Now colors can be loaded to color selector's palette by setting the palette name

evas_object_size_hint_weight_set(cs, EVAS_HINT_EXPAND, 0.0);
evas_object_size_hint_align_set(cs, EVAS_HINT_FILL, 0.0);
elm_colorselector_color_set(cs, 255, 90, 18, 255);
evas_object_show(cs);

Next we ask to be notified whenever the color changes on selector:

evas_object_smart_callback_add(cs, "changed", _change_color, rect);

Next we ask to be notified whenever the color item is selected and longpressed:

evas_object_smart_callback_add(cs, "color,item,selected", _colorpalette_clicked_cb, rect);
evas_object_smart_callback_add(cs, "color,item,longpressed", _colorpalette_longpressed_cb, rect);

We add some more code to the usual setup code:

elm_object_content_set(fr, cs);
evas_object_resize(win, 320, 480);
evas_object_show(win);
return 0;
}

now get to the "changed" callback that sets the color of the rectangle:

static void
_change_color(void *data, Evas_Object *obj, void *event_info)
{
int r, g, b, a;
elm_colorselector_color_get(obj, &r, &g, &b, &a);
// ensure colors are pre-multiplied by alpha
evas_color_argb_premul(a, &r, &g, &b);
evas_object_color_set(data, r, g, b, a);
}

And now get to the "color,item,selected" callback that sets the color of the rectangle:

static void
_colorpalette_clicked_cb(void *data, Evas_Object *obj, void *event_info)
{
int r = 0, g = 0, b = 0 ,a = 0;
Elm_Object_Item *color_it = (Elm_Object_Item *) event_info;
elm_colorselector_palette_item_color_get(color_it, &r, &g, &b, &a);
// ensure colors are pre-multiplied by alpha
evas_color_argb_premul(a, &r, &g, &b);
evas_object_color_set(data, r, g, b, a);
}

And now get to the "color,item,longpressed" callback that gets and displays the color of the rectangle:

static void
_colorpalette_longpressed_cb(void *data, Evas_Object *obj, void *event_info)
{
int r = 0,g = 0,b = 0 ,a = 0;
Elm_Object_Item *color_it = (Elm_Object_Item *) event_info;
elm_colorselector_palette_item_color_get(color_it, &r, &g, &b, &a);
printf("\ncolor = %d-%d-%d-%d\n", r, g, b, a);
}

This example will look like this:

colorselector_example_01.png