home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / msysjour / vol03 / 04 / palette / palette.c < prev    next >
C/C++ Source or Header  |  1988-04-13  |  22KB  |  814 lines

  1. /*
  2.  * WINDOWS COLOR PALETTE UTILITY - SOURCE
  3.  *
  4.  * LANGUAGE : Microsoft C 5.0
  5.  * TOOLKIT  : Windows 2.03 SDK
  6.  * MODEL    : Small
  7.  * STATUS   : Operational
  8.  *
  9.  * 03/20/88 - Kevin P. Welch - initial creation.
  10.  *
  11.  */
  12.  
  13. #include <windows.h>
  14. #include <math.h>
  15. #include "spectrum.h"
  16. #include "palette.h"
  17.  
  18. /* spectrum values */
  19. WORD        wModel;
  20. RGB        rgbColor;
  21. CMY        cmyColor;
  22. HSV        hsvColor;
  23.  
  24. /* default RGB colors */
  25. LONG        rgbRed[32] = {                
  26.                     0x00000000,0x00000011,0x00000022,0x00000033,
  27.                     0x00000044,0x00000055,0x00000066,0x00000077,
  28.                     0x00000088,0x00000099,0x000000aa,0x000000bb,
  29.                     0x000000cc,0x000000dd,0x000000ee,0x000000ff,
  30.                     0x00000000,0x3d888889,0x3e088889,0x3e4ccccd,
  31.                     0x3e888889,0x3eaaaaab,0x3ecccccd,0x3eeeeeef,
  32.                     0x3f088889,0x3f19999a,0x3f2aaaab,0x3f3bbbbc,
  33.                     0x3f4ccccd,0x3f5dddde,0x3f6eeeef,0x3f800000
  34.                 };
  35.  
  36. LONG        rgbGreen[32] = {    
  37.                     0x00000000,0x00001100,0x00002200,0x00003300,
  38.                     0x00004400,0x00005500,0x00006600,0x00007700,
  39.                     0x00008800,0x00009900,0x0000aa00,0x0000bb00,
  40.                     0x0000cc00,0x0000dd00,0x0000ee00,0x0000ff00,
  41.                     0x00000000,0x3d888889,0x3e088889,0x3e4ccccd,
  42.                     0x3e888889,0x3eaaaaab,0x3ecccccd,0x3eeeeeef,
  43.                     0x3f088889,0x3f19999a,0x3f2aaaab,0x3f3bbbbc,
  44.                     0x3f4ccccd,0x3f5dddde,0x3f6eeeef,0x3f800000
  45.                 };
  46.                 
  47. LONG        rgbBlue[32] = {
  48.                     0x00000000,0x00110000,0x00220000,0x00330000,
  49.                     0x00440000,0x00550000,0x00660000,0x00770000,
  50.                     0x00880000,0x00990000,0x00aa0000,0x00bb0000,
  51.                     0x00cc0000,0x00dd0000,0x00ee0000,0x00ff0000,
  52.                     0x00000000,0x3d888889,0x3e088889,0x3e4ccccd,
  53.                     0x3e888889,0x3eaaaaab,0x3ecccccd,0x3eeeeeef,
  54.                     0x3f088889,0x3f19999a,0x3f2aaaab,0x3f3bbbbc,
  55.                     0x3f4ccccd,0x3f5dddde,0x3f6eeeef,0x3f800000
  56.                 };
  57.  
  58. /* default CMY colors */
  59. LONG        cmyCyan[32] = {
  60.                     0x00ffffff,0x00ffffee,0x00ffffdd,0x00ffffcc,
  61.                     0x00ffffbb,0x00ffffa9,0x00ffff99,0x00ffff87,
  62.                     0x00ffff76,0x00ffff65,0x00ffff54,0x00ffff43,
  63.                     0x00ffff32,0x00ffff21,0x00ffff10,0x00ffff00,
  64.                     0x00000000,0x3d888889,0x3e088889,0x3e4ccccd,
  65.                     0x3e888889,0x3eaaaaab,0x3ecccccd,0x3eeeeeef,
  66.                     0x3f088889,0x3f19999a,0x3f2aaaab,0x3f3bbbbc,
  67.                     0x3f4ccccd,0x3f5dddde,0x3f6eeeef,0x3f800000
  68.                 };
  69.                 
  70. LONG        cmyMagenta[32] = {
  71.                     0x00ffffff,0x00ffeeff,0x00ffddff,0x00ffccff,
  72.                     0x00ffbbff,0x00ffa9ff,0x00ff99ff,0x00ff87ff,
  73.                     0x00ff76ff,0x00ff65ff,0x00ff54ff,0x00ff43ff,
  74.                     0x00ff32ff,0x00ff21ff,0x00ff10ff,0x00ff00ff,
  75.                     0x00000000,0x3d888889,0x3e088889,0x3e4ccccd,
  76.                     0x3e888889,0x3eaaaaab,0x3ecccccd,0x3eeeeeef,
  77.                     0x3f088889,0x3f19999a,0x3f2aaaab,0x3f3bbbbc,
  78.                     0x3f4ccccd,0x3f5dddde,0x3f6eeeef,0x3f800000
  79.                 };
  80.                 
  81. LONG        cmyYellow[32] = {
  82.                     0x00ffffff,0x00eeffff,0x00ddffff,0x00ccffff,
  83.                     0x00bbffff,0x00a9ffff,0x0099ffff,0x0087ffff,
  84.                     0x0076ffff,0x0065ffff,0x0054ffff,0x0043ffff,
  85.                     0x0032ffff,0x0021ffff,0x0010ffff,0x0000ffff,
  86.                     0x00000000,0x3d888889,0x3e088889,0x3e4ccccd,
  87.                     0x3e888889,0x3eaaaaab,0x3ecccccd,0x3eeeeeef,
  88.                     0x3f088889,0x3f19999a,0x3f2aaaab,0x3f3bbbbc,
  89.                     0x3f4ccccd,0x3f5dddde,0x3f6eeeef,0x3f800000
  90.                 };
  91.  
  92. /* default HSV colors */
  93. LONG        hsvHue[32] = {
  94.                     0x000000ff,0x000055ff,0x0000aaff,0x0000ffff,
  95.                     0x0000ffa9,0x0000ff55,0x0000ff00,0x0054ff00,
  96.                     0x00aaff00,0x00ffff00,0x00ffaa00,0x00ff5400,
  97.                     0x00ff0000,0x00ff0055,0x00ff00ff,0x005500ff,
  98.                     0x00000000,0x41a00000,0x42200000,0x42700000,
  99.                     0x42a00000,0x42c80000,0x42f00000,0x430c0000,
  100.                     0x43200000,0x43340000,0x43480000,0x435c0000,
  101.                     0x43700000,0x43820000,0x43960000,0x43aa0000
  102.                 };
  103.                 
  104. LONG        hsvSaturation[32] = {
  105.                     0x00ffffff,0x00ffeeee,0x00ffdddd,0x00ffcccc,
  106.                     0x00ffbbbb,0x00ffa9a9,0x00ff9999,0x00ff8787,
  107.                     0x00ff7676,0x00ff6565,0x00ff5454,0x00ff4343,
  108.                     0x00ff3232,0x00ff2121,0x00ff1010,0x00ff0000,
  109.                     0x00000000,0x3d888889,0x3e088889,0x3e4ccccd,
  110.                     0x3e888889,0x3eaaaaab,0x3ecccccd,0x3eeeeeef,
  111.                     0x3f088889,0x3f19999a,0x3f2aaaab,0x3f3bbbbc,
  112.                     0x3f4ccccd,0x3f5dddde,0x3f6eeeef,0x3f800000
  113.                 };
  114.                 
  115. LONG        hsvValue[32] = {
  116.                     0x00000000,0x00111111,0x00222222,0x00333333,
  117.                     0x00444444,0x00555555,0x00666666,0x00777777,
  118.                     0x00888888,0x00999999,0x00aaaaaa,0x00bbbbbb,
  119.                     0x00cccccc,0x00dddddd,0x00eeeeee,0x00ffffff,
  120.                     0x00000000,0x3d888889,0x3e088889,0x3e4ccccd,
  121.                     0x3e888889,0x3eaaaaab,0x3ecccccd,0x3eeeeeef,
  122.                     0x3f088889,0x3f19999a,0x3f2aaaab,0x3f3bbbbc,
  123.                     0x3f4ccccd,0x3f5dddde,0x3f6eeeef,0x3f800000
  124.                 };
  125.  
  126. /* */
  127.  
  128. /*
  129.  * WinMain( hInst, hPrevInst, lpszCmdLine, wCmdShow ) : int
  130.  *
  131.  *        hInst                handle of current instance
  132.  *        hPrevInst        handle to previous instance (if any)
  133.  *        lpszCmdLine        pointer to command line arguments
  134.  *        wCmdShow            initial ShowWindow command
  135.  *
  136.  *    This function is the system entry point for the application
  137.  * and is responsible for defining the appropriate window 
  138.  * classes and for processing all the messages.  Note how
  139.  * the dialog box manager is responsible for the operation of
  140.  * the palette window.
  141.  *
  142.  */
  143.  
  144. int PASCAL WinMain( hInst, hPrevInst, lpszCmdLine, wCmdShow )
  145.     HANDLE            hInst;
  146.     HANDLE            hPrevInst;
  147.     LPSTR                lpszCmdLine;
  148.     WORD                wCmdShow;
  149. {
  150.     /* local variables */
  151.     FARPROC            lpProc;                        /* temporary function */
  152.  
  153.     /* register window if first instance */
  154.     if ( hPrevInst || RegisterSpectrum(hInst) ) {
  155.             
  156.         /* display palette dialog box */
  157.         lpProc = MakeProcInstance( (FARPROC)PaletteDlgFn, hInst );
  158.          DialogBox( hInst, "Palette", NULL, lpProc );
  159.          FreeProcInstance( lpProc );
  160.         
  161.     } 
  162.             
  163.     /* end program */
  164.     return( FALSE );
  165.  
  166. }
  167.  
  168. /* */
  169.  
  170. /*
  171.  * PaletteDlgFn( hWnd, wMsg, wParam, lParam ) : BOOL 
  172.  *
  173.  *        hWnd                    handle to palette window
  174.  *        wMsg                    message number
  175.  *        wParam                single word parameter
  176.  *        lParam                double word parameter
  177.  *
  178.  * This function is responsible for processing all the messages
  179.  * which relate to the color palette dialog box.  This mainly
  180.  * involves the definition and retrieval of the various
  181.  * colors selected by the user.
  182.  *
  183.  */
  184.  
  185. BOOL FAR PASCAL PaletteDlgFn( hDlg, wMsg, wParam, lParam )
  186.     HWND             hDlg;
  187.     WORD             wMsg;
  188.     WORD             wParam;
  189.     LONG             lParam;
  190. {
  191.      /* local variables */
  192.      BOOL            bResult;                            /* result of function */
  193.  
  194.    /* initialization */
  195.    bResult = TRUE;
  196.    
  197.    /* process messages */
  198.    switch( wMsg )
  199.        {
  200.    case WM_INITDIALOG : /* initialize dialog box */
  201.     
  202.         /* select crt mixing model */
  203.         wModel = ID_RGB;
  204.         SelectMixingModel( hDlg, wModel );
  205.         
  206.         /* define icon for dialog box */
  207.           SetClassWord( 
  208.               hDlg, 
  209.               GCW_HICON, 
  210.               LoadIcon( INSTANCE, (LPSTR)"PaletteIcon" )
  211.           );
  212.                       
  213.           break;
  214.    case WM_SYSCOMMAND : /* system command */
  215.         
  216.         /* process sub-messages */
  217.         switch( wParam )
  218.             {
  219.         case SC_CLOSE : /* destroy dialog box */
  220.                EndDialog( hDlg, TRUE );
  221.             break;
  222.         default :
  223.             bResult = FALSE;
  224.             break;
  225.         }
  226.         
  227.         break;
  228.     case WM_COMMAND : /* window command */
  229.        
  230.        /* process sub-message */
  231.        switch( wParam )
  232.            {
  233.        case ID_RGB : /* RGB mixing model */
  234.        case ID_CMY : /* CMY mixing model */
  235.        case ID_HSV : /* HSV mixing model */
  236.             SelectMixingModel( hDlg, wParam );
  237.               break;
  238.        case ID_SPECTRUM1 : /* 1st spectrum */
  239.  
  240.            switch( wModel )
  241.                {
  242.            case ID_RGB :
  243.                rgbColor.fRed = *((float*)&lParam);
  244.                RGBtoCMY( &rgbColor, &cmyColor );
  245.                RGBtoHSV( &rgbColor, &hsvColor );
  246.                 DlgPrintf( hDlg, ID_VALUE1, "%.3f", rgbColor.fRed );
  247.                break;
  248.            case ID_CMY :
  249.                cmyColor.fCyan = *((float*)&lParam);
  250.                CMYtoRGB( &cmyColor, &rgbColor );
  251.                RGBtoHSV( &rgbColor, &hsvColor );
  252.                 DlgPrintf( hDlg, ID_VALUE1, "%.3f", cmyColor.fCyan );
  253.                break;
  254.            case ID_HSV :
  255.                hsvColor.fHue = *((float*)&lParam);
  256.                HSVtoRGB( &hsvColor, &rgbColor );
  257.                RGBtoCMY( &rgbColor, &cmyColor ); 
  258.                 DlgPrintf( hDlg, ID_VALUE1, "%.0f", hsvColor.fHue );
  259.                break;
  260.            }
  261.            InvalidateRect( GetDlgItem(hDlg,ID_SAMPLE), NULL, NULL );
  262.  
  263.            break;
  264.        case ID_SPECTRUM2 : /* 2nd spectrum */
  265.  
  266.            switch( wModel )
  267.                {
  268.            case ID_RGB :
  269.                rgbColor.fGreen = *((float*)&lParam);
  270.                RGBtoCMY( &rgbColor, &cmyColor );
  271.                RGBtoHSV( &rgbColor, &hsvColor );
  272.                 DlgPrintf( hDlg, ID_VALUE2, "%.3f", rgbColor.fGreen );
  273.                break;
  274.            case ID_CMY :
  275.                cmyColor.fMagenta = *((float*)&lParam);
  276.                CMYtoRGB( &cmyColor, &rgbColor );
  277.                RGBtoHSV( &rgbColor, &hsvColor );
  278.                 DlgPrintf( hDlg, ID_VALUE2, "%.3f", cmyColor.fMagenta );
  279.                break;
  280.            case ID_HSV :
  281.                hsvColor.fSaturation = *((float*)&lParam);
  282.                HSVtoRGB( &hsvColor, &rgbColor );
  283.                RGBtoCMY( &rgbColor, &cmyColor ); 
  284.                 DlgPrintf( hDlg, ID_VALUE2, "%.3f", hsvColor.fSaturation );
  285.                break;
  286.            }
  287.            InvalidateRect( GetDlgItem(hDlg,ID_SAMPLE), NULL, NULL );
  288.  
  289.            break;
  290.        case ID_SPECTRUM3 : /* 3rd spectrum */
  291.  
  292.            switch( wModel )
  293.                {
  294.            case ID_RGB :
  295.                rgbColor.fBlue = *((float*)&lParam);
  296.                RGBtoCMY( &rgbColor, &cmyColor );
  297.                RGBtoHSV( &rgbColor, &hsvColor );
  298.                 DlgPrintf( hDlg, ID_VALUE3, "%.3f", rgbColor.fBlue );
  299.                break;
  300.            case ID_CMY :
  301.                cmyColor.fYellow = *((float*)&lParam);
  302.                CMYtoRGB( &cmyColor, &rgbColor );
  303.                RGBtoHSV( &rgbColor, &hsvColor );
  304.                 DlgPrintf( hDlg, ID_VALUE3, "%.3f", cmyColor.fYellow );
  305.                break;
  306.            case ID_HSV :
  307.                hsvColor.fValue = *((float*)&lParam);
  308.                HSVtoRGB( &hsvColor, &rgbColor );
  309.                RGBtoCMY( &rgbColor, &cmyColor ); 
  310.                 DlgPrintf( hDlg, ID_VALUE3, "%.3f", hsvColor.fValue );
  311.                break;
  312.            }
  313.            InvalidateRect( GetDlgItem(hDlg,ID_SAMPLE), NULL, NULL );
  314.  
  315.            break;
  316.        case ID_SAMPLE : /* sample color patch */
  317.         
  318.             /* update sample only if necessary */
  319.               if ( HIWORD(lParam) == BN_PAINT ) {
  320.                                         
  321.                 /* local variables */
  322.                 HDC            hDC;
  323.                 CRT            crtValue;
  324.                 HANDLE        hOldBrush;
  325.                 RECT            rectClient;
  326.                                                             
  327.                 /* display crt equivalent numerical values */
  328.                 RGBtoCRT( &rgbColor, &crtValue );
  329.                 DlgPrintf( hDlg, ID_RED, "RED   %u", crtValue.cRed );
  330.                 DlgPrintf( hDlg, ID_GREEN, "GREEN %u", crtValue.cGreen );
  331.                 DlgPrintf( hDlg, ID_BLUE, "BLUE  %u", crtValue.cBlue );
  332.                     
  333.                 /* paint color sample */
  334.                hDC = GetDC( LOWORD(lParam) );
  335.                GetClientRect( LOWORD(lParam), &rectClient );
  336.  
  337.                 /* setup display context */
  338.                hOldBrush = SelectObject(
  339.                        hDC,
  340.                        CreateSolidBrush( *((DWORD*)&crtValue) )
  341.                    );
  342.                    
  343.                /* draw color sample */
  344.                Rectangle(
  345.                    hDC,
  346.                    rectClient.left,
  347.                   rectClient.top,
  348.                    rectClient.right,
  349.                    rectClient.bottom
  350.                );
  351.  
  352.                 /* cleanup & release display context */
  353.                DeleteObject( SelectObject(hDC,hOldBrush) );
  354.                ReleaseDC( LOWORD(lParam), hDC );
  355.  
  356.            }
  357.  
  358.            break;
  359.        default : /* something else */
  360.            break;
  361.        }
  362.        
  363.        break;
  364.     case WM_NCLBUTTONDBLCLK : /* non-client double click */
  365.         EndDialog( hDlg, TRUE );
  366.         break;
  367.    default :
  368.        bResult = FALSE;
  369.        break;
  370.    }    
  371.    
  372.    /* return result */
  373.    return( bResult );
  374.  
  375. }
  376.  
  377. /* */
  378.  
  379. /*
  380.  * SelectMixingModel( hDlg, wNewModel )
  381.  *
  382.  *        hDlg            handle to palette dialog box
  383.  *        wNewModel    id of new mixing model
  384.  *
  385.  * This function is responsible for changing the displayed 
  386.  * color mixing model to a new one.  In doing so the color
  387.  * spectrum are changed and an attempt is made to find a
  388.  * match for the currently defined color under the new
  389.  * mixing model.
  390.  *
  391.  */
  392.  
  393. void SelectMixingModel( hDlg, wNewModel )
  394.     HANDLE        hDlg;
  395.     WORD            wNewModel;
  396. {
  397.     /* local variables */
  398.     WORD            wRange;
  399.     WORD            wEntry;
  400.         
  401.     /* define new range & check button */
  402.     wRange = 16;
  403.     wModel = wNewModel;
  404.     CheckRadioButton( hDlg, ID_RGB, ID_HSV, wModel );
  405.             
  406.     /* perform initialization depending on model */
  407.     switch( wNewModel )
  408.         {
  409.     case ID_RGB :
  410.  
  411.         SetDlgItemText( hDlg, ID_TITLE1, "Red.......:" );
  412.         SetDlgItemText( hDlg, ID_TITLE2, "Green.....:" );
  413.         SetDlgItemText( hDlg, ID_TITLE3, "Blue......:" );
  414.  
  415.         wEntry = MatchColor(rgbRed,rgbColor.fRed);
  416.           SetSpectrumColors( SPECTRUM1, &wRange, rgbRed );
  417.         SetSpectrum( SPECTRUM1, &wEntry  );
  418.  
  419.         wEntry = MatchColor(rgbGreen,rgbColor.fGreen);
  420.           SetSpectrumColors( SPECTRUM2, &wRange, rgbGreen );
  421.         SetSpectrum( SPECTRUM2, &wEntry );
  422.  
  423.         wEntry = MatchColor(rgbBlue,rgbColor.fBlue);
  424.           SetSpectrumColors( SPECTRUM3, &wRange, rgbBlue );
  425.         SetSpectrum( SPECTRUM3, &wEntry  );
  426.         
  427.         break;    
  428.     case ID_CMY :
  429.  
  430.         SetDlgItemText( hDlg, ID_TITLE1, "Cyan......:" );
  431.         SetDlgItemText( hDlg, ID_TITLE2, "Magenta...:" );
  432.         SetDlgItemText( hDlg, ID_TITLE3, "Yellow....:" );
  433.  
  434.         wEntry = MatchColor(cmyCyan,cmyColor.fCyan);
  435.           SetSpectrumColors( SPECTRUM1, &wRange, cmyCyan );
  436.         SetSpectrum( SPECTRUM1, &wEntry );
  437.  
  438.         wEntry = MatchColor(cmyMagenta,cmyColor.fMagenta);
  439.           SetSpectrumColors( SPECTRUM2, &wRange, cmyMagenta );
  440.         SetSpectrum( SPECTRUM2, &wEntry );
  441.  
  442.         wEntry = MatchColor(cmyYellow,cmyColor.fYellow);
  443.           SetSpectrumColors( SPECTRUM3, &wRange, cmyYellow );
  444.         SetSpectrum( SPECTRUM3, &wEntry );
  445.         
  446.         break;    
  447.     case ID_HSV :
  448.     
  449.         SetDlgItemText( hDlg, ID_TITLE1, "Hue.......:" );
  450.         SetDlgItemText( hDlg, ID_TITLE2, "Saturation:" );
  451.         SetDlgItemText( hDlg, ID_TITLE3, "Value.....:" );
  452.  
  453.         wEntry = MatchColor(hsvHue,hsvColor.fHue);
  454.           SetSpectrumColors( SPECTRUM1, &wRange, hsvHue );
  455.         SetSpectrum( SPECTRUM1, &wEntry );
  456.  
  457.         wEntry = MatchColor(hsvSaturation,hsvColor.fSaturation);
  458.           SetSpectrumColors( SPECTRUM2, &wRange, hsvSaturation );
  459.         SetSpectrum( SPECTRUM2, &wEntry );
  460.  
  461.         wEntry = MatchColor(hsvValue,hsvColor.fValue);
  462.           SetSpectrumColors( SPECTRUM3, &wRange, hsvValue );
  463.         SetSpectrum( SPECTRUM3, &wEntry );
  464.         
  465.         break;
  466.     }
  467.     
  468. }
  469.  
  470. /* */
  471.  
  472. /*
  473.  * MatchColor( plTable, fValue ) : iEntry
  474.  *
  475.  *        plTable            table of color values
  476.  *        fValue            value to search for
  477.  *
  478.  * This utility function searches a spectrum table for
  479.  * a particular entry using the key value provided.  An
  480.  * index to the closest match is returned for subsequent
  481.  * use when switching color mixing modes.
  482.  *
  483.  * Note that an exact match may not be present (due to the
  484.  * discrete nature of the spectrum table).  As a result 
  485.  * some variations in the resulting color may by visible.
  486.  *
  487.  */
  488.  
  489. static int MatchColor( plTable, fValue )
  490.     LONG *        plTable;
  491.     float            fValue;
  492. {
  493.     int            iEntry;
  494.     int            iMinimum;
  495.     float            fMinimum;
  496.     
  497.     /* intialization */
  498.     iMinimum = 0;
  499.     fMinimum = ABS(*((float*)&plTable[16])-fValue);
  500.  
  501.     /* search for best fit */
  502.     for ( iEntry=16; iEntry<32; iEntry++ ) {
  503.         if ( ABS(*((float*)&plTable[iEntry])-fValue) < fMinimum ) {
  504.             iMinimum = iEntry - 16;
  505.             fMinimum = ABS(*((float*)&plTable[iEntry])-fValue);
  506.         }
  507.     }
  508.     
  509.     /* return best entry */
  510.     return( iMinimum );
  511.     
  512. }
  513.  
  514. /* */
  515.  
  516. /*
  517.  * DlgPrintf( hDlg, wId, szParmList ) : int
  518.  *
  519.  *        hDlg                handle to dialog box
  520.  *        wId                child window id number
  521.  *        szParmList        printf parameter list
  522.  *
  523.  * This function performs a standard printf operation
  524.  * to a text field in a dialog box.  The parameter list
  525.  * is a standard printf one.  The value returned is the
  526.  * number of characters output to the text field.
  527.  *
  528.  */
  529.  
  530. static int DlgPrintf( hDlg, wId, szParmList )
  531.     HANDLE        hDlg;
  532.     WORD            wId;
  533.     PSTR            szParmList;
  534. {
  535.     /* local variables */
  536.     int            iResult;                        
  537.     PSTR            pArguments;                    
  538.     char            szString[132];                
  539.     
  540.     /* define string from parameter list & display in text field */
  541.     pArguments = (char *)&szParmList + sizeof(szParmList);        
  542.     iResult = vsprintf( szString, szParmList, pArguments );
  543.     SetDlgItemText( hDlg, wId, szString );
  544.     
  545.     /* normal return */
  546.     return( iResult );
  547. }
  548.  
  549. /* */
  550.  
  551. /*
  552.  * RGBtoCRT( prgbValue, pcrtValue ) : BOOL
  553.  *
  554.  *        prgbValue        pointer to red-green-blue value
  555.  *        pcrtValue        pointer to CRT red-green-blue color
  556.  * 
  557.  * This function converts the given RGB color value to the
  558.  * corresponding rgb CRT value.  Normal ranges are assumed.
  559.  * A value of TRUE is returned if successful.
  560.  *
  561.  */
  562.  
  563. BOOL RGBtoCRT( prgbValue, pcrtValue )
  564.     RGB *        prgbValue;
  565.     CRT *        pcrtValue;
  566. {
  567.     
  568.     /* convert RGB to CRT */
  569.     pcrtValue->cUnused = 0;
  570.     pcrtValue->cRed = prgbValue->fRed*255.00;
  571.     pcrtValue->cGreen = prgbValue->fGreen*255.00;
  572.     pcrtValue->cBlue = prgbValue->fBlue*255.00;
  573.  
  574.     /* normal return */
  575.     return( TRUE );
  576.     
  577. }
  578.  
  579. /* */
  580.  
  581. /*
  582.  * CRTtoRGB( pcrtValue, prgbValue ) : BOOL
  583.  *
  584.  *        pcrtValue        pointer to CRT red-green-blue color
  585.  *        prgbValue        pointer to red-green-blue value
  586.  * 
  587.  * This function converts the given rgb CRT color value to a
  588.  * corresponding RGB value.  Normal ranges are assumed.  A
  589.  * value of TRUE is returned if successful.
  590.  *
  591.  */
  592.  
  593. BOOL CRTtoRGB( pcrtValue, prgbValue )
  594.     CRT *        pcrtValue;
  595.     RGB *        prgbValue;
  596. {
  597.     
  598.     /* convert CRT to RGB */
  599.     prgbValue->fRed = pcrtValue->cRed / 255.00;
  600.     prgbValue->fGreen = pcrtValue->cGreen / 255.00;
  601.     prgbValue->fBlue = pcrtValue->cBlue / 255.00;
  602.  
  603.     /* normal return */
  604.     return( TRUE );
  605.     
  606. }
  607.  
  608. /* */
  609.  
  610. /*
  611.  * RGBtoCMY( prgbValue, pcmyValue ) : BOOL
  612.  *
  613.  *        prgbValue        pointer to red-green-blue value
  614.  *        pcmyValue        pointer to cyan-magenta-yellow color
  615.  * 
  616.  * This function converts the given RGB color value to the
  617.  * corresponding CMY value.  Normal ranges are assumed.  A
  618.  * value of TRUE is returned if successful.
  619.  *
  620.  */
  621.  
  622. BOOL RGBtoCMY( prgbValue, pcmyValue )
  623.     RGB *        prgbValue;
  624.     CMY *        pcmyValue;
  625. {
  626.     
  627.     /* convert RGB to CMY */
  628.     pcmyValue->fCyan = 1.00 - prgbValue->fRed;
  629.     pcmyValue->fMagenta = 1.00 - prgbValue->fGreen;
  630.     pcmyValue->fYellow = 1.00 - prgbValue->fBlue;
  631.  
  632.     /* normal return */
  633.     return( TRUE );
  634.     
  635. }
  636.  
  637. /* */
  638.  
  639. /*
  640.  * CMYtoRGB( pcmyValue, prgbValue ) : BOOL
  641.  *
  642.  *        pcmyValue        pointer to cyan-magenta-yellow color
  643.  *        prgbValue        pointer to red-green-blue value
  644.  * 
  645.  * This function converts the given CMY color value to a
  646.  * corresponding RGB value.  Normal ranges are assumed.  A
  647.  * value of TRUE is returned if successful.
  648.  *
  649.  */
  650.  
  651. BOOL CMYtoRGB( pcmyValue, prgbValue )
  652.     CMY *        pcmyValue;
  653.     RGB *        prgbValue;
  654. {
  655.     
  656.     /* convert CMY to RGB */
  657.     prgbValue->fRed = 1.00 - pcmyValue->fCyan;
  658.     prgbValue->fGreen = 1.00 - pcmyValue->fMagenta;
  659.     prgbValue->fBlue = 1.00 - pcmyValue->fYellow;
  660.  
  661.     /* normal return */
  662.     return( TRUE );
  663.     
  664. }
  665.  
  666. /* */
  667.  
  668. /*
  669.  * RGBtoHSV( prgbValue, phsvValue ) : BOOL
  670.  *
  671.  *        prgbValue        pointer to red-green-blue value
  672.  *        phsvValue        pointer to hue-saturation-value color
  673.  * 
  674.  * This function converts the given RGB color value to the
  675.  * corresponding HSV value.  Normal ranges are assumed.  A
  676.  * value of TRUE is returned if successful.
  677.  *
  678.  */
  679.  
  680. BOOL RGBtoHSV( prgbValue, phsvValue )
  681.     RGB *            prgbValue;
  682.     HSV *            phsvValue;
  683. {
  684.     /* local variables */
  685.     float            fMin;
  686.     float            fMax;
  687.     float            fRedDist;
  688.     float            fBlueDist;
  689.     float            fGreenDist;
  690.     
  691.     /* calculate minimum & maximum values */
  692.     fMin = MINIMUM( prgbValue->fRed, prgbValue->fGreen, prgbValue->fBlue );
  693.     fMax = MAXIMUM( prgbValue->fRed, prgbValue->fGreen, prgbValue->fBlue );
  694.     
  695.     /* define value & saturation */
  696.     phsvValue->fValue = fMax;
  697.     phsvValue->fSaturation = (fMax) ? (fMax-fMin)/fMax : 0.00;
  698.     
  699.     /* define hue */
  700.     if ( phsvValue->fSaturation > 0.00 ) {
  701.         
  702.         /* calculate related color distances */
  703.         fRedDist = (fMax-prgbValue->fRed)/(fMax-fMin);
  704.         fGreenDist = (fMax-prgbValue->fGreen)/(fMax-fMin);
  705.         fBlueDist = (fMax-prgbValue->fBlue)/(fMax-fMin);
  706.     
  707.         /* compute hue based on distances */
  708.         if ( prgbValue->fRed == fMax )
  709.             phsvValue->fHue = fBlueDist - fGreenDist;
  710.         else
  711.             if ( prgbValue->fGreen == fMax )
  712.                 phsvValue->fHue = 2 + fRedDist - fBlueDist;
  713.             else
  714.                 phsvValue->fHue = 4 + fGreenDist - fRedDist;
  715.         
  716.         /* convert hue to degrees and make non-negative */
  717.         phsvValue->fHue *= 60.00;
  718.         if ( phsvValue->fHue < 0.00 )
  719.             phsvValue->fHue += 360.00;
  720.     
  721.     } else
  722.         phsvValue->fHue = 0.00;
  723.  
  724.     /* normal return */
  725.     return( TRUE );
  726.  
  727. }
  728.  
  729. /* */
  730.  
  731. /*
  732.  * HSVtoRGB( phsvValue, prgbValue ) : BOOL
  733.  *
  734.  *        phsvValue        pointer to hue-saturation-value color
  735.  *        prgbValue        pointer to red-green-blue value
  736.  * 
  737.  * This function converts the given HSV color value to the
  738.  * corresponding RGB value.  Normal ranges are assumed.  A
  739.  * value of TRUE is returned if successful.
  740.  *
  741.  */
  742.  
  743. BOOL HSVtoRGB( phsvValue, prgbValue )
  744.     HSV *            phsvValue;
  745.     RGB *            prgbValue;
  746. {
  747.     /* local variables */
  748.     float            fP;
  749.     float            fQ;
  750.     float            fT;
  751.     float            fHue;
  752.     float            fFract;
  753.     
  754.     /* branch if chromatic */
  755.     if ( phsvValue->fSaturation > 0.00 ) {
  756.  
  757.         /* define adjusted hue */
  758.         fHue = ( phsvValue->fHue >= 360.00 ) ?
  759.             0.00 :
  760.             phsvValue->fHue/60.00;
  761.         
  762.         /* calculate triplet */
  763.         fFract = fHue - floor(fHue);
  764.         
  765.         fP=phsvValue->fValue*(1.0-phsvValue->fSaturation);
  766.         fQ=phsvValue->fValue*(1.0-(phsvValue->fSaturation*fFract));
  767.         fT=phsvValue->fValue*(1.0-(phsvValue->fSaturation*(1.0-fFract)));        
  768.         
  769.         /* triplet assignment */
  770.         switch( (unsigned)floor(fHue) )
  771.             {
  772.         case 0 :
  773.             prgbValue->fRed = phsvValue->fValue;
  774.             prgbValue->fGreen = fT;
  775.             prgbValue->fBlue = fP;
  776.             break;
  777.         case 1 :
  778.             prgbValue->fRed = fQ;
  779.             prgbValue->fGreen = phsvValue->fValue;
  780.             prgbValue->fBlue = fP;
  781.             break;
  782.         case 2 :
  783.             prgbValue->fRed = fP;
  784.             prgbValue->fGreen = phsvValue->fValue;
  785.             prgbValue->fBlue = fT;
  786.             break;
  787.         case 3 :
  788.             prgbValue->fRed = fP;
  789.             prgbValue->fGreen = fQ;
  790.             prgbValue->fBlue = phsvValue->fValue;
  791.             break;
  792.         case 4 :
  793.             prgbValue->fRed = fT;
  794.             prgbValue->fGreen = fP;
  795.             prgbValue->fBlue = phsvValue->fValue;
  796.             break;
  797.         default :
  798.             prgbValue->fRed = phsvValue->fValue;
  799.             prgbValue->fGreen = fP;
  800.             prgbValue->fBlue = fQ;
  801.             break;
  802.         }
  803.     
  804.     } else {    
  805.         prgbValue->fRed = phsvValue->fValue;
  806.         prgbValue->fGreen = phsvValue->fValue;
  807.         prgbValue->fBlue = phsvValue->fValue;
  808.     }
  809.  
  810.     /* normal return */
  811.     return( TRUE );
  812.  
  813. }    
  814.