aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/e_spectrum.c
blob: b0c76722ff5cc9d09293e9cb85a741b19d4a4085 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include "e.h"

Evas_Smart *_e_spectrum_smart = NULL;

typedef struct _E_Spectrum E_Spectrum;

struct _E_Spectrum
{
   Evas_Object      *o_spectrum;
   Evas_Object      *o_event;
   Evas_Object      *o_cursor;

   int               iw, ih; /* square image width/height */
   E_Color_Component mode;

   E_Color          *cv;
   Ecore_Timer      *draw_timer;
};

static Eina_Bool _e_spectrum_redraw(void *d);

static void
_e_spectrum_smart_add(Evas_Object *o)
{
   E_Spectrum *sp;
   sp = E_NEW(E_Spectrum, 1);

   if (!sp) return;

   evas_object_smart_data_set(o, sp);

   sp->mode = E_COLOR_COMPONENT_R;

   sp->o_spectrum = evas_object_image_add(evas_object_evas_get(o));
   sp->iw = sp->ih = 255;
   evas_object_image_size_set(sp->o_spectrum, sp->iw, sp->ih);
   evas_object_image_alpha_set(sp->o_spectrum, 1);

   evas_object_smart_member_add(sp->o_spectrum, o);
}

static void
_e_spectrum_smart_del(Evas_Object *o)
{
   E_Spectrum *sp;

   sp = evas_object_smart_data_get(o);
   if (!sp) return;

   evas_object_del(sp->o_spectrum);
   evas_object_del(sp->o_event);
   evas_object_del(sp->o_cursor);

   if (sp->draw_timer) ecore_timer_del(sp->draw_timer);

   E_FREE(sp);
}

static void
_e_spectrum_smart_show(Evas_Object *o)
{
   E_Spectrum *sp;

   sp = evas_object_smart_data_get(o);
   if (!sp) return;

   evas_object_show(sp->o_spectrum);
   evas_object_show(sp->o_event);
   evas_object_show(sp->o_cursor);
}

static void
_e_spectrum_smart_hide(Evas_Object *o)
{
   E_Spectrum *sp;

   sp = evas_object_smart_data_get(o);
   if (!sp) return;

   evas_object_hide(sp->o_spectrum);
   evas_object_hide(sp->o_event);
   evas_object_hide(sp->o_cursor);
}

static void
_e_spectrum_smart_move(Evas_Object *o, Evas_Coord x, Evas_Coord y)
{
   E_Spectrum *sp;

   sp = evas_object_smart_data_get(o);
   if (!sp) return;

   evas_object_move(sp->o_spectrum, x, y);
   evas_object_move(sp->o_event, x, y);
   evas_object_move(sp->o_cursor, x, y);
}

static void
_e_spectrum_smart_resize(Evas_Object *o, Evas_Coord w, Evas_Coord h)
{
   E_Spectrum *sp;

   sp = evas_object_smart_data_get(o);
   if (!sp) return;

   evas_object_image_fill_set(sp->o_spectrum, 0, 0, w, h);

   evas_object_resize(sp->o_spectrum, w, h);
   evas_object_resize(sp->o_event, w, h);
   evas_object_resize(sp->o_cursor, w, h);
}

static void
_e_spectrum_smart_color_set(Evas_Object *o, int r, int g, int b, int a)
{
   E_Spectrum *sp;

   sp = evas_object_smart_data_get(o);
   if (!sp) return;
   evas_object_color_set(sp->o_spectrum, r, g, b, a);
   evas_object_color_set(sp->o_event, r, g, b, a);
   evas_object_color_set(sp->o_cursor, r, g, b, a);
}

static void
_e_spectrum_smart_clip_set(Evas_Object *o, Evas_Object *o_clip)
{
   E_Spectrum *sp;

   sp = evas_object_smart_data_get(o);
   if (!sp) return;
   evas_object_clip_set(sp->o_spectrum, o_clip);
   evas_object_clip_set(sp->o_event, o_clip);
   evas_object_clip_set(sp->o_cursor, o_clip);
}

static void
_e_spectrum_smart_clip_unset(Evas_Object *o)
{
   E_Spectrum *sp;

   sp = evas_object_smart_data_get(o);
   if (!sp) return;
   evas_object_clip_unset(sp->o_spectrum);
   evas_object_clip_unset(sp->o_event);
   evas_object_clip_unset(sp->o_cursor);
}

static void
_e_spectrum_smart_init(void)
{
   if (_e_spectrum_smart) return;
   {
      static const Evas_Smart_Class sc =
      {
         "e_spectrum",
         EVAS_SMART_CLASS_VERSION,
         _e_spectrum_smart_add,
         _e_spectrum_smart_del,
         _e_spectrum_smart_move,
         _e_spectrum_smart_resize,
         _e_spectrum_smart_show,
         _e_spectrum_smart_hide,
         _e_spectrum_smart_color_set,
         _e_spectrum_smart_clip_set,
         _e_spectrum_smart_clip_unset,
         NULL,
         NULL,
         NULL,
         NULL,
         NULL,
         NULL,
         NULL
      };
      _e_spectrum_smart = evas_smart_class_new(&sc);
   }
}

void
_e_spectrum_color_calc(E_Spectrum *sp, float vx, float vy, float vz, int *r, int *g, int *b)
{
   switch (sp->mode)
     {
      case E_COLOR_COMPONENT_R:
        *r = 255 * vz;
        *g = 255 * vy;
        *b = 255 * vx;
        break;

      case E_COLOR_COMPONENT_G:
        *r = 255 * vx;
        *g = 255 * vz;
        *b = 255 * vy;
        break;

      case E_COLOR_COMPONENT_B:
        *r = 255 * vy;
        *g = 255 * vx;
        *b = 255 * vz;
        break;

      case E_COLOR_COMPONENT_H:
        evas_color_hsv_to_rgb(vz * 360.0, vy, vx, r, g, b);
        break;

      case E_COLOR_COMPONENT_S:
        evas_color_hsv_to_rgb(vx * 360.0, vz, vy, r, g, b);
        break;

      case E_COLOR_COMPONENT_V:
        evas_color_hsv_to_rgb(vy * 360.0, vx, vz, r, g, b);
        break;

      default:
        break;
     }
}

void
_e_spectrum_2d_color_at(E_Spectrum *sp, int x, int y, int *r, int *g, int *b)
{
   int rr, gg, bb;
   float h, s, v;

   if (!sp || !sp->cv) return;

   switch (sp->mode)
     {
      case E_COLOR_COMPONENT_R:
        rr = sp->cv->r;
        gg = (1 - (y / (double)(sp->ih))) * 255;
        bb = (x / (double)(sp->iw)) * 255;
        break;

      case E_COLOR_COMPONENT_G:
        rr = (x / (double)(sp->iw)) * 255;
        gg = sp->cv->g;
        bb = (1 - (y / (double)(sp->ih))) * 255;
        break;

      case E_COLOR_COMPONENT_B:
        rr = (1 - (y / (double)(sp->ih))) * 255;
        gg = (x / (double)(sp->iw)) * 255;
        bb = sp->cv->b;
        break;

      case E_COLOR_COMPONENT_H:
        h = sp->cv->h;
        s = 1 - (y / (double)(sp->ih));
        v = x / (double)(sp->iw);
        evas_color_hsv_to_rgb(h, s, v, &rr, &gg, &bb);
        break;

      case E_COLOR_COMPONENT_S:
        s = sp->cv->s;
        v = 1 - (y / (double)(sp->ih));
        h = x / (double)(sp->iw) * 360;
        evas_color_hsv_to_rgb(h, s, v, &rr, &gg, &bb);
        break;

      case E_COLOR_COMPONENT_V:
        v = sp->cv->v;
        h = (1 - (y / (double)(sp->ih))) * 360;
        s = x / (double)(sp->iw);
        evas_color_hsv_to_rgb(h, s, v, &rr, &gg, &bb);
        break;

      case E_COLOR_COMPONENT_MAX:
      default:
        return;
     }

   if (r) *r = rr;
   if (g) *g = gg;
   if (b) *b = bb;
}

static Eina_Bool
_e_spectrum_redraw(void *d)
{
   E_Spectrum *sp = d;
   int *data;
   int i, j;
   int r, g, b;
   float vx, vy, vz = 0;

   data = evas_object_image_data_get(sp->o_spectrum, 1);
   if (!data)
     {
        sp->draw_timer = NULL;
        return ECORE_CALLBACK_CANCEL;
     }

   switch (sp->mode)
     {
      case E_COLOR_COMPONENT_R:
        vz = (float)sp->cv->r / 255;
        break;

      case E_COLOR_COMPONENT_G:
        vz = (float)sp->cv->g / 255;
        break;

      case E_COLOR_COMPONENT_B:
        vz = (float)sp->cv->b / 255;
        break;

      case E_COLOR_COMPONENT_H:
        vz = sp->cv->h / 360;
        break;

      case E_COLOR_COMPONENT_S:
        vz = sp->cv->s;
        break;

      case E_COLOR_COMPONENT_V:
        vz = sp->cv->v;
        break;

      case E_COLOR_COMPONENT_MAX:
        break;
     }

   for (i = 0; i < sp->ih; i++)
     {
        vy = (float)i / sp->ih;
        for (j = 0; j < sp->iw; j++)
          {
             vx = (float)j / sp->iw;
             _e_spectrum_color_calc(sp, vx, vy, vz, &r, &g, &b);
             data[(i * sp->iw) + j] =
               (sp->cv->a << 24) |
               (((r * sp->cv->a) / 255) << 16) |
               (((g * sp->cv->a) / 255) << 8) |
               (((b * sp->cv->a) / 255));
          }
     }

   evas_object_image_data_set(sp->o_spectrum, data);
   evas_object_image_data_update_add(sp->o_spectrum, 0, 0, sp->iw, sp->ih);
   sp->draw_timer = NULL;
   return ECORE_CALLBACK_CANCEL;
}

static void
_e_spectrum_update(E_Spectrum *sp)
{
   if (!sp || !sp->cv) return;

   if (sp->draw_timer) ecore_timer_del(sp->draw_timer);
   sp->draw_timer = ecore_timer_loop_add(.001, _e_spectrum_redraw, sp);
}

Evas_Object *
e_spectrum_add(Evas *e)
{
   _e_spectrum_smart_init();
   return evas_object_smart_add(e, _e_spectrum_smart);
}

void
e_spectrum_mode_set(Evas_Object *o, E_Color_Component mode)
{
   E_Spectrum *sp;

   if (evas_object_smart_smart_get(o) != _e_spectrum_smart) SMARTERRNR();
   sp = evas_object_smart_data_get(o);
   if (!sp) return;

   if (sp->mode == mode) return;
   sp->mode = mode;
   _e_spectrum_update(sp);
}

E_Color_Component
e_spectrum_mode_get(Evas_Object *o)
{
   E_Spectrum *sp;

   if (evas_object_smart_smart_get(o) != _e_spectrum_smart) SMARTERR(0);
   sp = evas_object_smart_data_get(o);
   if (!sp) return -1;

   return sp->mode;
}

void
e_spectrum_color_value_set(Evas_Object *o, E_Color *cv)
{
   E_Spectrum *sp;

   if (evas_object_smart_smart_get(o) != _e_spectrum_smart) SMARTERRNR();
   sp = evas_object_smart_data_get(o);
   if (!sp) return;

   sp->cv = cv;
   _e_spectrum_update(sp);
}

void
e_spectrum_update(Evas_Object *o)
{
   E_Spectrum *sp;

   if (evas_object_smart_smart_get(o) != _e_spectrum_smart) SMARTERRNR();
   sp = evas_object_smart_data_get(o);
   if (!sp) return;

   _e_spectrum_update(sp);
}