home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / disks / disk433.lzh / Gwin / exsrc.lzh / colormap2.c < prev    next >
C/C++ Source or Header  |  1991-01-12  |  6KB  |  256 lines

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