home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d571 / gwin.lha / Gwin / Examples / colormap2.c < prev    next >
C/C++ Source or Header  |  1991-12-22  |  6KB  |  238 lines

  1. #include "gwin.user.h"
  2. FILE *fopen(),*fp;
  3.  
  4. float black = 0.0;
  5. float red = 1.0;
  6. float blue = 3.0;
  7. float yellow = 5.0;
  8. float white = 7.0;
  9. int NOT_INITIALIZED = -10000;
  10.  
  11. float x11 = 10.0;
  12. float x12 = 266.0;
  13. float y11 = 5.0;
  14. float y12 = 6.0;
  15.  
  16. float x21 = 10.0;
  17. float x22 = 266.0;
  18. float y21 = 3.0;
  19. float y22 = 4.0;
  20.  
  21. float x31 = 10.0;
  22. float x32 = 266.0;
  23. float y31 = 1.0;
  24. float y32 = 2.0;
  25.  
  26. float x41 = 300.0;
  27. float x42 = 390.0;
  28. float y41 = 1.0;
  29. float y42 = 6.0;
  30. int value[17];
  31.  
  32. float xwinmax = 17.0;
  33. float ywinmax = 1.6;
  34. float radius  = .48;
  35. main()
  36. {
  37. int i,j,mrank,nrank;
  38. float x,y;
  39. float fillcolor;
  40. int status;
  41. float ycen;
  42. char filename[256];
  43. int cv,rgb,mask[3];
  44. int colorvalue;
  45. float redval,greenval,blueval;
  46. int rval,gval,bval;
  47.  
  48.    USTART("high2",0.,640.,0.,400.);
  49.    udarea(G,0.,100.,80.,100.);
  50.    uwindo(G,0.,xwinmax,0.,ywinmax);
  51.    upset(G,"colo",black);
  52.    uset(G,"fill");
  53.    uoutln(G);
  54.    uset(G,"nofi");
  55.    upset(G,"colo",white);
  56.  
  57.    uprint(G,(float).5,(float)1.4,
  58.           "COLOREDIT - Depress mouse button and drag - Author:  Howard C. Anderson  ");
  59.  
  60.    ycen = (float)ywinmax/2.0;
  61.    uprint(G,(float).1,(float)(ycen-1.0*radius),"R");
  62.    uprint(G,(float).1,(float)(ycen-1.0*radius -.11*ywinmax),"G");
  63.    uprint(G,(float).1,(float)(ycen-1.0*radius -.11*ywinmax*2.0),"B");
  64.  
  65.    for(i=0;i<16;i++){
  66.       fillcolor = (float)i;
  67.       x=(float)i+1.0;
  68.       y=ywinmax/2.0;
  69.       fill_triangle(x,y,fillcolor);
  70.       ugetrgb(G,(float)i,&redval,&greenval,&blueval);
  71.  
  72.       rval = (int)redval;
  73.       gval = (int)greenval;
  74.       bval = (int)blueval;
  75.  
  76.       colorvalue = (rval << 16) + (gval << 8) + bval;
  77.       value[i] = colorvalue;
  78.       printcol(i,(colorvalue<<4),x,y);
  79.    }
  80.  
  81.    while(TRUE){
  82.       if(ugrinc(G,&x,&y,&uuev)){UEND();exit(0);}
  83.       if(uuev.event == (int)MOUSEBUTTONS){
  84.          if(uuev.key == 'a' |
  85.             uuev.key == 'b' |
  86.             uuev.key == 'c'){
  87.             umove(G,x,y);
  88.             change_color(x,y);
  89.             while(TRUE){
  90.                if(ugrinl(G,&x,&y,&uuev)){UEND();exit(0);}
  91.                if(uuev.event == (int)MOUSEBUTTONS)goto checkforquit;
  92.                if(uuev.event == (int)MOUSEMOVE){
  93.                   umove(G,x,y);
  94.                   change_color(x,y);
  95.                }
  96.             }
  97.          }
  98.       }
  99.       checkforquit:
  100.       if(uuev.key == 'q') {
  101.          UEND();
  102.  
  103.          /* output a colormap file */
  104.  
  105. create_file:
  106.          printf("\n\nPlease enter a file name for the colormap data:  ");
  107.          scanf("%s",filename);
  108.  
  109.          if((fp = fopen(filename,"w")) == 0){
  110.             printf("ERROR - Can't open file:  %s",filename);
  111.             goto create_file;
  112.          }
  113.  
  114.          mask[0] = 0x00ff0000;
  115.          mask[1] = 0x0000ff00;
  116.          mask[2] = 0x000000ff;
  117.  
  118.          for(i=1;i<17;i++){
  119.             fprintf(fp,"%2d   ",i-1);
  120.             for(rgb=0;rgb<3;rgb++){
  121.                cv=(value[i-1] & mask[rgb]) >> (8*(2-rgb));
  122.                fprintf(fp,"%2d   ",cv);
  123.             }
  124.             fprintf(fp,"\n");
  125.          }
  126.          fclose(fp);
  127.          exit(0);
  128.       }
  129.    }
  130. }
  131.  
  132. /*******************************************************/
  133. /* fill_triangle                                       */
  134. /*******************************************************/
  135. fill_triangle(x,y,fillcolor)
  136. float x,y,fillcolor;
  137. {
  138. float fillcolor2;
  139.  
  140.    upset(G,"colo",(float)fillcolor);
  141.    uset(G,"fill");
  142.    uplygn(G,x,y,3.0,radius);
  143.    fillcolor2 = fillcolor+1.0;
  144.    if(fillcolor2>15.0)fillcolor2=0.0;
  145.    upset(G,"colo",(float)fillcolor2);
  146.    ucrcle(G,x,y,.05*(radius));
  147.    uset(G,"nofi");
  148. }
  149.  
  150. /*******************************************************/
  151. /* change_color                                        */
  152. /*******************************************************/
  153. change_color(x,y)
  154. float x,y;
  155. {
  156. int i,ix,isave,cv,colval,rgbshift;
  157. float xcen,ycen,xt[4],yt[4],dxmin,dxtest;
  158. int status;
  159. int rgbmask[4];
  160.  
  161.    rgbmask[1] = 0xff00ffff;
  162.    rgbmask[2] = 0xffff00ff;
  163.    rgbmask[3] = 0xffffff00;
  164.  
  165.    ix = (int)(x+.5);
  166.  
  167.    xcen = (float)ix;
  168.    ycen = (float)ywinmax/2.0;
  169.  
  170.    if(sqrt( (xcen-x)*(xcen-x) + (ycen-y)*(ycen-y) ) <= 1.1*radius
  171.       && ix > 0 && ix < 17 ){
  172.  
  173.       xt[1] = xcen - .8 * radius;
  174.       yt[1] = ycen - .6 * radius;
  175.  
  176.       xt[2] = xcen + .8 * radius;
  177.       yt[2] = ycen - .6 * radius;
  178.  
  179.       xt[3] = xcen;
  180.       yt[3] = ycen + radius;
  181.  
  182.       /* find nearest point and its distance */
  183.  
  184.       dxmin = 1.0e10;
  185.       for(i=1;i<4;i++){
  186.          dxtest = sqrt((x-xt[i])*(x-xt[i]) +
  187.                        (y-yt[i])*(y-yt[i]));
  188.          if(dxtest<dxmin){
  189.             dxmin = dxtest;
  190.             isave = i;
  191.          }
  192.       }
  193.  
  194.       /*  compute colorvalue */
  195.       rgbshift = 3-isave;
  196.       cv = (int)( 16*( (radius-dxmin)/radius ) );
  197.       colval = cv << (rgbshift*8);
  198.  
  199.       /*  insert in existing color value slot */
  200.  
  201.       value[ix-1] = (value[ix-1] & rgbmask[isave]) + colval;
  202.  
  203.       printcol(ix-1,(value[ix-1]<<4),xcen,ycen);
  204.  
  205.    }
  206. }
  207.  
  208. /*******************************************************/
  209. /* printcol                                            */
  210. /*******************************************************/
  211. printcol(ix,color_value,xcen,ycen)
  212. int color_value,ix;
  213. float xcen,ycen;
  214. {
  215. char chstr[100];
  216. int cv,rgb;
  217. int mask[3];
  218. float colval[3];
  219.  
  220.    mask[0] = 0x00ff0000;
  221.    mask[1] = 0x0000ff00;
  222.    mask[2] = 0x000000ff;
  223.  
  224.    for(rgb=0;rgb<3;rgb++){
  225.       cv = (color_value & mask[rgb]) >> (8*(2-rgb));
  226.       colval[rgb] = (float)(cv >> 4);
  227.  
  228.       sprintf(chstr,"%2d",(cv >> 4));
  229.       upset(G,"colo",white);
  230.       uprint(G,(float)(xcen-.35*radius),
  231.                (float)(ycen-1.0*radius -.11*ywinmax*rgb),
  232.                chstr);
  233.    }
  234.    if(color_value >= 0) usetrgb(G,(float)ix,colval[0],colval[1],colval[2]);
  235. }
  236.  
  237.  
  238.