/*
From: anneke@chem.ruu.nl (Anneke Sicherer-Roetman)

Hello all, 
I would like to contribute the color selector dialog below. Enjoy!

Usage example:

double r,g,b; 

if (fl_show_cselector("Color for C-O bonds",&r,&g,&b)) 
   fl_mapcolor(color,r,g,b);

Anneke Sicherer-Roetman --- Laboratory for Crystal and Structural Chemistry
Bijvoet Center - Utrecht University -- HOLLAND -- email: anneke@chem.ruu.nl

*/

/* color selector dialog for xforms library (based upon ldial demo program) */
/* Anneke Sicherer-Roetman, Utrecht University, Holland, anneke@chem.ruu.nl */

#include "forms.h"

int fl_show_cselector(const char *title,double *red,double *green,double *blue)
{
   FL_FORM *form;                  /* color selector dialog window */
   FL_OBJECT *ok,*cancel;          /* dialog buttons */
   FL_OBJECT *redD,*greenD,*blueD; /* color dials */
   FL_OBJECT *redL,*greenL,*blueL; /* color labels */
   FL_OBJECT *mix;                 /* resulting color */
   FL_OBJECT *result;              /* function result */
   char str[100];                  /* work string */

   /* open form */
   form = fl_bgn_form(FL_UP_BOX,300,340);

   /* create red dial and label */
   redD = fl_add_dial(FL_LINE_DIAL,30,85,60,60,"Red");
   fl_set_dial_bounds(redD,0.0,255.0);
   fl_set_dial_value(redD,*red);
   fl_set_object_color(redD,FL_RED,FL_DIAL_COL2);
   redL = fl_add_box(FL_DOWN_BOX,105,103,50,24,"");

   /* create green dial and label */
   greenD = fl_add_dial(FL_LINE_DIAL,30,169,60,60,"Green");
   fl_set_dial_bounds(greenD,0.0,255.0);
   fl_set_dial_value(greenD,*green);
   fl_set_object_color(greenD,FL_GREEN,FL_DIAL_COL2);
   greenL = fl_add_box(FL_DOWN_BOX,105,187,50,24,"");

   /* create blue dial and label */
   blueD = fl_add_dial(FL_LINE_DIAL,30,253,60,60,"Blue");
   fl_set_dial_bounds(blueD,0.0,255.0);
   fl_set_dial_value(blueD,*blue);
   fl_set_object_color(blueD,FL_BLUE,FL_DIAL_COL2);
   blueL = fl_add_box(FL_DOWN_BOX,105,271,50,24,"");

   /* create box to show resulting color */
   mix = fl_add_box(FL_DOWN_BOX,180,80,90,230,"");
   fl_set_object_color(mix,FL_FREE_COL1,FL_FREE_COL1);

   /* create dialog buttons */
   ok = fl_add_button(FL_NORMAL_BUTTON,45,20,90,30,"OK");
   cancel = fl_add_button(FL_NORMAL_BUTTON,160,20,90,30,"Cancel");

   /* close form */
   fl_end_form();

   /* display form */
   fl_deactivate_all_forms();
   fl_show_form(form,FL_PLACE_MOUSE,FL_TRANSIENT,title);

   /* dialog loop */
   do {
      *red=(int)fl_get_dial_value(redD);
      *green=(int)fl_get_dial_value(greenD);
      *blue=(int)fl_get_dial_value(blueD);
      fl_mapcolor(FL_FREE_COL1,*red,*green,*blue);
      fl_redraw_object(mix);
      sprintf(str,"%.0f",*red); fl_set_object_label(redL,str);
      sprintf(str,"%.0f",*green); fl_set_object_label(greenL,str);
      sprintf(str,"%.0f",*blue); fl_set_object_label(blueL,str);
      result = fl_do_forms();
   } while (result!=ok && result!=cancel);

   /* destroy form */
   fl_hide_form(form);
   fl_free_form(form);
   fl_activate_all_forms();

   /* return 1 if OK was clicked, 0 otherwise */
   return result==ok;
}
