home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 22 / AACD 22.iso / AACD / Graphics / FlashMandel / Sources / Modules / ColorWindow.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-05-20  |  38.0 KB  |  932 lines

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