home *** CD-ROM | disk | FTP | other *** search
/ The Best of Mecomp Multimedia 2 / MECOMP-CD-II.iso / amiga / grafix / flashmandel / sources / modules / colorwindow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-18  |  34.8 KB  |  868 lines

  1. /*******************************************************************************
  2. **
  3. **  Coded by Dino Papararo                 18-Apr-1998
  4. **
  5. *******************************************************************************/
  6.  
  7. #define __USE_SYSBASE
  8.  
  9. #include <math.h>
  10. #include <string.h>
  11. #include <exec/types.h>
  12. #include <exec/execbase.h>
  13. #include <intuition/intuition.h>
  14. #include <intuition/gadgetclass.h>
  15. #include <proto/exec.h>
  16. #include <proto/intuition.h>
  17. #include <proto/graphics.h>
  18. #include <proto/gadtools.h>
  19. #include <proto/utility.h>
  20.  
  21. #define PALETTE      (0L)
  22. #define ACCEPT       (1L)
  23. #define RESET        (2L)
  24. #define CANCEL       (3L)
  25. #define RED          (4L)
  26. #define GREEN        (5L)
  27. #define BLUE         (6L)
  28. #define COPY         (7L)
  29. #define SWAP         (8L)
  30. #define SPREAD       (9L)
  31. #define UNDO         (10L)
  32. #define INVERT       (11L)
  33.  
  34. #define STARTPEN     (4L)
  35.  
  36. #define MINVALUE     (0L)
  37. #define MAXVALUE     (255L)
  38. #define DELTA        (10L)
  39.  
  40. BOOL ModifyPalette  (struct Window *,WORD,WORD,WORD,WORD,ULONG *);
  41. VOID KeepPalette    (struct Window *);
  42. VOID Copy           (struct Window *,ULONG);
  43. VOID Paste          (struct Window *,const ULONG);
  44. VOID Swap           (struct Window *,const ULONG,const ULONG);
  45. BOOL Spread         (struct Window *,const ULONG,const ULONG);
  46. VOID InvertPalette  (struct Window *,ULONG,ULONG);
  47.  
  48. IMPORT struct ExecBase *SysBase;
  49.  
  50. ULONG COLOR_RGB [3L * 256L + 2L],UNDO_RGB [3L * 256L + 2L],COPY_RGB [3L];
  51.  
  52. struct NewGadget BUTTON_GAD,SLIDER_GAD,PALETTE_GAD;
  53.  
  54. BOOL ModifyPalette (struct Window *Win,WORD LeftEdge,WORD TopEdge,WORD Width,WORD Height,ULONG *Palette32)
  55. {
  56. struct Window *ColorWin;
  57.  
  58. struct IntuiMessage *Message;
  59.  
  60. struct Gadget *GadList = NULL,*MyButtonGad,*MyPaletteGad;
  61.  
  62. struct Gadget *RedSliderGad,*GreenSliderGad,*BlueSliderGad,*MyGad;
  63.  
  64. BOOL Copy_Msg = FALSE,Swap_Msg = FALSE,Spread_Msg = FALSE,Exit = FALSE;
  65.  
  66. ULONG SelectedPen = STARTPEN,OldPen = NULL,NewPen;
  67.  
  68. UWORD MyCode;
  69.  
  70. ULONG MyClass,Colors,ColorBase = 3L * STARTPEN + 1L;
  71.  
  72. ULONG RedLevel,GreenLevel,BlueLevel;
  73.  
  74. CPTR *VInfo;
  75.  
  76.   Colors = 1L << Win->RPort->BitMap->Depth;
  77.  
  78.   if (Colors < 2L) return FALSE;
  79.  
  80.   VInfo = GetVisualInfo (Win->WScreen,TAG_END);
  81.  
  82.   if (! VInfo) return FALSE;
  83.  
  84.   COLOR_RGB [0L] = UNDO_RGB [0L] = Colors << 16L;
  85.  
  86.   COLOR_RGB [3L * Colors + 1L] = UNDO_RGB [3L * Colors + 1L] = NULL;
  87.  
  88.   COPY_RGB [0L] = COPY_RGB [1L] = COPY_RGB [2L] = NULL;
  89.  
  90.   GetRGB32 (ViewPortAddress (Win)->ColorMap,0L,Colors,&COLOR_RGB [1L]);
  91.  
  92.   RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  93.  
  94.   GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  95.  
  96.   BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  97.  
  98.   MyButtonGad = CreateContext (&GadList);
  99.  
  100.   BUTTON_GAD.ng_VisualInfo = PALETTE_GAD.ng_VisualInfo = SLIDER_GAD.ng_VisualInfo = VInfo;
  101.  
  102.   BUTTON_GAD.ng_LeftEdge   = 10;
  103.  
  104.   BUTTON_GAD.ng_TopEdge    = 180;
  105.  
  106.   BUTTON_GAD.ng_Width      = 80;
  107.  
  108.   BUTTON_GAD.ng_Height     = 20;
  109.  
  110.   BUTTON_GAD.ng_GadgetText = "_Accept";
  111.  
  112.   BUTTON_GAD.ng_GadgetID   = ACCEPT;
  113.  
  114.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  115.  
  116.   BUTTON_GAD.ng_LeftEdge  += (15 + BUTTON_GAD.ng_Width);
  117.  
  118.   BUTTON_GAD.ng_GadgetText = "Reset";
  119.  
  120.   BUTTON_GAD.ng_GadgetID   = RESET;
  121.  
  122.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,TAG_END);
  123.  
  124.   BUTTON_GAD.ng_LeftEdge  += (15 + BUTTON_GAD.ng_Width);
  125.  
  126.   BUTTON_GAD.ng_GadgetText = "_Cancel";
  127.  
  128.   BUTTON_GAD.ng_GadgetID   = CANCEL;
  129.  
  130.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  131.  
  132.   BUTTON_GAD.ng_TopEdge    = 8;
  133.  
  134.   BUTTON_GAD.ng_GadgetText = "C_opy";
  135.  
  136.   BUTTON_GAD.ng_GadgetID   = COPY;
  137.  
  138.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  139.  
  140.   BUTTON_GAD.ng_TopEdge   += (6 + BUTTON_GAD.ng_Height);
  141.  
  142.   BUTTON_GAD.ng_GadgetText = "S_wap";
  143.  
  144.   BUTTON_GAD.ng_GadgetID   = SWAP;
  145.  
  146.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  147.  
  148.   BUTTON_GAD.ng_TopEdge   += (6 + BUTTON_GAD.ng_Height);
  149.  
  150.   BUTTON_GAD.ng_GadgetText = "_Spread";
  151.  
  152.   BUTTON_GAD.ng_GadgetID   = SPREAD;
  153.  
  154.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  155.  
  156.   BUTTON_GAD.ng_TopEdge   += (6 + BUTTON_GAD.ng_Height);
  157.  
  158.   BUTTON_GAD.ng_GadgetText = "_Invert";
  159.  
  160.   BUTTON_GAD.ng_GadgetID   = INVERT;
  161.  
  162.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  163.  
  164.   BUTTON_GAD.ng_TopEdge   += (6 + BUTTON_GAD.ng_Height);
  165.  
  166.   BUTTON_GAD.ng_GadgetText = "_Undo";
  167.  
  168.   BUTTON_GAD.ng_GadgetID   = UNDO;
  169.  
  170.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  171.  
  172.   SLIDER_GAD.ng_LeftEdge    = 25;
  173.  
  174.   SLIDER_GAD.ng_TopEdge     = 142;
  175.  
  176.   SLIDER_GAD.ng_Width       = 225;
  177.  
  178.   SLIDER_GAD.ng_Height      = 8;
  179.  
  180.   SLIDER_GAD.ng_GadgetText  = "R";
  181.  
  182.   SLIDER_GAD.ng_GadgetID    = RED;
  183.  
  184.   RedSliderGad = CreateGadget (SLIDER_KIND,MyButtonGad,&SLIDER_GAD,GA_RelVerify,TRUE,GTSL_Max,MAXVALUE,GTSL_Level,(WORD) RedLevel,GTSL_LevelFormat,"%03ld",GTSL_MaxLevelLen,3,GTSL_LevelPlace,PLACETEXT_RIGHT,TAG_END);
  185.  
  186.   SLIDER_GAD.ng_TopEdge    += (4 + SLIDER_GAD.ng_Height);
  187.  
  188.   SLIDER_GAD.ng_GadgetText  = "G";
  189.  
  190.   SLIDER_GAD.ng_GadgetID    = GREEN;
  191.  
  192.   GreenSliderGad = CreateGadget (SLIDER_KIND,RedSliderGad,&SLIDER_GAD,GA_RelVerify,TRUE,GTSL_Max,MAXVALUE,GTSL_Level,(WORD) GreenLevel,GTSL_LevelFormat,"%03ld",GTSL_MaxLevelLen,3,GTSL_LevelPlace,PLACETEXT_RIGHT,TAG_END);
  193.  
  194.   SLIDER_GAD.ng_TopEdge    += (4 + SLIDER_GAD.ng_Height);
  195.  
  196.   SLIDER_GAD.ng_GadgetText  = "B";
  197.  
  198.   SLIDER_GAD.ng_GadgetID    = BLUE;
  199.  
  200.   BlueSliderGad = CreateGadget (SLIDER_KIND,GreenSliderGad,&SLIDER_GAD,GA_RelVerify,TRUE,GTSL_Max,MAXVALUE,GTSL_Level,(WORD) BlueLevel,GTSL_LevelFormat,"%03ld",GTSL_MaxLevelLen,3,GTSL_LevelPlace,PLACETEXT_RIGHT,TAG_END);
  201.  
  202.   PALETTE_GAD.ng_LeftEdge   = 10;
  203.  
  204.   PALETTE_GAD.ng_TopEdge    = 5;
  205.  
  206.   PALETTE_GAD.ng_Width      = 185;
  207.  
  208.   PALETTE_GAD.ng_Height     = 132;
  209.  
  210.   PALETTE_GAD.ng_GadgetID   = PALETTE;
  211.  
  212.   MyPaletteGad = CreateGadget (PALETTE_KIND,BlueSliderGad,&PALETTE_GAD,GTPA_Color,4,GTPA_IndicatorWidth,10,GTPA_NumColors,(UWORD) Colors,TAG_END);
  213.  
  214.   if (! MyPaletteGad)
  215.   {
  216.      FreeGadgets (GadList);
  217.  
  218.      FreeVisualInfo (VInfo);
  219.  
  220.      return FALSE;
  221.   }
  222.  
  223.   ColorWin = OpenWindowTags (0,WA_Left,LeftEdge,
  224.                                WA_Top,TopEdge,
  225.                                WA_Width,Width,
  226.                                WA_Height,Height,
  227.                                WA_Title,"Palette requester",
  228.                                WA_ScreenTitle,"Modify palette...",
  229.                                WA_CustomScreen,Win->WScreen,
  230.                                WA_IDCMP,IDCMP_CLOSEWINDOW|IDCMP_REFRESHWINDOW|IDCMP_VANILLAKEY|IDCMP_GADGETDOWN|BUTTONIDCMP|SLIDERIDCMP|PALETTEIDCMP,
  231.                                WA_Flags,WFLG_ACTIVATE|WFLG_DRAGBAR|WFLG_SIMPLE_REFRESH|WFLG_RMBTRAP|WFLG_GIMMEZEROZERO,
  232.                                WA_Gadgets,GadList,
  233.                                TAG_END);
  234.  
  235.   if (! ColorWin)
  236.   {
  237.      FreeGadgets (GadList);
  238.  
  239.      FreeVisualInfo (VInfo);
  240.  
  241.      return FALSE;
  242.   }
  243.  
  244.   GT_RefreshWindow (ColorWin,NULL);
  245.  
  246.   do { WaitPort (ColorWin->UserPort);
  247.  
  248.        Message = (struct IntuiMessage *) GT_GetIMsg (ColorWin->UserPort);
  249.  
  250.        MyGad   = (struct Gadget *) Message->IAddress;
  251.  
  252.        MyClass = Message->Class;
  253.  
  254.        MyCode  = Message->Code;
  255.  
  256.        GT_ReplyIMsg ((struct IntuiMessage *) Message);
  257.  
  258.        switch (MyClass)
  259.        {
  260.               case IDCMP_REFRESHWINDOW : GT_BeginRefresh (ColorWin);
  261.  
  262.                                          GT_EndRefresh   (ColorWin,TRUE);
  263.  
  264.                                          break;
  265.  
  266.               case IDCMP_VANILLAKEY    : switch (MyCode)
  267.                                          {
  268.                                                  case 'a' :
  269.                                                  case 'A' : GetRGB32 (ViewPortAddress (ColorWin)->ColorMap,0L,Colors,&Palette32 [1L]);
  270.  
  271.                                                             Exit = TRUE;
  272.  
  273.                                                             break;
  274.  
  275.                                                  case 'c' :
  276.                                                  case 'C' : LoadRGB32 (ViewPortAddress (ColorWin),Palette32);
  277.  
  278.                                                             Exit = TRUE;
  279.  
  280.                                                             break;
  281.  
  282.                                                  case 'o' :
  283.                                                  case 'O' : Copy (ColorWin,SelectedPen);
  284.  
  285.                                                             Copy_Msg = TRUE;
  286.  
  287.                                                             Swap_Msg = FALSE;
  288.  
  289.                                                             Spread_Msg = FALSE;
  290.  
  291.                                                             break;
  292.  
  293.                                                  case 'w' :
  294.                                                  case 'W' : OldPen = SelectedPen;
  295.  
  296.                                                             Copy_Msg = FALSE;
  297.  
  298.                                                             Swap_Msg = TRUE;
  299.  
  300.                                                             Spread_Msg = FALSE;
  301.  
  302.                                                             break;
  303.  
  304.                                                  case 's' :
  305.                                                  case 'S' : OldPen = SelectedPen;
  306.  
  307.                                                             Copy_Msg = FALSE;
  308.  
  309.                                                             Swap_Msg = FALSE;
  310.  
  311.                                                             Spread_Msg = TRUE;
  312.  
  313.                                                             break;
  314.  
  315.                                                  case 'u' :
  316.                                                  case 'U' : LoadRGB32 (ViewPortAddress (ColorWin),UNDO_RGB);
  317.  
  318.                                                             GetRGB32 (ViewPortAddress (ColorWin)->ColorMap,0L,Colors,&COLOR_RGB [1L]);
  319.  
  320.                                                             RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  321.  
  322.                                                             GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  323.  
  324.                                                             BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  325.  
  326.                                                             GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  327.  
  328.                                                             GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  329.  
  330.                                                             GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  331.  
  332.                                                             break;
  333.  
  334.                                                  case 'E' : RedLevel = COLOR_RGB [ColorBase] >> 24L;
  335.  
  336.                                                             if (RedLevel > MINVALUE)
  337.                                                             {
  338.                                                                KeepPalette (Win);
  339.  
  340.                                                                RedLevel = (RedLevel < (MINVALUE + DELTA)) ? MINVALUE : RedLevel - DELTA;
  341.  
  342.                                                                GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  343.  
  344.                                                                COLOR_RGB [ColorBase] = RedLevel << 24L;
  345.  
  346.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  347.                                                             }
  348.  
  349.                                                             break;
  350.  
  351.                                                  case 'e' : RedLevel = COLOR_RGB [ColorBase] >> 24L;
  352.  
  353.                                                             if (RedLevel > MINVALUE)
  354.                                                             {
  355.                                                                KeepPalette (Win);
  356.  
  357.                                                                RedLevel--;
  358.  
  359.                                                                GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  360.  
  361.                                                                COLOR_RGB [ColorBase] = RedLevel << 24L;
  362.  
  363.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  364.                                                             }
  365.  
  366.                                                             break;
  367.  
  368.                                                  case 'T' : RedLevel = COLOR_RGB [ColorBase] >> 24L;
  369.  
  370.                                                             if (RedLevel < MAXVALUE)
  371.                                                             {
  372.                                                                KeepPalette (Win);
  373.  
  374.                                                                RedLevel += DELTA;
  375.  
  376.                                                                if (RedLevel > MAXVALUE) RedLevel = MAXVALUE;
  377.  
  378.                                                                GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  379.  
  380.                                                                COLOR_RGB [ColorBase] = RedLevel << 24L;
  381.  
  382.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  383.                                                             }
  384.  
  385.                                                             break;
  386.  
  387.                                                  case 't' : RedLevel = COLOR_RGB [ColorBase] >> 24L;
  388.  
  389.                                                             if (RedLevel < MAXVALUE)
  390.                                                             {
  391.                                                                KeepPalette (Win);
  392.  
  393.                                                                RedLevel++;
  394.  
  395.                                                                GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  396.  
  397.                                                                COLOR_RGB [ColorBase] = RedLevel << 24L;
  398.  
  399.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  400.                                                             }
  401.  
  402.                                                             break;
  403.  
  404.                                                  case 'F' : GreenLevel = COLOR_RGB [ColorBase + 1L] >> 24L;
  405.  
  406.                                                             if (GreenLevel > MINVALUE)
  407.                                                             {
  408.                                                                KeepPalette (Win);
  409.  
  410.                                                                GreenLevel = (GreenLevel < (MINVALUE + DELTA)) ? MINVALUE : GreenLevel - DELTA;
  411.  
  412.                                                                GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  413.  
  414.                                                                COLOR_RGB [ColorBase + 1L] = GreenLevel << 24L;
  415.  
  416.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  417.                                                             }
  418.  
  419.                                                             break;
  420.  
  421.                                                  case 'f' : GreenLevel = COLOR_RGB [ColorBase + 1L] >> 24L;
  422.  
  423.                                                             if (GreenLevel > MINVALUE)
  424.                                                             {
  425.                                                                KeepPalette (Win);
  426.  
  427.                                                                GreenLevel--;
  428.  
  429.                                                                GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  430.  
  431.                                                                COLOR_RGB [ColorBase + 1L] = GreenLevel << 24L;
  432.  
  433.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  434.                                                             }
  435.  
  436.                                                             break;
  437.  
  438.                                                  case 'H' : GreenLevel = COLOR_RGB [ColorBase + 1L] >> 24L;
  439.  
  440.                                                             if (GreenLevel < MAXVALUE)
  441.                                                             {
  442.                                                                KeepPalette (Win);
  443.  
  444.                                                                GreenLevel += DELTA;
  445.  
  446.                                                                if (GreenLevel > MAXVALUE) GreenLevel = MAXVALUE;
  447.  
  448.                                                                GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  449.  
  450.                                                                COLOR_RGB [ColorBase + 1L] = GreenLevel << 24L;
  451.  
  452.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  453.                                                             }
  454.  
  455.                                                             break;
  456.  
  457.                                                  case 'h' : GreenLevel = COLOR_RGB [ColorBase + 1L] >> 24L;
  458.  
  459.                                                             if (GreenLevel < MAXVALUE)
  460.                                                             {
  461.                                                                KeepPalette (Win);
  462.  
  463.                                                                GreenLevel++;
  464.  
  465.                                                                GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  466.  
  467.                                                                COLOR_RGB [ColorBase + 1L] = GreenLevel << 24L;
  468.  
  469.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  470.                                                             }
  471.  
  472.                                                             break;
  473.  
  474.                                                  case 'V' : BlueLevel = COLOR_RGB [ColorBase + 2L] >> 24L;
  475.  
  476.                                                             if (BlueLevel > MINVALUE)
  477.                                                             {
  478.                                                                KeepPalette (Win);
  479.  
  480.                                                                BlueLevel = (BlueLevel < (MINVALUE + DELTA)) ? MINVALUE : BlueLevel - DELTA;
  481.  
  482.                                                                GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  483.  
  484.                                                                COLOR_RGB [ColorBase + 2L] = BlueLevel << 24L;
  485.  
  486.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  487.                                                             }
  488.  
  489.                                                             break;
  490.  
  491.                                                  case 'v' : BlueLevel = COLOR_RGB [ColorBase + 2L] >> 24L;
  492.  
  493.                                                             if (BlueLevel > MINVALUE)
  494.                                                             {
  495.                                                                KeepPalette (Win);
  496.  
  497.                                                                BlueLevel--;
  498.  
  499.                                                                GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  500.  
  501.                                                                COLOR_RGB [ColorBase + 2L] = BlueLevel << 24L;
  502.  
  503.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  504.                                                             }
  505.  
  506.                                                             break;
  507.  
  508.                                                  case 'N' : BlueLevel = COLOR_RGB [ColorBase + 2L] >> 24L;
  509.  
  510.                                                             if (BlueLevel < MAXVALUE)
  511.                                                             {
  512.                                                                KeepPalette (Win);
  513.  
  514.                                                                BlueLevel += DELTA;
  515.  
  516.                                                                if (BlueLevel > MAXVALUE) BlueLevel = MAXVALUE;
  517.  
  518.                                                                GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  519.  
  520.                                                                COLOR_RGB [ColorBase + 2L] = BlueLevel << 24L;
  521.  
  522.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  523.                                                             }
  524.  
  525.                                                             break;
  526.  
  527.                                                  case 'n' : BlueLevel = COLOR_RGB [ColorBase + 2L] >> 24L;
  528.  
  529.                                                             if (BlueLevel < MAXVALUE)
  530.                                                             {
  531.                                                                KeepPalette (Win);
  532.  
  533.                                                                BlueLevel++;
  534.  
  535.                                                                GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  536.  
  537.                                                                COLOR_RGB [ColorBase + 2L] = BlueLevel << 24L;
  538.  
  539.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  540.                                                             }
  541.  
  542.                                                             break;
  543.  
  544.                                                  case 'I' :
  545.                                                  case 'i' : KeepPalette (Win);
  546.  
  547.                                                             InvertPalette (Win,STARTPEN,Colors - 1L);
  548.  
  549.                                                             RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  550.  
  551.                                                             GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  552.  
  553.                                                             BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  554.  
  555.                                                             GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  556.  
  557.                                                             GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  558.  
  559.                                                             GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  560.  
  561.                                                             break;
  562.                                          }
  563.  
  564.                                          break;
  565.  
  566.               case IDCMP_GADGETUP      : switch (MyGad->GadgetID)
  567.                                          {
  568.                                                  case ACCEPT  : GetRGB32 (ViewPortAddress (ColorWin)->ColorMap,0L,Colors,&Palette32 [1L]);
  569.  
  570.                                                                 Exit = TRUE;
  571.  
  572.                                                                 break;
  573.  
  574.                                                  case RESET   : KeepPalette (Win);
  575.  
  576.                                                                 LoadRGB32 (ViewPortAddress (ColorWin),Palette32);
  577.  
  578.                                                                 GetRGB32 (ViewPortAddress (ColorWin)->ColorMap,0L,Colors,&COLOR_RGB [1L]);
  579.  
  580.                                                                 RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  581.  
  582.                                                                 GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  583.  
  584.                                                                 BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  585.  
  586.                                                                 GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  587.  
  588.                                                                 GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  589.  
  590.                                                                 GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  591.  
  592.                                                                 break;
  593.  
  594.                                                  case CANCEL  : LoadRGB32 (ViewPortAddress (ColorWin),Palette32);
  595.  
  596.                                                                 Exit = TRUE;
  597.  
  598.                                                                 break;
  599.  
  600.                                                  case COPY    : Copy (ColorWin,SelectedPen);
  601.  
  602.                                                                 Copy_Msg = TRUE;
  603.  
  604.                                                                 Swap_Msg = FALSE;
  605.  
  606.                                                                 Spread_Msg = FALSE;
  607.  
  608.                                                                 break;
  609.  
  610.                                                  case SWAP    : OldPen = SelectedPen;
  611.  
  612.                                                                 Copy_Msg = FALSE;
  613.  
  614.                                                                 Swap_Msg = TRUE;
  615.  
  616.                                                                 Spread_Msg = FALSE;
  617.  
  618.                                                                 break;
  619.  
  620.                                                  case SPREAD  : OldPen = SelectedPen;
  621.  
  622.                                                                 Copy_Msg = FALSE;
  623.  
  624.                                                                 Swap_Msg = FALSE;
  625.  
  626.                                                                 Spread_Msg = TRUE;
  627.         
  628.                                                                 break;
  629.  
  630.                                                  case INVERT  : KeepPalette (Win);
  631.  
  632.                                                                 InvertPalette (Win,STARTPEN,Colors - 1L);
  633.  
  634.                                                                 RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  635.  
  636.                                                                 GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  637.  
  638.                                                                 BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  639.  
  640.                                                                 GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  641.  
  642.                                                                 GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  643.  
  644.                                                                 GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  645.  
  646.                                                                 break;
  647.  
  648.                                                  case UNDO    : LoadRGB32 (ViewPortAddress (ColorWin),UNDO_RGB);
  649.  
  650.                                                                 GetRGB32 (ViewPortAddress (ColorWin)->ColorMap,0L,Colors,&COLOR_RGB [1L]);
  651.  
  652.                                                                 RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  653.  
  654.                                                                 GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  655.  
  656.                                                                 BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  657.  
  658.                                                                 GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  659.  
  660.                                                                 GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  661.  
  662.                                                                 GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  663.  
  664.                                                                 break;
  665.  
  666.                                                  case RED     : KeepPalette (Win);
  667.  
  668.                                                                 GT_GetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,&RedLevel);
  669.  
  670.                                                                 COLOR_RGB [ColorBase]      = RedLevel << 24L;
  671.  
  672.                                                                 LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  673.  
  674.                                                                 break;
  675.  
  676.                                                  case GREEN   : KeepPalette (Win);
  677.  
  678.                                                                 GT_GetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,&GreenLevel);
  679.  
  680.                                                                 COLOR_RGB [ColorBase + 1L] = GreenLevel << 24L;
  681.  
  682.                                                                 LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  683.  
  684.                                                                 break;
  685.  
  686.                                                  case BLUE    : KeepPalette (Win);
  687.  
  688.                                                                 GT_GetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,&BlueLevel);
  689.  
  690.                                                                 COLOR_RGB [ColorBase + 2L] = BlueLevel << 24L;
  691.  
  692.                                                                 LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  693.  
  694.                                                                 break;
  695.  
  696.                                                  case PALETTE : GT_GetGadgetAttrs (MyPaletteGad,ColorWin,NULL,GTPA_Color,&SelectedPen);
  697.  
  698.                                                                 if (Copy_Msg)
  699.                                                                 {
  700.                                                                    KeepPalette (Win);
  701.  
  702.                                                                    Paste (ColorWin,SelectedPen);
  703.  
  704.                                                                    Copy_Msg = FALSE;
  705.                                                                 }
  706.  
  707.                                                                 if (Swap_Msg)
  708.                                                                 {
  709.                                                                    KeepPalette (Win);
  710.  
  711.                                                                    NewPen = SelectedPen;
  712.  
  713.                                                                    Swap (ColorWin,OldPen,NewPen);
  714.  
  715.                                                                    Swap_Msg = FALSE;
  716.                                                                 }
  717.  
  718.                                                                 if (Spread_Msg)
  719.                                                                 {
  720.                                                                    KeepPalette (Win);
  721.  
  722.                                                                    NewPen = SelectedPen;
  723.  
  724.                                                                    Spread (ColorWin,OldPen,NewPen);
  725.  
  726.                                                                    Spread_Msg = FALSE;
  727.                                                                 }
  728.  
  729.                                                                 ColorBase = 3L * SelectedPen + 1L;
  730.  
  731.                                                                 RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  732.  
  733.                                                                 GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  734.  
  735.                                                                 BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  736.  
  737.                                                                 GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  738.  
  739.                                                                 GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  740.  
  741.                                                                 GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  742.  
  743.                                                                 break;
  744.                                          }
  745.  
  746.                                          break;
  747.  
  748.               case IDCMP_CLOSEWINDOW   : Exit = TRUE;
  749.  
  750.                                          break;
  751.        }
  752.  
  753.      } while (Exit == FALSE);
  754.  
  755.   CloseWindow (ColorWin);
  756.  
  757.   FreeGadgets (GadList);
  758.  
  759.   FreeVisualInfo (VInfo);
  760.  
  761.   return TRUE;
  762. }
  763.  
  764. VOID Copy (struct Window *Win,ULONG PenNumber)
  765. {
  766.   GetRGB32 (ViewPortAddress (Win)->ColorMap,PenNumber,1L,COPY_RGB);
  767. }
  768.  
  769. VOID Paste (struct Window *Win,const ULONG PenNumber)
  770. {
  771. const ULONG ColorBase = 3L * PenNumber + 1L;
  772.  
  773. ULONG Index;
  774.  
  775.   for (Index = 0L; Index < 3L; Index++) COLOR_RGB [ColorBase + Index] = COPY_RGB [Index];
  776.  
  777.   LoadRGB32 (ViewPortAddress (Win),COLOR_RGB);
  778. }
  779.  
  780. VOID Swap (struct Window *Win,const ULONG OldPen,const ULONG NewPen)
  781. {
  782. ULONG Tmp_RGB [3L];
  783.  
  784. ULONG Index;
  785.  
  786.    GetRGB32 (ViewPortAddress (Win)->ColorMap,OldPen,1L,Tmp_RGB);
  787.  
  788.    Copy (Win,NewPen);
  789.  
  790.    Paste (Win,OldPen);
  791.  
  792.    for (Index = 0L; Index < 3L; Index++) COPY_RGB [Index] = Tmp_RGB [Index];
  793.  
  794.    Paste (Win,NewPen);
  795. }
  796.  
  797. BOOL Spread (struct Window *Win,const ULONG OldPen,const ULONG NewPen)
  798. {
  799. const ULONG StartPen = min (OldPen,NewPen) , EndPen = max (OldPen,NewPen);
  800.  
  801. const ULONG  Range   = EndPen - StartPen;
  802.  
  803. LONG RedStep,GreenStep,BlueStep;
  804.  
  805. ULONG Index,RedLevel,GreenLevel,BlueLevel,ColorBase;
  806.  
  807.   if (Range < 2L) return TRUE;
  808.  
  809.   ColorBase   = 3L * StartPen + 1L;
  810.  
  811.   RedLevel    = COLOR_RGB [ColorBase]      >> 8L;
  812.  
  813.   GreenLevel  = COLOR_RGB [ColorBase + 1L] >> 8L;
  814.  
  815.   BlueLevel   = COLOR_RGB [ColorBase + 2L] >> 8L;
  816.  
  817.   ColorBase   = 3L * EndPen + 1L;
  818.  
  819.   RedStep     = (LONG) (COLOR_RGB [ColorBase]      >> 8L);
  820.  
  821.   GreenStep   = (LONG) (COLOR_RGB [ColorBase + 1L] >> 8L);
  822.  
  823.   BlueStep    = (LONG) (COLOR_RGB [ColorBase + 2L] >> 8L);
  824.  
  825.   RedStep    -= (LONG) RedLevel;
  826.  
  827.   GreenStep  -= (LONG) GreenLevel;
  828.  
  829.   BlueStep   -= (LONG) BlueLevel;
  830.  
  831.   RedStep    /= (LONG) Range;
  832.  
  833.   GreenStep  /= (LONG) Range;
  834.  
  835.   BlueStep   /= (LONG) Range;
  836.  
  837.   for (Index = (StartPen + 1L); Index < EndPen; Index++)
  838.   {
  839.       RedLevel   += RedStep;
  840.  
  841.       GreenLevel += GreenStep;
  842.  
  843.       BlueLevel  += BlueStep;
  844.  
  845.       ColorBase   = 3L * Index + 1L;
  846.  
  847.       COLOR_RGB [ColorBase]      = RedLevel   << 8L;
  848.  
  849.       COLOR_RGB [ColorBase + 1L] = GreenLevel << 8L;
  850.  
  851.       COLOR_RGB [ColorBase + 2L] = BlueLevel  << 8L;
  852.   }
  853.  
  854.   LoadRGB32 (ViewPortAddress (Win),COLOR_RGB);
  855.  
  856.   return FALSE;
  857. }
  858.  
  859. VOID KeepPalette (struct Window *Win)
  860. {
  861.   GetRGB32 (ViewPortAddress (Win)->ColorMap,0L,1L << Win->RPort->BitMap->Depth,&UNDO_RGB [1L]);
  862. }
  863.  
  864. VOID InvertPalette (struct Window *Win,ULONG StartPen,ULONG EndPen)
  865. {
  866.   while (StartPen < EndPen) Swap (Win,StartPen++,EndPen--);
  867. }
  868.