File: CPICKER.C | Size: 8,039 bytes | Download file | Back to directory listing | BWPOW's homepage
/* Color Picker module */
 
#include "allegro.h"
#include "tdgui.h"
 
int retcode;
char sr[3],sg[3],sb[3];
char sh[3],ss[3],sv[3];
BITMAP *truecolorPalette;
static DIALOG color_picker[];
 
int my_button_proc(int msg, DIALOG *d, int c)
{
   if (msg == MSG_CLICK)
   {
     retcode = d->d1;
   }
 
   return d_billbutton_proc(msg, d, c);
}
 
void compute_rgb()
{
  int r,g,b;
  float h,s,v;
  int hi=atoi(sh),si=atoi(ss),vi=atoi(sv);
  h = ((hi) * 360.0 / 255.0);
  s = ((si) / 255.0);
  v = ((vi) / 255.0);
 
  hsv_to_rgb(h,s,v,&r,&g,&b);
  itoa(r,sr,10);
  itoa(g,sg,10);
  itoa(b,sb,10);
}
 
void compute_hsv()
{
  int hi,si,vi;
  float h,s,v;
  int r=atoi(sr),g=atoi(sg),b=atoi(sb);
 
  rgb_to_hsv(r,g,b,&h,&s,&v);
  hi = (int) ((h * 255.0 / 360.0));
  si = (int) ((s * 255.0));
  vi = (int) ((v * 255.0));
 
  itoa(hi,sh,10);
  itoa(si,ss,10);
  itoa(vi,sv,10);
}
 
/* gui object procedure for the color selection sliders */
int my_edit_proc(int msg, DIALOG *d, int c)
{
   int ret;
   ret = d_billedit_proc(msg, d, c);
   switch (msg) {
      case MSG_CHAR:
		 if (atoi(sr) > 255)
		   strcpy(sr,"255");
		 if (atoi(sg) > 255)
		   strcpy(sg,"255");
		 if (atoi(sb) > 255)
		   strcpy(sb,"255");
		 if (atoi(sh) > 255)
		   strcpy(sh,"255");
		 if (atoi(ss) > 255)
		   strcpy(ss,"255");
		 if (atoi(sv) > 255)
		   strcpy(sv,"255");
		 if (d->key == 0)
		 {
		   compute_hsv();
		   SEND_MESSAGE(&color_picker[13],MSG_DRAW,c);
		   SEND_MESSAGE(&color_picker[14],MSG_DRAW,c);
		   SEND_MESSAGE(&color_picker[15],MSG_DRAW,c);
		 }
		 else
		 {
		   compute_rgb();
		   SEND_MESSAGE(&color_picker[10],MSG_DRAW,c);
		   SEND_MESSAGE(&color_picker[11],MSG_DRAW,c);
		   SEND_MESSAGE(&color_picker[12],MSG_DRAW,c);
		 }
	     SEND_MESSAGE(&color_picker[3],MSG_IDLE,c);
	     SEND_MESSAGE(&color_picker[16],MSG_DRAW,c);
         break;
   }
 
   return ret;
}
 
/* Hue is 0..255 */
void makeSV(int hue)
{
  int x2,y2,r,g,b;
  float h,s,v;
  for (y2=0;y2<=255;y2++)
    for (x2=0;x2<=255;x2++)
 	{
 	  h = (hue * 360.0 / 255.0);
	  s = x2 / 255.0;
	  v = y2 / 255.0;
      hsv_to_rgb(h, s, v, &r, &g, &b);
	  putpixel(truecolorPalette,x2,y2,makecol(r,g,b));
	}
}
 
void makeH()
{
  int y2,r,g,b;
  float h,s,v;
  for (y2=0;y2<=255;y2++)
  {
    h = (y2 * 360.0 / 255.0);
    s = 1;
    v = 1;
    hsv_to_rgb(h, s, v, &r, &g, &b);
    hline(truecolorPalette,260,y2,270,makecol(r,g,b));
  }
}
 
int my_color_proc(int msg, DIALOG* d, int c)
{
  RGB rgb;
  switch (msg)
  {
  case MSG_DRAW:
	rgb.r = atoi(sr);
	rgb.g = atoi(sg);
	rgb.b = atoi(sb);
	scare_mouse();
	rectfill(screen,d->x,d->y,d->x+d->w,d->y+d->h,makecol(rgb.r,rgb.g,rgb.b));
	rect(screen,d->x,d->y,d->x+d->w,d->y+d->h,makecol(255,255,255));
	unscare_mouse();
	break;
  }
  return D_O_K;
}
 
int d_truecolorPalProc(int msg, DIALOG* d, int c)
{
	int x1,x2,y1,y2;
	int h,s,v;
	int mx,my;
	static BITMAP *backbuffer;
 
	switch (msg)
	{
	case MSG_START:
		d->w=272;
		d->h=255;
		backbuffer=create_bitmap(d->w,d->h);
		truecolorPalette = create_bitmap(d->w,d->h);
		clear_to_color(truecolorPalette,makecol(192,192,192));
		clear(backbuffer);
		d->d1=0;
		d->d2=0;
		makeH();
		makeSV(d->d1);
		SEND_MESSAGE(d,MSG_IDLE,0);
		break;
	case MSG_DRAW:
		blit(truecolorPalette, backbuffer, 0, 0, 0, 0, d->w, d->h);
		rect(backbuffer,259,d->d1-1,271,d->d1+1,makecol(255,255,255));
		x1 = d->d2%255-3;
		y1 = d->d2/255-3;
		x2 = x1+6;
		y2 = y1+6;
		rect(backbuffer,x1,y1,x2,y2,makecol(255,255,255));
		scare_mouse();
		blit(backbuffer,screen,0,0,d->x,d->y,d->w,d->h);
		unscare_mouse();
		break;
	case MSG_END:
	    destroy_bitmap(backbuffer);
	    destroy_bitmap(truecolorPalette);
		break;
    case MSG_IDLE:
		h=atoi(sh);
		s=atoi(ss);
		v=atoi(sv);
		x1 = v*255+s;
		if (h!=d->d1||x1!=d->d2)
		{
		  if (h!=d->d1)
			makeSV(h);
		  d->d1 = h;
		  d->d2 = x1;
		  SEND_MESSAGE(d,MSG_DRAW,c);
		}
    case MSG_CLICK:
		if (gui_mouse_b() & 1)
		{
		mx = gui_mouse_x();
		my = gui_mouse_y();
		x1 = d->x+260;
		x2 = x1+10;
		y1 = d->y;
		y2 = y1+255;
 
	    /* menime hue */
		if (mx >= x1 && mx <= x2 && my >= y1 &&
			my <= y2)
		{
		  d->d1 = my-y1;
  		  makeSV(d->d1);
		  scare_mouse();
		  SEND_MESSAGE(d,MSG_DRAW,0);
		  unscare_mouse();
		  itoa(d->d1,sh,10);
		  compute_rgb();
		  SEND_MESSAGE(&color_picker[10],MSG_DRAW,c);
		  SEND_MESSAGE(&color_picker[11],MSG_DRAW,c);
	      SEND_MESSAGE(&color_picker[12],MSG_DRAW,c);
		  SEND_MESSAGE(&color_picker[13],MSG_DRAW,c);
		  SEND_MESSAGE(&color_picker[14],MSG_DRAW,c);
		  SEND_MESSAGE(&color_picker[15],MSG_DRAW,c);
  	      SEND_MESSAGE(&color_picker[16],MSG_DRAW,c);
		}
	    /* menime saturation a value */
		if (mx >= d->x && mx <= d->x+255 && my >= d->y &&
			my <= d->y+255)
		{
		  d->d2 = ((my-d->y)*255)+mx-d->x;
		  scare_mouse();
		  SEND_MESSAGE(d,MSG_DRAW,0);
		  unscare_mouse();
		  itoa((mx-d->x),ss,10);
		  itoa((my-d->y),sv,10);
		  compute_rgb();
		  SEND_MESSAGE(&color_picker[10],MSG_DRAW,c);
		  SEND_MESSAGE(&color_picker[11],MSG_DRAW,c);
	      SEND_MESSAGE(&color_picker[12],MSG_DRAW,c);
		  SEND_MESSAGE(&color_picker[13],MSG_DRAW,c);
		  SEND_MESSAGE(&color_picker[14],MSG_DRAW,c);
		  SEND_MESSAGE(&color_picker[15],MSG_DRAW,c);
  	      SEND_MESSAGE(&color_picker[16],MSG_DRAW,c);
		}
        }
	case MSG_GOTMOUSE:
//		d->dp = currentPointer;
//		circleCross.set();
		break;
	case MSG_LOSTMOUSE:
		break;
//		((MousePointer*)d->dp)->set();
//		set_mouse_sprite_focus(0, 0);
	}
	return D_O_K;
}
 
static DIALOG color_picker[] =
{
   { d_billwin_proc,     0,   15,   302,  360,   0,    0,    0,       0,          0,             0,       "Color picker" },
   { my_button_proc,     220, 315,   60,  20,    0,    0,    0,       D_EXIT,     1,             0,       "OK" },
   { my_button_proc,     220, 345,   60,  20,    0,    0,    0,       D_EXIT,     0,             0,       "Cancel" },
   { d_truecolorPalProc, 15,  50, 	 0,    0, 	 0,	   0,	 0,	      0,	      0,	         0,	      NULL},
 
   { d_billtext_proc,   10,   323,  0,    0,    0,    0,    0,    0,       0,    0,    "R:"  },
   { d_billtext_proc,   10,   338,  0,    0,    0,    0,    0,    0,       0,    0,    "G:"  },
   { d_billtext_proc,   10,   353,  0,    0,    0,    0,    0,    0,       0,    0,    "B:"  },
   { d_billtext_proc,   100,  323,  0,    0,    0,    0,    0,    0,       0,    0,    "H:"  },
   { d_billtext_proc,   100,  338,  0,    0,    0,    0,    0,    0,       0,    0,    "S:"  },
   { d_billtext_proc,   100,  353,  0,    0,    0,    0,    0,    0,       0,    0,    "V:"  },
 
   { my_edit_proc,      30,   320,  40,   13,   0,    0,    0,    0,       3,    0,    sr },
   { my_edit_proc,      30,   335,  40,   13,   0,    0,    0,    0,       3,    0,    sg },
   { my_edit_proc,      30,   350,  40,   13,   0,    0,    0,    0,       3,    0,    sb },
   { my_edit_proc,      120,  320,  40,   13,   0,    0,    1,    0,       3,    0,    sh },
   { my_edit_proc,      120,  335,  40,   13,   0,    0,    1,    0,       3,    0,    ss },
   { my_edit_proc,      120,  350,  40,   13,   0,    0,    1,    0,       3,    0,    sv },
   { my_color_proc,     170,  330,  20,   20, 	0,	  0,    0,    0,       0,    0,	   NULL},
   { d_billtext_proc,	5,    180,   0,    0,   0,    0,    0,    0,       0,    0,	   "V"},
   { d_billtext_proc,	290,  180,   0,    0,   0,    0,    0,    0,       0,    0,	   "H"},
   { d_billtext_proc,	130,  35,    0,    0,   0,    0,    0,    0,       0,    0,	   "S"},
   { NULL }
};
 
/* returns 1 if color changed 0 elsewhere */
int color_get(unsigned char *r, unsigned char *g, unsigned char *b)
{
  itoa(*r,sr,10);
  itoa(*g,sg,10);
  itoa(*b,sb,10);
  compute_hsv();
  centre_dialog(color_picker);
  do_dialog(color_picker,-1);
  *r = atoi(sr);
  *g = atoi(sg);
  *b = atoi(sb);
  return retcode;
}