home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / screen / scremo10.lha / ScreenMod / Source / palette.c < prev    next >
C/C++ Source or Header  |  1991-06-06  |  14KB  |  532 lines

  1. /**********************************************************************\
  2.  *             Color Palette Adjuster v2.0 enhanced                   *
  3.  *                                      *
  4.  *   By Syd L. Bolton, with mods from code by Carolyn Schepper, CBM   *
  5.  *          ©1990,91 Legendary Design Technologies, Inc.              *
  6.  *                                      *
  7.  *         Version: 2.0  Date: May 17, 1990  Time: 10:30 pm           *
  8.  *         Version: 2.1  Date: May 2, 1991   Time: 08:12 pm           *
  9.  *                       NOTE: SIMPLY REMOVED SPREAD ROUTINE          *
  10. \**********************************************************************/
  11.  
  12. #define MAXCOLORS 32
  13. #define RGB_BODY  0xFFF
  14.  
  15. #define PWIDE 192         /* Palette Window Width */
  16. #define PBASEHIGH 87      /* Height before adjust for color rows */
  17.  
  18. /* Gadget ID's */
  19. #define colID  1
  20. #define okID   MAXCOLORS + 1
  21. #define resID  MAXCOLORS + 2
  22. #define canID  MAXCOLORS + 3
  23. #define rID    MAXCOLORS + 4
  24. #define bID    MAXCOLORS + 5
  25. #define gID    MAXCOLORS + 6
  26. #define copID  MAXCOLORS + 8
  27. #define swpID  MAXCOLORS + 9
  28.  
  29. /* Proportional RGB Gadgets */
  30.  
  31. #define PRPX  52
  32. #define PRPY -77  /* Top prop, relative to bottom */
  33. #define PRPW  90
  34. #define PRPH  11
  35.  
  36. struct IntuiText
  37.    rTxt = {1,0,JAM1,-11,2,0,"R",0},
  38.    gTxt = {1,0,JAM1,-11,2,0,"G",0},
  39.    bTxt = {1,0,JAM1,-11,2,0,"B",0};
  40.  
  41. struct Image  rImg, gImg, bImg;
  42.  
  43. struct PropInfo
  44.    rInf = {AUTOKNOB|FREEHORIZ,0,0,RGB_BODY,0,0,0,0,0,0,0},
  45.    gInf = {AUTOKNOB|FREEHORIZ,0,0,RGB_BODY,0,0,0,0,0,0,0},
  46.    bInf = {AUTOKNOB|FREEHORIZ,0,0,RGB_BODY,0,0,0,0,0,0,0};
  47.  
  48. struct Gadget
  49.    bGad = { NULL, PRPX,PRPY+30, PRPW,PRPH,
  50.             GADGHNONE|GADGIMAGE|GRELBOTTOM,
  51.             GADGIMMEDIATE|RELVERIFY|FOLLOWMOUSE,
  52.             PROPGADGET,(APTR)&bImg, 0,
  53.             &bTxt, 0,(APTR)&bInf, bID, 0 },   /* Last Gadget */
  54.  
  55.    gGad = { &bGad, PRPX,PRPY+15, PRPW,PRPH,
  56.             GADGHNONE|GADGIMAGE|GRELBOTTOM,
  57.             GADGIMMEDIATE|RELVERIFY|FOLLOWMOUSE,
  58.             PROPGADGET,(APTR)&gImg, 0,
  59.             &gTxt, 0,(APTR)&gInf, gID, 0 },
  60.  
  61.    rGad = { &gGad, PRPX,PRPY, PRPW,PRPH,
  62.             GADGHNONE|GADGIMAGE|GRELBOTTOM,
  63.             GADGIMMEDIATE|RELVERIFY|FOLLOWMOUSE,
  64.             PROPGADGET,(APTR)&rImg, 0,
  65.             &rTxt, 0,(APTR)&rInf, rID, 0 };
  66.  
  67. /* Text Selection Gadgets */
  68. #define TXTX  8
  69. #define TXTY -32      /* Relative to bottom */
  70. #define TXTW  54
  71. #define TXTH  13
  72. #define NEWY -17      /* also relative to bottom */
  73.  
  74. SHORT  txtBorXY[10] = {0,0, TXTW-1,0, TXTW-1,TXTH-1, 0,TXTH-1, 0,0};
  75. struct Border txtBor = { 0,0,3,0,JAM1,5,&txtBorXY[0],NULL };
  76.  
  77. struct IntuiText canTxt = {1,0,JAM1,4,3,0,"CANCEL",0};
  78. struct Gadget canGad = {
  79.    &rGad,   TXTX+122,NEWY,TXTW,TXTH, GADGHCOMP|GRELBOTTOM,
  80.    RELVERIFY, BOOLGADGET, (APTR)&txtBor, 0,
  81.    &canTxt, 0,0, canID, 0 };      /* Linked to red Prop Gadget */
  82.  
  83. struct IntuiText resTxt = {1,0,JAM1,8,3,0,"RESET",0};
  84. struct Gadget resGad = {
  85.    &canGad, TXTX,TXTY,TXTW,TXTH, GADGHCOMP|GRELBOTTOM,
  86.    RELVERIFY, BOOLGADGET, (APTR)&txtBor, 0,
  87.    &resTxt, 0,0, resID, 0 };
  88.  
  89. struct IntuiText swpTxt = {1,0,JAM1,12,3,0,"SWAP",0};
  90. struct Gadget swpGad = {
  91.    &resGad, TXTX+122,TXTY,TXTW,TXTH,GADGHCOMP|GRELBOTTOM,
  92.    RELVERIFY, BOOLGADGET, (APTR)&txtBor,0,
  93.    &swpTxt, 0, 0, swpID, 0};
  94.  
  95. struct IntuiText copTxt = {1,0,JAM1,12,3,0,"COPY",0};
  96. struct Gadget copGad = {
  97.    &swpGad, TXTX+61, TXTY,TXTW,TXTH,GADGHCOMP|GRELBOTTOM,
  98.    RELVERIFY, BOOLGADGET, (APTR)&txtBor,0,
  99.    &copTxt, 0, 0, copID, 0};
  100.  
  101. struct IntuiText okTxt = {1,0,JAM1,19,3,0,"OK",0};
  102. struct Gadget okGad = {
  103.    &copGad, TXTX,NEWY,TXTW,TXTH, GADGHCOMP|GRELBOTTOM,
  104.    RELVERIFY, BOOLGADGET, (APTR)&txtBor, 0,
  105.    &okTxt, 0,0, okID, 0 };  /* Color gads will be linked to this one */
  106.  
  107.  
  108. /* Palette Color Gadgets */
  109.  
  110. #define COLX  10     /* Offsets of first color gadget */
  111. #define COLY  14
  112. #define COLSW  4     /* Spacing between color gadgets */
  113. #define COLSH  4
  114. #define COLW8 18     /* Image width if 8 or more colors */
  115. #define COLW4 (COLW8+COLW8+COLSW)  /* 4 colors */
  116. #define COLW2 (COLW4+COLW4+COLSW)  /* 2 colors */
  117. #define COLH  10     /* Image height */
  118. #define COLDY (COLH+COLSH)
  119. SHORT   colW, colDX;
  120.  
  121. /* Drawn with DrawBorder() when color is selected */
  122. /* Note - the border coords will be filled in by initColGads() */
  123. SHORT  colBorXY[10] = {-2,-2, NULL,-2, NULL,COLH+1, -2,COLH+1, -2,-2};
  124. struct Border colBor = { 0,0,1,0,JAM1,5,&colBorXY[0],NULL };
  125.  
  126. SHORT colGadX[MAXCOLORS], colGadY[MAXCOLORS];
  127. struct Image  *colImgs;
  128. struct Gadget *colGads;
  129.  
  130.  
  131. /* Palette Window */ 
  132. struct NewWindow newpWin = {
  133.    224,24, PWIDE,NULL,   /* Height set before opening */
  134.    2,1,
  135.    MOUSEMOVE | GADGETUP | GADGETDOWN,             /* IDCMP Flags */
  136.    ACTIVATE | SMART_REFRESH | WINDOWDRAG | WINDOWDEPTH, /* Flags */
  137.    NULL,             /* First Gadget - set before OpenWindow */
  138.    NULL,             /* CheckMark */
  139.    "Color Palette",   /* Title */
  140.    NULL,             /* Set up screen pointer before opening */
  141.    NULL,             /* No superbitmap */
  142.    0,0,0,0,          /* Max and Min (no sizing) */
  143.    NULL              /* Screen type - set before opening */
  144.    };
  145.  
  146.  
  147. struct Window   *pWin;      /* Palette window pointer */
  148. struct ViewPort *pVp;
  149. struct RastPort *pRp;
  150. struct IntuiMessage *pMsg;
  151. struct Gadget   *pMGad;
  152. int    pDepth, pColors, pReg, pTopSize;
  153. ULONG  pMClass, colGadMem, colGadMemSize;
  154. UWORD  curColor[MAXCOLORS], resColor[MAXCOLORS];
  155. USHORT pMGadID;
  156. BOOL Done;
  157.  
  158. /* 
  159.  * doPalette(screen)
  160.  */
  161.  
  162. doPalette(pScr)
  163. struct Screen *pScr;
  164.    {
  165.    int k;
  166.  
  167.    /* For safe pcleanup */
  168.    pWin    = NULL;
  169.    colGadMem = NULL;
  170.  
  171.    /* From the screen - needed by Allocations */
  172.    pVp = &pScr->ViewPort;
  173.    pDepth = pScr->RastPort.BitMap->Depth;
  174.    if (pDepth > 5) pDepth=5;
  175.    pColors = 1 << pDepth;
  176.  
  177.    /* Allocations - Note: Requires initialized pDepth, pColors ! */
  178.    colGadMemSize =
  179.       (sizeof(struct Gadget)<<pDepth)+(sizeof(struct Image)<<pDepth);
  180.    if(!(colGadMem=(ULONG)AllocMem(colGadMemSize,MEMF_PUBLIC|MEMF_CLEAR)))
  181.       {
  182.       pcleanup("Can't alloc gadget mem\n");
  183.       return(0);
  184.       }
  185.  
  186.    colGads = (struct Gadget *)colGadMem;
  187.    colImgs = (struct Image *)(colGadMem+(sizeof(struct Gadget)<<pDepth));
  188.  
  189.    /* Init Color Gadgets, linking last one to okGad */
  190.    initColGads(colGads,colImgs,pColors,&okGad);
  191.  
  192.    newpWin.FirstGadget = colGads;
  193.    newpWin.Screen = pScr;
  194.    newpWin.Type = pScr->Flags & 0x000f;
  195.    /* Color rows * color gad Y spacing + 10 for top border */
  196.    pTopSize = ((((pColors-1) >> 3)+1) * COLDY)+ 10;
  197.    newpWin.Height = PBASEHIGH + pTopSize;
  198.    newpWin.TopEdge=(pScr->Height-PBASEHIGH)/2;
  199.    newpWin.LeftEdge=(pScr->Width-PWIDE)/2;
  200.  
  201.    if (!(pWin = (struct Window *)OpenWindow(&newpWin)))
  202.       {
  203.       pcleanup("Can't open palette window\n");
  204.       return(0);
  205.       }
  206.    pRp = pWin->RPort;
  207.  
  208.    /* Get current colors and save for RESET */
  209.    for (k=0; k < pColors; k++)
  210.       curColor[k] = resColor[k] = GetRGB4(pVp->ColorMap,k);
  211.  
  212.    pReg = 0;  /* initially select color 0 */
  213.    selectColor(0, pReg);
  214.  
  215.    Done = FALSE;
  216.    while(! Done)
  217.       {
  218.       Wait(1<<pWin->UserPort->mp_SigBit);
  219.  
  220.       while (pMsg=(struct IntuiMessage *)GetMsg(pWin->UserPort))
  221.          {
  222.          /*  Note that rbg values are modified in 3 cases:
  223.           *     GADGETDOWN of the props
  224.           *     GADGETUP   of the props
  225.           *     MOUSEMOVE (reporting turned on by the props)
  226.           */
  227.  
  228.          pMClass  = pMsg->Class;
  229.          pMGad   = (struct Gadget *)pMsg->IAddress;
  230.          pMGadID = pMGad->GadgetID;
  231.  
  232.          ReplyMsg(pMsg);
  233.  
  234.          switch(pMClass)
  235.             {
  236.             case MOUSEMOVE:
  237.                modColor(pReg);
  238.                break;
  239.  
  240.             case GADGETDOWN:
  241.                switch(pMGadID)
  242.                   {
  243.                   case rID:  case gID:  case bID:
  244.                      modColor(pReg);
  245.                      break;
  246.  
  247.                   default:
  248.                      if ((pMGadID >= colID)&&(pMGadID<(colID+pColors)))
  249.                         {
  250.                         k = pReg;
  251.                         pReg = pMGadID - colID;
  252.                         selectColor(k,pReg);
  253.                         }
  254.                      break;
  255.                   }
  256.  
  257.             case GADGETUP:
  258.                switch (pMGadID)
  259.                   {
  260.                   case okID:        /* OK */
  261.                      Done = TRUE;
  262.                      break;
  263.  
  264.                   case resID:      /* RESET */
  265.                      resetColors();
  266.                      selectColor(pReg,pReg);
  267.              break;
  268.  
  269.                   case canID:      /* CANCEL */
  270.                      resetColors();
  271.                      Done = TRUE;
  272.                      break;
  273.  
  274.           case swpID:      /* swap two colors */
  275.              doswap();
  276.                      break;
  277.  
  278.           case copID:      /* copy color a to color b */
  279.              docopy();
  280.              break;
  281.  
  282.                   case rID:  case gID:  case bID:
  283.                      modColor(pReg);
  284.              break;
  285.  
  286.                   default:
  287.                      break;
  288.                   }
  289.  
  290.             default:
  291.                break;
  292.             }
  293.          }
  294.       }
  295.    pcleanup("");
  296.    return(1);
  297.    }
  298.  
  299.  
  300. pcleanup(s)
  301. char *s;
  302.    {
  303.    if (*s) printf(s);
  304.    if (pWin)
  305.       {
  306.       while (pMsg=(struct IntuiMessage *)GetMsg(pWin->UserPort))
  307.          {
  308.          ReplyMsg(pMsg);
  309.          }
  310.       CloseWindow(pWin);
  311.    if (colGadMem)  FreeMem(colGadMem,colGadMemSize);
  312.       }
  313.    }
  314.  
  315.  
  316. initColGads(gad,img,colors,lastLink)
  317. struct Gadget *gad;
  318. struct Image  *img;
  319. int colors;
  320. struct Gadget *lastLink;
  321.    {
  322.    struct Gadget *nextgad;
  323.    int k, lastk, row, col, rows;
  324.  
  325.    colW  = COLW8;
  326.    if (colors == 4)  colW = COLW4;
  327.    if (colors == 2)  colW = COLW2;
  328.    colDX = colW + COLSW; /* color width + space between colors */
  329.    colBorXY[2] = colW+1;
  330.    colBorXY[4] = colW+1;
  331.  
  332.    /*  Make array of colGad xy positions */
  333.    rows = ((colors-1) >> 3) + 1;  /* 8 colors per row */
  334.    for (row=0, k=0; row<rows; row++)
  335.       {
  336.       for (col=0; (col<8)&&(k<colors); col++, k++)
  337.          {
  338.          colGadX[k] = COLX + (col * colDX);
  339.          colGadY[k] = COLY + (row * COLDY);
  340.          }
  341.       }
  342.  
  343.    nextgad = gad+1;
  344.    lastk = colors-1;
  345.    for (k=0; k<colors; k++, gad++, img++, nextgad++)
  346.       {
  347.       if (k < lastk) gad->NextGadget = nextgad;
  348.       else gad->NextGadget = lastLink;
  349.       gad->LeftEdge = colGadX[k];
  350.       gad->TopEdge =  colGadY[k];
  351.       gad->Width =  colW;
  352.       gad->Height = COLH;
  353.       gad->Flags = GADGHBOX|GADGIMAGE;
  354.       gad->Activation = GADGIMMEDIATE;
  355.       gad->GadgetType = BOOLGADGET;
  356.       gad->GadgetRender = (APTR)img;
  357.       gad->GadgetID = colID + k;
  358.  
  359.       img->Width  = colW;
  360.       img->Height = COLH;
  361.       img->Depth  = 1;
  362.       img->PlaneOnOff = k;
  363.       }
  364.    }
  365.  
  366. resetColors()
  367.    {
  368.    int k; 
  369.    for(k=0; k<pColors; k++) curColor[k] = resColor[k];
  370.    LoadRGB4(pVp, &resColor[0], pColors);
  371.    }
  372.  
  373. selectColor(oldreg,newreg)
  374. int oldreg, newreg;
  375.    {
  376.    USHORT rgb;
  377.    int chx1,chy1,chx2,chy2;
  378.  
  379.    colBor.FrontPen = 0;
  380.    DrawBorder(pRp,&colBor,colGadX[oldreg],colGadY[oldreg]);
  381.    colBor.FrontPen = 1;
  382.    DrawBorder(pRp,&colBor,colGadX[newreg],colGadY[newreg]);
  383.  
  384.    /* Large chip of selected color */
  385.    chx1 = 8;  chy1 = pWin->Height - 78;
  386.    chx2 = 34; chy2 = pWin->Height - 38;
  387.  
  388.    SetDrMd(pRp,JAM1);
  389.    SetAPen(pRp,1);
  390.    Move(pRp,chx1,chy1);
  391.    Draw(pRp,chx2,chy1);
  392.    Draw(pRp,chx2,chy2);
  393.    Draw(pRp,chx1,chy2);
  394.    Draw(pRp,chx1,chy1);
  395.    SetAPen(pRp,newreg);
  396.    RectFill(pRp,chx1+2,chy1+2,chx2-2,chy2-2);
  397.  
  398.    rgb = curColor[newreg];
  399.    modColorProps(rgb);
  400.    printRGB((rgb>>8)&0xF, (rgb>>4)&0xF, rgb&0xF);
  401.    }
  402.  
  403.  
  404. modColor(reg)
  405. int reg;
  406.    {
  407.    USHORT r,g,b;
  408.  
  409.    r = (rInf.HorizPot >> 12) & 0x0F;
  410.    g = (gInf.HorizPot >> 12) & 0x0F;
  411.    b = (bInf.HorizPot >> 12) & 0x0F;
  412.  
  413.    curColor[reg] = (r << 8)|(g << 4)|b;
  414.  
  415.    LoadRGB4(pVp,&curColor[0],pColors);
  416.  
  417.    printRGB(r,g,b);
  418.    }
  419.  
  420. doswap()
  421. {
  422. int reg1,reg2;
  423.  
  424. reg1=pReg;   /* current register */
  425. reg2=secondregister();
  426.  
  427. swap(reg1,reg2);
  428. }
  429.  
  430. docopy()
  431. {
  432. int reg1,reg2;
  433.  
  434. reg1=pReg;
  435. reg2=secondregister();
  436.  
  437. copy(reg1,reg2);
  438. }
  439.  
  440. /*** this routine gets second register for swap, copy, or spread ***/
  441. secondregister()
  442. {
  443. int done,r=0;
  444.  
  445. done=0;
  446.  
  447.    while(done==0)
  448.       {
  449.       Wait(1<<pWin->UserPort->mp_SigBit);
  450.  
  451.       while (pMsg=(struct IntuiMessage *)GetMsg(pWin->UserPort))
  452.          {
  453.          pMClass  = pMsg->Class;
  454.          pMGad   = (struct Gadget *)pMsg->IAddress;
  455.          pMGadID = pMGad->GadgetID;
  456.  
  457.          ReplyMsg(pMsg);
  458.  
  459.          switch(pMClass)
  460.             {
  461.             case GADGETDOWN:
  462.                switch(pMGadID)
  463.                   {
  464.                   default:
  465.                      if ((pMGadID >= colID)&&(pMGadID<(colID+pColors)))
  466.                         {
  467.                         r = pMGadID - colID;
  468.                         return(r);
  469.                         }
  470.                      break;
  471.                     }
  472.            }
  473.           }
  474.      }
  475. }
  476.  
  477. swap(reg1,reg2)
  478. int reg1,reg2;
  479. {
  480. UWORD temp;
  481.  
  482. temp=curColor[reg1];
  483. curColor[reg1]=curColor[reg2];
  484. curColor[reg2]=temp;
  485. LoadRGB4(pVp,&curColor[0],pColors);
  486. selectColor(reg1,reg2);   /* just redisplay to show changes */
  487. pReg=reg2;
  488. }
  489.  
  490. copy(reg1,reg2)
  491. int reg1,reg2;
  492. {
  493. curColor[reg2]=curColor[reg1];
  494. LoadRGB4(pVp,&curColor[0],pColors);
  495. selectColor(reg1,reg2);
  496. pReg=reg2;
  497. }
  498.  
  499. modColorProps(rgb)
  500. USHORT rgb;
  501.    {
  502.    USHORT r,g,b;
  503.  
  504.    r = (rgb >> 8) & 0xF;
  505.    g = (rgb >> 4) & 0xF;
  506.    b = rgb & 0xF;
  507.    ModifyProp(&rGad,pWin,0,rInf.Flags,(r*0x11111111),
  508.               rInf.VertPot,rInf.HorizBody,rInf.VertBody);
  509.    ModifyProp(&gGad,pWin,0,gInf.Flags,(g*0x11111111),
  510.               gInf.VertPot,gInf.HorizBody,gInf.VertBody);
  511.    ModifyProp(&bGad,pWin,0,bInf.Flags,(b*0x11111111),
  512.               bInf.VertPot,bInf.HorizBody,bInf.VertBody);
  513.    }
  514.  
  515.  
  516. printRGB(r,g,b)
  517. USHORT r,g,b;
  518.    {
  519.    char str[4];
  520.    char *xstr;
  521.  
  522.    xstr = "0123456789ABCDEF";
  523.    str[0] = xstr[r];
  524.    str[1] = xstr[g];
  525.    str[2] = xstr[b];
  526.    SetAPen(pRp,1);
  527.    SetBPen(pRp,0);
  528.    SetDrMd(pRp,JAM2);
  529.    Move(pRp,PWIDE-40,pWin->Height - 40);
  530.    Text(pRp,&str[0],3);
  531.    }
  532.