home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Snippets / QuickDraw / Color Picker / Color Picker.c next >
Encoding:
C/C++ Source or Header  |  1995-02-09  |  6.7 KB  |  281 lines  |  [TEXT/MPS ]

  1. /****************************************************************************/
  2. /*                                                                            */
  3. /*    Application:    Color Picker Test                                        */
  4. /*                                                                            */
  5. /*    Description:    This application demonstrates how to use the color        */
  6. /*                    picker package to dynamically change colors in a        */
  7. /*                    custom palette.  The program basically displays 16        */
  8. /*                    squares, each representing a separate entry in the        */
  9. /*                    palette.  Clicking the mouse in any box allows the        */
  10. /*                    user to redefine that box's color with the color        */
  11. /*                    picker package routines.  The trap ActivatePalette        */
  12. /*                    is called after each color change to guarantee the        */
  13. /*                    window is updated with the palette changes.  Without    */
  14. /*                    this safeguard, random results may occur and the        */
  15. /*                    color change may not take affect until the window        */
  16. /*                    has physically changed or moved.  Finally, the first    */
  17. /*                    and last entries in the palette cannot be changed        */
  18. /*                    and are always defined to black and white.  This is        */
  19. /*                    done because ActivatePalette will only update 14        */
  20. /*                    non-b/w Tolerant colors.  The remaining 2 colors        */
  21. /*                    if defined as b/w will be updated, otherwise QD will    */
  22. /*                    return the color in the palette which is the closest    */
  23. /*                    match to the RGB values of these entries.                */    
  24. /*                                                                            */
  25. /*    File:            Color Picker.c                                            */
  26. /*                                                                            */
  27. /*    Programmer:        Edgar Lee                                                */
  28. /*    Organization:    Apple Computer, Inc.                                    */
  29. /*    Department:        Developer Technical Support, DTS                        */
  30. /*    Language:        C (Think C version 4.0.4)                                */
  31. /*    Date Created:    10-02-91                                                */
  32. /*                                                                            */
  33. /*  Converted to MPW & Universal Interfaces 11-11-94
  34. /****************************************************************************/
  35.  
  36. #include <AppleEvents.h>
  37. #include <Errors.h>
  38. #include <Events.h>
  39. #include <Fonts.h>
  40. #include <GestaltEqu.h>
  41. #include <Memory.h>
  42. #include <Menus.h>
  43. #include <OSUtils.h>
  44. #include <QDOffscreen.h>
  45. #include <QuickDraw.h>
  46. #include <Resources.h>
  47. #include <Script.h>
  48. #include <ToolUtils.h>
  49. #include <Windows.h>
  50. #include <Palettes.h>
  51. #include <TextEdit.h>
  52. #include <Dialogs.h>
  53. #include <Picker.h>
  54.  
  55. /* Constant Declarations */
  56.  
  57. #define    TOTALCOLORS    16
  58.  
  59. #define    BOXSIZE        75
  60. #define    WWIDTH        ((TOTALCOLORS / 4) * BOXSIZE)
  61. #define    WHEIGHT        ((TOTALCOLORS / 4) * BOXSIZE )
  62.  
  63. #define WLEFT        (((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - WWIDTH) / 2)
  64. #define WTOP        (((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - WHEIGHT) / 2)
  65.  
  66. /* Global Variable Definitions */
  67.  
  68. WindowPtr            gWindow;
  69. PaletteHandle        gPalette;
  70.  
  71. void initMac();
  72. void createWindow();
  73. void createPalette();
  74. void drawImage();
  75.  
  76. void doEventLoop();
  77. void doInContent();
  78.  
  79. #ifdef powerc
  80.    QDGlobals    qd;
  81. #endif
  82.  
  83.  
  84. main()
  85. {
  86.     initMac();
  87.     
  88.     createWindow();
  89.     createPalette();
  90.     drawImage();
  91.  
  92.     doEventLoop();
  93.  
  94.     DisposeWindow( gWindow );
  95. }
  96.  
  97.  
  98.  
  99. void initMac()
  100. {
  101.     MaxApplZone();
  102.  
  103.     InitGraf( &qd.thePort );
  104.     InitFonts();
  105.     InitWindows();
  106.     InitMenus();
  107.     TEInit();
  108.     InitDialogs( nil );
  109.     InitCursor();
  110.     FlushEvents( 0, everyEvent );
  111. }
  112.  
  113. void createWindow()
  114. {
  115.     Rect wBounds;
  116.     
  117.     /* Create a window to display the image. */
  118.     
  119.     SetRect( &wBounds, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
  120.     
  121.     gWindow = NewCWindow( 0L, &wBounds, "\pColor Picker Test", true, documentProc,
  122.                             (WindowPtr)-1L, true, 0L );
  123.                             
  124.     ShowWindow( gWindow );
  125.     SetPort( gWindow );
  126. }
  127.  
  128. void createPalette()
  129. {
  130.     int    index;
  131.     RGBColor aColor;
  132.  
  133.     /* Create the palette of size TOTALCOLORS. */
  134.     
  135.     gPalette = NewPalette( TOTALCOLORS, nil, pmTolerant, 0 );
  136.     
  137.     /* Assign a color to the first 15 palette entries. */
  138.  
  139.     for (index = 0; index < (TOTALCOLORS - 1); index++)
  140.     {
  141.         aColor.blue = index * (0xffff / (TOTALCOLORS - 2));
  142.         aColor.red = aColor.green = 0;
  143.         SetEntryColor( gPalette, index, &aColor );
  144.     }
  145.     
  146.     /* Set the last entry to white. */
  147.     
  148.     aColor.red = aColor.green = aColor.blue = 0xffff;
  149.     SetEntryColor( gPalette, (TOTALCOLORS - 1), &aColor );
  150.  
  151.     /* Attach the new palette to the main window. */
  152.     
  153.     SetPalette( gWindow, gPalette, true );
  154. }
  155.  
  156. void drawImage()
  157. {
  158.     int            i;
  159.     int            x, y;
  160.     Rect        rect;
  161.     RGBColor    aColor;
  162.  
  163.     /* Draw a grid of colors to represent each color entry in the palette. */
  164.  
  165.     for (i = 0; i < TOTALCOLORS; i++)
  166.     {
  167.         x = (i % 4) * BOXSIZE;
  168.         y = (i / 4) * BOXSIZE;
  169.         
  170.         GetEntryColor( gPalette, i, &aColor );
  171.         RGBForeColor( &aColor );
  172.         
  173.         SetRect( &rect, x, y, x + BOXSIZE, y + BOXSIZE );
  174.         PaintRect( &rect );
  175.     }
  176. }
  177.  
  178. void doInContent( thePoint )
  179. Point thePoint;
  180. {
  181.     int            paletteIndex = -1;
  182.     Point        where;
  183.     RGBColor    currentColor, newColor;
  184.     Rect        rect;
  185.  
  186.     /* Get the palette entry index for the box drawn at the mouse click. */
  187.     
  188.     paletteIndex = ((thePoint.v / BOXSIZE) * 4) + (thePoint.h / BOXSIZE);
  189.     
  190.     /* If the paletteIndex isn't the first or last entry, do the following. */
  191.  
  192.     if (paletteIndex > 0 && paletteIndex < (TOTALCOLORS - 1))
  193.     {
  194.         /* Invert the selected box then beep the Mac. */
  195.     
  196.         SetRect( &rect, (paletteIndex % 4) * BOXSIZE,
  197.                     (paletteIndex / 4) * BOXSIZE,
  198.                     ((paletteIndex % 4) * BOXSIZE) + BOXSIZE,
  199.                     ((paletteIndex / 4) * BOXSIZE) + BOXSIZE );
  200.         InvertRect( &rect );
  201.         SysBeep( 1 );
  202.     
  203.         /* Get the RGB values for the color stored at this palette index. */
  204.         
  205.         GetEntryColor( gPalette, paletteIndex, ¤tColor );
  206.         
  207.         /* Open the color picker dialog to select new RGB values. */
  208.         
  209.         where.h = where.v = -1;
  210.  
  211.         if (GetColor( where, "\pSelect a new palette color.", ¤tColor, &newColor ))
  212.         {
  213.             /* Assign the new RGB values to this entry. */
  214.             
  215.             SetEntryColor( gPalette, paletteIndex, &newColor );
  216.             
  217.             /* Update the palette with the new colors. */
  218.             
  219.             ActivatePalette( gWindow );
  220.             
  221.             /* Redraw the image with the new palette colors. */
  222.         
  223.             drawImage();
  224.         }
  225.         else
  226.         {
  227.             /* Invert the rect back to its original state on a Cancel. */
  228.             
  229.             InvertRect( &rect );
  230.         }
  231.     }
  232. }
  233.  
  234. void doEventLoop()
  235. {
  236.     EventRecord anEvent;
  237.     WindowPtr   evtWind;
  238.     short       clickArea;
  239.     Rect        screenRect;
  240.     Point        thePoint;
  241.  
  242.     for (;;)
  243.     {
  244.         if (WaitNextEvent( everyEvent, &anEvent, 0, nil ))
  245.         {
  246.             if (anEvent.what == mouseDown)
  247.             {
  248.                 clickArea = FindWindow( anEvent.where, &evtWind );
  249.                 
  250.                 if (clickArea == inDrag)
  251.                 {
  252.                     screenRect = (**GetGrayRgn ()).rgnBBox;
  253.                     DragWindow( evtWind, anEvent.where, &screenRect );
  254.                 }
  255.                 else if (clickArea == inContent)
  256.                 {
  257.                     if (evtWind != FrontWindow())
  258.                         SelectWindow( evtWind );
  259.                     else
  260.                     {
  261.                         thePoint = anEvent.where;
  262.                         GlobalToLocal( &thePoint );
  263.                         doInContent( thePoint );
  264.                     }
  265.                 }
  266.                 else if (clickArea == inGoAway)
  267.                     if (TrackGoAway( evtWind, anEvent.where ))
  268.                         return;
  269.             }
  270.             else if (anEvent.what == updateEvt)
  271.             {
  272.                 evtWind = (WindowPtr)anEvent.message;    
  273.                 SetPort( evtWind );
  274.                 
  275.                 BeginUpdate( evtWind );
  276.                 drawImage();
  277.                 EndUpdate (evtWind);
  278.             }
  279.         }
  280.     }
  281. }