summaryrefslogtreecommitdiff
path: root/cd/test/cdtest/colorbar.c
blob: c83f539815cdf8e4ca49d849b9ebe836ea811cd2 (plain)
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/*=========================================================================*/
/* COLORBAR.C - 03/03/96                                                   */
/* Color Bar implementation.                                               */
/*=========================================================================*/

/*- Constantes: -----------------------------------------------------------*/
#define NUMCOLORS 16

/*- Bibliotecas padrao usadas ---------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

/*- Inclusao das bibliotecas IUP e CD: ------------------------------------*/
#include <iup.h>
#include <cd.h>
#include <cdiup.h>
#include <iupkey.h>

/*- Declaraccoes e Prototypes: --------------------------------------------*/
#include "cdtest.h"

#undef isdigit
#include <ctype.h>


/*- Globais: --------------------------------------------------------------*/
static struct {
  cdCanvas *bar_canvas;             /* canvas da colorbar */
  cdCanvas *other_canvas;           /* canvas ativo anteriormente */
  int w, h;                         /* dimensoes do canvas */
  int bgr, bgg, bgb;                /* cor de fundo do dialogo pai */
  int bci, fci;                     /* indice das cores correntes */
  long *p_foreground, *p_background;              /* variaveis do  usuario */
  long colors[NUMCOLORS];       /* palheta interna */
  int bounds[2*(NUMCOLORS+1)];      /* fronteiras dos elementos da palheta */ 
} colorbar;                         /* contexto da colorbar */

/*- Dialogo para mudancca da cor da palheta: ------------------------------*/
static struct {
  Ihandle *dialog;
  Ihandle *red;
  Ihandle *green;
  Ihandle *blue;
  Ihandle *alpha;
  Ihandle *bt_ok;
  Ihandle *bt_cancel;
  int to_change;
} color_change;

/*- Macros: ---------------------------------------------------------------*/
#define ignore(_) (void)(_)

/*-------------------------------------------------------------------------*/
/* Filtra inteiros.                                                        */
/*-------------------------------------------------------------------------*/
static int integer(Ihandle *self, int c)
{
  ignore(self);

  if (isdigit(c)) {
    return IUP_DEFAULT;
  }
  else if ((c==K_TAB) || (c==K_CR) || (c==K_LEFT) ||
           (c==K_RIGHT) || (c==K_DEL) || (c==K_BS) || (c==K_sTAB)) {
    return IUP_DEFAULT;
  }
  else {
    return IUP_IGNORE;
  }
}

/*-------------------------------------------------------------------------*/
/* Retorna a cor de indice i.                                              */
/*-------------------------------------------------------------------------*/
static long getcolor(int i)
{
  if (i<=NUMCOLORS) {
    return colorbar.colors[i-1];
  }
  else return 0;
}

/*-------------------------------------------------------------------------*/
/* Mostra a caixa de dialogo Color Change.                                 */
/*-------------------------------------------------------------------------*/
static void color_change_show(int c)
{
  unsigned char r, g, b, a;
  long color;
  char s[4];

  /* mostra o dialogo Color Change */
  IupShow(color_change.dialog);

  /* mostra a cor atual */
  color = getcolor(c);
  cdDecodeColor(color, &r, &g, &b);
  sprintf(s, "%d", r);
  IupSetAttribute(color_change.red, IUP_VALUE, s);
  sprintf(s, "%d", g);
  IupSetAttribute(color_change.green, IUP_VALUE, s);
  sprintf(s, "%d", b);
  IupSetAttribute(color_change.blue, IUP_VALUE, s);

  a = cdDecodeAlpha(color);
  sprintf(s, "%d", a);
  IupSetAttribute(color_change.alpha, IUP_VALUE, s);

  /* salva cor a ser alterada no contexto */
  color_change.to_change = c;
}


/*-------------------------------------------------------------------------*/
/* Cancela a operaccao de mudancca de cor.                                 */
/*-------------------------------------------------------------------------*/
static int color_change_cancel(void)
{
  IupHide(color_change.dialog);

  return IUP_DEFAULT;
}

/*-------------------------------------------------------------------------*/
/* Determina as fronteiras dos elementos da palheta.                       */
/*-------------------------------------------------------------------------*/
static void bounds(int dx, int p, int *x)
{
  int i;                                /* contador dos intervalos */
  int j;                                /* indice do vetor x */
  int k;                                /* inicio do intervalo */
  int e;                                /* erro da aproximacao inteira */
  int _2p;                              /* dobro do numero de intervalos */
  int q;                                /* quociente da divisao inteira */
  int _2r;                              /* dobro do resto da divisao inteira */

  /* inicializa as variaveis */
  k = 0;                                /* inicio do primeiro intervalo */
  j = 0;                                /* indice do vetor x */
  e = 0;                                /* inicializa o erro */
  _2p = p << 1;                         /* dobro do numero de intervalos */
  _2r = (dx % p) << 1;                  /* dobro do resto da divisao inteira */
  q = dx / p;                           /* quociente da divisao inteira */

  /* gera o vetor de intervalos */
  for (i=0; i<p; i++) {                 /* para p intervalos */
    e += _2r;                           /* incrementa o erro */
    x[j++] = k;                         /* inicio do intervalo */
    if (e >= p) {                       /* estourou? */
      e -= _2p;                         /* ajusta o novo limite */
      k += q + 1;                       /* arredonda para cima */
    } 
    else {
      k += q;                           /* arredonda para baixo */
    }
    x[j++] = k - 2;                     /* fim do intervalo */
  }
}

/*-------------------------------------------------------------------------*/
/* Acha a cor onde o mouse foi clicado por busca binaria. Retorna zero se  */ 
/* o click foi fora de qualquer cor.                                       */
/*-------------------------------------------------------------------------*/
static int findwhere(int x, int y, int c1, int c2)
{
  int mid;
  
  /* so pode ser este ou o click foi fora */
  if (c1 == c2) {
    if ((x > colorbar.bounds[c1<<1]) && (x < colorbar.bounds[(c1<<1) + 1])) {
      if ((y > 0) && (y < (colorbar.h-1))) return c1;
      else return 0;
    }
    else return 0;
  }
  
  /* elemento intermediario */
  mid = (c1 + c2)>>1;
  
  /* se o click estah a direita do elemento intermediario */
  if (x > colorbar.bounds[(mid<<1) + 1]) return findwhere(x, y, mid+1, c2);
  
  /* se estah a esquerda do elemento intermediario */
  else if (x < colorbar.bounds[mid<<1]) return findwhere(x, y, c1, mid-1);
  
  /* estah no meio do intermediario e a vertical estah legal, eh esse */
  else if ((y > 0) && (y < (colorbar.h-1))) return mid;

  /* o programa nunca chega aqui, mas o compilador fica feliz */
  return 0;
}

/*-------------------------------------------------------------------------*/
/* Desenha a moldura do elemento da palheta.                               */
/*-------------------------------------------------------------------------*/
static void hollowbox(int xmin, int xmax, int ymin, int ymax)
{
  cdForeground(cdEncodeColor(255, 255, 255));
  cdLine(xmin, ymin, xmax, ymin);
  cdLine(xmax, ymin+1, xmax, ymax);

  cdForeground(cdEncodeColor(102, 102, 102));
  cdLine(xmin, ymin+1, xmin, ymax);
  cdLine(xmin+1, ymax, xmax-1, ymax);
}


/*-------------------------------------------------------------------------*/
/* Muda a cor de indice i e retorna a anterior.                            */
/*-------------------------------------------------------------------------*/
static long changecolor(int i, long color)
{
  long temp;

  if (i<=NUMCOLORS) {
    temp = colorbar.colors[i-1];
    colorbar.colors[i-1] = color;
    return temp;
  }
  else return 0;
}


/*-------------------------------------------------------------------------*/
/* Inicializa o vetor de cores.                                            */
/*-------------------------------------------------------------------------*/
static void resetcolors(void)
{
  colorbar.colors[ 0] = cdEncodeColor(  0,   0,   0);
  colorbar.colors[ 1] = cdEncodeColor(153, 153, 153);
  colorbar.colors[ 2] = cdEncodeColor(178, 178, 178);
  colorbar.colors[ 3] = cdEncodeColor(255, 255, 255);
  colorbar.colors[ 4] = cdEncodeColor(255, 255,   0);
  colorbar.colors[ 5] = cdEncodeColor(255,   0,   0);
  colorbar.colors[ 6] = cdEncodeColor(255,   0, 255);
  colorbar.colors[ 7] = cdEncodeColor(  0, 255, 255);
  colorbar.colors[ 8] = cdEncodeColor(  0,   0, 255);
  colorbar.colors[ 9] = cdEncodeColor(  0, 255,   0);
  colorbar.colors[10] = cdEncodeColor(128, 128,   0);
  colorbar.colors[11] = cdEncodeColor(128,   0,   0);
  colorbar.colors[12] = cdEncodeColor(128,   0, 128);
  colorbar.colors[13] = cdEncodeColor(  0, 128, 128);
  colorbar.colors[14] = cdEncodeColor(  0,   0, 128);
  colorbar.colors[15] = cdEncodeColor(  0, 128,   0);
}

/*-------------------------------------------------------------------------*/
/* Desenha a ColorBar.                                                     */
/*-------------------------------------------------------------------------*/
static int fColorBarRepaint(void)
{
  int i;
  double dt;

  /* salva o canvas ativo no momento */
  colorbar.other_canvas = cdActiveCanvas();

  /* ativa o canvas da colorbar */
  if (cdActivate(colorbar.bar_canvas) == CD_ERROR) {
    printf("Color Bar Error: Unable to activate canvas.");
    return 1;
  }
  cdClear();

  /* desenha as cores da palheta */
  for (i=2; i<=2*NUMCOLORS; i+=2) {
    cdForeground(getcolor(i>>1));
    cdBox(colorbar.bounds[i], colorbar.bounds[i+1], 1, colorbar.h-1);
    hollowbox(colorbar.bounds[i], colorbar.bounds[i+1], 1, colorbar.h-1);
  }

  /* desenha o fedback */
  dt = (colorbar.w-1)/(NUMCOLORS+1);
  /* desenha a cor de fundo */
  cdForeground(getcolor(colorbar.bci));
  cdBox(1, (int)(2.0*dt/3.0), 1, (int)(2.0*(colorbar.h-1)/3.0));
  hollowbox(1, (int)(2.0*dt/3.0), 1, (int)(2.0*(colorbar.h-1)/3.0));
  /* desenha a cor de frente */
  cdForeground(getcolor(colorbar.fci));
  cdBox((int)(dt/3.0), (int)(dt)-1, (int)((colorbar.h-1)/3.0)+1, colorbar.h-1);
  hollowbox((int)(dt/3.0), (int)(dt)-1, (int)((colorbar.h-1)/3.0)+1, colorbar.h-1);
 
  /* restaura o canvas anteriormente ativo */
  cdActivate(colorbar.other_canvas);

  return IUP_DEFAULT;
}

/*-------------------------------------------------------------------------*/
/* Funcao associada ao botao do mouse.                                     */
/*-------------------------------------------------------------------------*/
static int fColorBarButtonCB(Ihandle *self, int b, int e, int x, int y, char *r)
{
  int c;
 
  ignore(self);
  ignore(r);

  /* salva o canvas ativo no momento */
  colorbar.other_canvas = cdActiveCanvas();

  /* ativa o canvas da colorbar */
  if (cdActivate(colorbar.bar_canvas) == CD_ERROR) {
    printf("Color Bar Error: Unable to activate canvas.");
    return 1;
  }

  /* converte para coordenadas do canvas */
  cdUpdateYAxis(&y);

  /* se o botao foi pressionado */
  if (e) {

    /* acha onde foi o click */
    c = findwhere(x, y, 1, 16);
    
    /* se o click foi dentro de alguma cor... */
    if (c != 0) {

      /* botao da esquerda eh mudancca de cor de foreground */
      if (b == IUP_BUTTON1) {

        /* se for double-click */
        if ((isdouble(r)) && (c == colorbar.fci) ) {

          /* mostra o dialogo */
          color_change_show(c);
        }

        /* muda a cor de frente corrente */
        else {

          /* largura de cada celula */
          double dt;
          unsigned char r, g, b;
  
          /* altera a variavel do usuario */
          *(colorbar.p_foreground) = getcolor(c);
       
          /* altera o indice da cor de frente corrente */
          colorbar.fci = c;

          cdDecodeColor(getcolor(colorbar.fci), &r, &g, &b);
          sprintf(ctgc.status_line, "cdForeground(cdEncodeColor(%d, %d, %d))", (int)r, (int)g, (int)b);
          set_status();

          /* altera o feedback no primeiro elemento da palheta */
          dt = (colorbar.w-1)/(NUMCOLORS+1);
          cdForeground(getcolor(colorbar.fci));
          cdBox((int)(dt/3.0), (int)(dt)-1, (int)((colorbar.h-1)/3.0)+1, colorbar.h-1);
          hollowbox((int)(dt/3.0), (int)(dt)-1, (int)((colorbar.h-1)/3.0)+1, colorbar.h-1);
        }

      }

      else if (b == IUP_BUTTON3) {
          
        /* largura de cada celula */
        double dt;
        unsigned char r, g, b;

        /* altera a variavel do usuario */
        *(colorbar.p_background) = getcolor(c);
      
        /* altera o indice da cor de frente corrente */
        colorbar.bci = c;
     
        cdDecodeColor(getcolor(colorbar.bci), &r, &g, &b);
        sprintf(ctgc.status_line, "cdBackground(cdEncodeColor(%d, %d, %d))", (int)r, (int)g, (int)b);
        set_status();

        /* altera o feedback no primeiro elemento da palheta */
        dt = (colorbar.w-1)/(NUMCOLORS+1);
        cdForeground(getcolor(colorbar.bci));
        cdBox(1, (int)(2.0*dt/3.0), 1, (int)(2.0*(colorbar.h-1)/3.0));
        hollowbox(1, (int)(2.0*dt/3.0), 1, (int)(2.0*(colorbar.h-1)/3.0));
        cdForeground(getcolor(colorbar.fci));
        cdBox((int)(dt/3.0), (int)(dt)-1, (int)((colorbar.h-1)/3.0)+1, colorbar.h-1);
        hollowbox((int)(dt/3.0), (int)(dt)-1, (int)((colorbar.h-1)/3.0)+1, colorbar.h-1);
      }

    }

  }

  /* restaura o canvas anteriormente ativo */
  cdActivate(colorbar.other_canvas);
  
  return IUP_DEFAULT;
}

/*-------------------------------------------------------------------------*/
/* Callback associado ao resize do canvas.                                 */
/*-------------------------------------------------------------------------*/
static int fColorBarResizeCB(Ihandle *self, int w, int h)
{
  ignore(self);

  /* atualiza as dimensoes do canvas */
  colorbar.w = w;
  colorbar.h = h;

  /* atualiza as fronteiras dos elementos da palheta */
  bounds(colorbar.w, NUMCOLORS+1, colorbar.bounds); 

  return IUP_DEFAULT;
}

/*-------------------------------------------------------------------------*/
/* Confirma a mudanca de cores.                                            */
/*-------------------------------------------------------------------------*/
static int color_change_ok(void)
{
  int r, g, b, a;
  long new_color;

  /* pega os novos valores */
  r = IupGetInt(color_change.red, IUP_VALUE);
  g = IupGetInt(color_change.green, IUP_VALUE);
  b = IupGetInt(color_change.blue, IUP_VALUE);
  a = IupGetInt(color_change.alpha, IUP_VALUE);

  /* se todos forem validos */
  if ((r<256)&&(g<256)&&(b<256)) {

    /* esconde a caixa de dialogo */
    IupHide(color_change.dialog);

    /* atualiza a cor no contexto */ 
    new_color = cdEncodeColor((unsigned char)r, (unsigned char) g, (unsigned char) b);
    new_color = cdEncodeAlpha(new_color, (unsigned char)a);
    changecolor(color_change.to_change, new_color);

    colorbar.fci = color_change.to_change;

    /* redesenha a colorbar */
    fColorBarRepaint();

    /* altera a variavel do usuario */
    *(colorbar.p_foreground) = new_color;

  }

  return IUP_DEFAULT;
}

/*-------------------------------------------------------------------------*/
/* Inicializa a ColorBar.                                                  */
/*-------------------------------------------------------------------------*/
void ColorBarClose(void)
{
  cdKillCanvas(colorbar.bar_canvas);
  IupDestroy(color_change.dialog);
}

int ColorBarInit(Ihandle *parent, Ihandle *canvas, long *foreground, long *background)
{
  char *bg_color;

  /* pega a cor de fundo do dialogo parente */
  bg_color = IupGetAttribute(parent, "BGCOLOR");
  if (bg_color == NULL) {
    printf("Color Bar Error: Unable to get bg_color.");
    return 0;
  }
  sscanf(bg_color, "%d %d %d", &colorbar.bgr, &colorbar.bgg, &colorbar.bgb);

  /* inicializa as cores de fundo e de frente defaults */
  colorbar.fci = 1;
  colorbar.bci = 4;

  /* pega o enderecco das variaveis do usuario */
  colorbar.p_foreground = foreground;
  colorbar.p_background = background;

  /* inicializa a palheta interna de cores */
  resetcolors();

  /* cria o canvas do CD */
  colorbar.bar_canvas = cdCreateCanvas(CD_IUP, canvas);
  if (colorbar.bar_canvas == NULL) {
    printf("Color Bar Error: Unable to create canvas.");
    return 0;
  }

  /* salva o canvas ativo no momento */
  colorbar.other_canvas = cdActiveCanvas();

  /* ativa o canvas da colorbar */
  if (cdActivate(colorbar.bar_canvas) == CD_ERROR) {
    printf("Color Bar Error: Unable to activate canvas.");
    return 0;
  }

  /* pega as dimensoes do canvas pela primeira vez */
  cdGetCanvasSize(&colorbar.w, &colorbar.h, NULL, NULL);

  /* restaura o canvas anteriormente ativo */
  if (colorbar.other_canvas != NULL) cdActivate(colorbar.other_canvas);

  /* cria o vetor com as fronteiras dos elementos da palheta */
  bounds(colorbar.w, NUMCOLORS+1, colorbar.bounds); 
  
  /* associa os callbacks */
  IupSetFunction("cmdColorBarButtonCB", (Icallback) fColorBarButtonCB);
  IupSetFunction("cmdColorBarRepaint", (Icallback) fColorBarRepaint);
  IupSetFunction("cmdColorBarResizeCB", (Icallback) fColorBarResizeCB);

  /* desenha a barra de cores pela primeira vez */
  fColorBarRepaint();

  /* inicializa o dialogo de troca de cores da palheta */
  color_change.dialog = IupDialog(
    IupVbox(
      IupHbox(
        IupLabel("R:"),
        color_change.red = IupText("cmdInteger"),
        IupLabel("G:"),
        color_change.green = IupText("cmdInteger"),
        IupLabel("B:"),
        color_change.blue = IupText("cmdInteger"),
        IupLabel("A:"),
        color_change.alpha = IupText("cmdInteger"),
        NULL
      ),
      IupHbox(
        IupFill(),
        color_change.bt_ok = IupButton("OK", "cmdColorChangeOK"),
        color_change.bt_cancel = IupButton("Cancel", "cmdColorChangeCancel"),
        IupFill(),
        NULL
      ),
      NULL
    )
  );

  /* atributos do dialogo */
  IupSetAttribute(color_change.dialog, IUP_TITLE, "Color Change:");
  IupSetAttribute(color_change.dialog, IUP_MARGIN, "5x5");
  IupSetAttribute(color_change.dialog, IUP_GAP, "5");
  IupSetAttribute(color_change.dialog, "MAXBOX", "NO");
  IupSetAttribute(color_change.dialog, "MINBOX", "NO");
  IupSetAttribute(color_change.dialog, "PARENTDIALOG", "dlgMain");

  /* atributos dos texts */
  IupSetFunction("cmdInteger", (Icallback) integer);
  IupSetAttribute(color_change.red, "NC", "3");
  IupSetAttribute(color_change.green, "NC", "3");
  IupSetAttribute(color_change.blue, "NC", "3");
  IupSetAttribute(color_change.alpha, "NC", "3");
  IupSetAttribute(color_change.red, "SIZE", "24");
  IupSetAttribute(color_change.green, "SIZE", "24");
  IupSetAttribute(color_change.blue, "SIZE", "24");
  IupSetAttribute(color_change.alpha, "SIZE", "24");
  
  /* atributos dos botoes */
  IupSetAttribute(color_change.bt_ok, IUP_SIZE, "30");
  IupSetAttribute(color_change.bt_cancel, IUP_SIZE, "30");
  IupSetFunction("cmdColorChangeCancel", (Icallback) color_change_cancel);
  IupSetFunction("cmdColorChangeOK", (Icallback) color_change_ok);

  return 1;
}