#include <stdio.h>
#include <exec/types.h>
#include <graphics/gfxbase.h>
#include <graphics/display.h>
#include <graphics/regions.h>
#include <graphics/gfx.h>
#include <graphics/gfxmacros.h>
#include <intuition/intuitionbase.h>
#include <intuition/intuition.h>
#include <stdio.h>
#include <exec/memory.h>
#include <hardware/custom.h>
#include <hardware/dmabits.h>
#include <libraries/dos.h>
#include <libraries/diskfont.h>
#include <devices/audio.h>
#include <math.h>
FILE *fopen(),*fp;

float black = 0.0;
float red = 1.0;
float blue = 3.0;
float yellow = 5.0;
float white = 7.0;
int NOT_INITIALIZED = -10000;

float x11 = 10.0;
float x12 = 266.0;
float y11 = 5.0;
float y12 = 6.0;

float x21 = 10.0;
float x22 = 266.0;
float y21 = 3.0;
float y22 = 4.0;

float x31 = 10.0;
float x32 = 266.0;
float y31 = 1.0;
float y32 = 2.0;

float x41 = 300.0;
float x42 = 390.0;
float y41 = 1.0;
float y42 = 6.0;
int value[17];

float xwinmax = 17.0;
float ywinmax = 1.6;
float radius  = .48;
main()
{
int i,j,mrank,nrank;
float x,y;
float fillcolor;
int ievent;
char keystroke;
int status;
float ycen;
char filename[256];
int cv,rgb,mask[3];
int colorvalue;
float redval,greenval,blueval;
int rval,gval,bval;

   ustart("high2",0.,640.,0.,400.);
   udarea(0.,100.,80.,100.);
   uwindo(0.,xwinmax,0.,ywinmax);
   upset("colo",black);
   uset("fill");
   uoutln();
   uset("nofi");
   upset("colo",white);

   uprint((float).5,(float)1.4,
          "COLOREDIT - Depress mouse button and drag - Author:  Howard C. Anderson  ");

   ycen = (float)ywinmax/2.0;
   uprint((float).1,(float)(ycen-1.0*radius),"R");
   uprint((float).1,(float)(ycen-1.0*radius -.11*ywinmax),"G");
   uprint((float).1,(float)(ycen-1.0*radius -.11*ywinmax*2.0),"B");

   for(i=0;i<16;i++){
      fillcolor = (float)i;
      x=(float)i+1.0;
      y=ywinmax/2.0;
      fill_triangle(x,y,fillcolor);
      ugetrgb((float)i,&redval,&greenval,&blueval);

      rval = (int)redval;
      gval = (int)greenval;
      bval = (int)blueval;

      colorvalue = (rval << 16) + (gval << 8) + bval;
      value[i] = colorvalue;
      printcol(i,(colorvalue<<4),x,y);
   }

   while(TRUE){
      ugrinc(&x,&y,&ievent,&keystroke);
      if(ievent == (int)MOUSEBUTTONS){
         if(keystroke == 'a' |
            keystroke == 'b' |
            keystroke == 'c'){
            umove(x,y);
            change_color(x,y);
            while(TRUE){
               ugrinl(&x,&y,&ievent,&keystroke);
               if(ievent == (int)MOUSEBUTTONS)goto checkforquit;
               if(ievent == (int)MOUSEMOVE){
                  umove(x,y);
                  change_color(x,y);
               }
            }
         }
      }
      checkforquit:
      if(keystroke == 'q') {
         uend();

         /* output a colormap file */

create_file:
         printf("\n\nPlease enter a file name for the colormap data:  ");
         scanf("%s",filename);

         if((fp = fopen(filename,"w")) == 0){
            printf("ERROR - Can't open file:  %s",filename);
            goto create_file;
         }

         mask[0] = 0x00ff0000;
         mask[1] = 0x0000ff00;
         mask[2] = 0x000000ff;

         for(i=1;i<17;i++){
            fprintf(fp,"%2d   ",i-1);
            for(rgb=0;rgb<3;rgb++){
               cv=(value[i-1] & mask[rgb]) >> (8*(2-rgb));
               fprintf(fp,"%2d   ",cv);
            }
            fprintf(fp,"\n");
         }
         fclose(fp);
         exit(0);
      }
   }
}

/*******************************************************/
/* fill_triangle                                       */
/*******************************************************/
fill_triangle(x,y,fillcolor)
float x,y,fillcolor;
{
float fillcolor2;

   upset("colo",(float)fillcolor);
   uset("fill");
   uplygn(x,y,3.0,radius);
   fillcolor2 = fillcolor+1.0;
   if(fillcolor2>15.0)fillcolor2=0.0;
   upset("colo",(float)fillcolor2);
   ucrcle(x,y,.05*(radius));
   uset("nofi");
}

/*******************************************************/
/* change_color                                        */
/*******************************************************/
change_color(x,y)
float x,y;
{
int i,ix,isave,cv,colval,rgbshift;
float xcen,ycen,xt[4],yt[4],dxmin,dxtest;
int status;
int rgbmask[4];

   rgbmask[1] = 0xff00ffff;
   rgbmask[2] = 0xffff00ff;
   rgbmask[3] = 0xffffff00;

   ix = (int)(x+.5);

   xcen = (float)ix;
   ycen = (float)ywinmax/2.0;

   if(sqrt( (xcen-x)*(xcen-x) + (ycen-y)*(ycen-y) ) <= 1.1*radius
      && ix > 0 && ix < 17 ){

      xt[1] = xcen - .8 * radius;
      yt[1] = ycen - .6 * radius;

      xt[2] = xcen + .8 * radius;
      yt[2] = ycen - .6 * radius;

      xt[3] = xcen;
      yt[3] = ycen + radius;

      /* find nearest point and its distance */

      dxmin = 1.0e10;
      for(i=1;i<4;i++){
         dxtest = sqrt((x-xt[i])*(x-xt[i]) +
                       (y-yt[i])*(y-yt[i]));
         if(dxtest<dxmin){
            dxmin = dxtest;
            isave = i;
         }
      }

      /*  compute colorvalue */
      rgbshift = 3-isave;
      cv = (int)( 16*( (radius-dxmin)/radius ) );
      colval = cv << (rgbshift*8);

      /*  insert in existing color value slot */

      value[ix-1] = (value[ix-1] & rgbmask[isave]) + colval;

      printcol(ix-1,(value[ix-1]<<4),xcen,ycen);

   }
}

/*******************************************************/
/* printcol                                            */
/*******************************************************/
printcol(ix,color_value,xcen,ycen)
int color_value,ix;
float xcen,ycen;
{
char chstr[100];
int cv,rgb;
int mask[3];
float colval[3];

   mask[0] = 0x00ff0000;
   mask[1] = 0x0000ff00;
   mask[2] = 0x000000ff;

   for(rgb=0;rgb<3;rgb++){
      cv = (color_value & mask[rgb]) >> (8*(2-rgb));
      colval[rgb] = (float)(cv >> 4);

      sprintf(chstr,"%2d",(cv >> 4));
      upset("colo",white);
      uprint((float)(xcen-.35*radius),
             (float)(ycen-1.0*radius -.11*ywinmax*rgb),
             chstr);
   }
   if(color_value >= 0) usetrgb((float)ix,colval[0],colval[1],colval[2]);
}


