home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / OS / FWGraphx / SLPalete.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  6.6 KB  |  240 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                SLPalete.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef SLPALETE_H
  13. #include "SLPalete.h"
  14. #endif
  15.  
  16. #ifndef FWDEBUG_H
  17. #include "FWDebug.h"
  18. #endif
  19.  
  20. #if defined(FW_BUILD_MAC) && !defined(__QDOFFSCREEN__)
  21. #include <QDOffscreen.h>
  22. #endif
  23.  
  24. //========================================================================================
  25. //    Runtime informations
  26. //========================================================================================
  27.  
  28. #ifdef FW_BUILD_MAC    
  29. #pragma segment FWGraphix_Palette
  30. #endif
  31.  
  32. //========================================================================================
  33. //    struct SWinPal
  34. //========================================================================================
  35.  
  36. #ifdef FW_BUILD_WIN
  37.  
  38. struct SWinPal
  39. {
  40.     WORD            palVersion;
  41.     WORD            palNumEntries;
  42.     PALETTEENTRY    palPalEntry[256];
  43. };
  44.  
  45. #endif
  46.  
  47. //========================================================================================
  48. //    Palette functions
  49. //========================================================================================
  50.  
  51. //----------------------------------------------------------------------------------------
  52. //    FW_PrivCreatePalette
  53. //----------------------------------------------------------------------------------------
  54.  
  55. FW_Palette SL_API FW_PrivCreatePalette(const FW_SColor*    colors,
  56.                                         short nColorCount)
  57. {
  58.     // returns NULL if fails -  No Try block necessary
  59.     FW_ASSERT(nColorCount <= 256);
  60.  
  61. #ifdef FW_BUILD_WIN
  62.     SWinPal logpal;
  63.     logpal.palVersion = 0x300;
  64.     logpal.palNumEntries = nColorCount;
  65.  
  66.     for (short i = 0; i < nColorCount; ++ i)
  67.     {
  68.         logpal.palPalEntry[i].peRed    = FW_RGB_R(colors[i]);
  69.         logpal.palPalEntry[i].peGreen    = FW_RGB_G(colors[i]);
  70.         logpal.palPalEntry[i].peBlue    = FW_RGB_B(colors[i]);
  71.         logpal.palPalEntry[i].peFlags    = 0;
  72.     }
  73.  
  74.     return ::CreatePalette((LOGPALETTE*)&logpal);
  75. #endif
  76. #ifdef FW_BUILD_MAC
  77.     Size colorTableSize = sizeof(ColorTable) + sizeof(ColorSpec) * (nColorCount - 1);
  78.     CTabHandle colorTableHandle = (CTabHandle) NewHandle(colorTableSize);
  79.     if (colorTableHandle != 0)
  80.     {
  81.         HLock((Handle)colorTableHandle);
  82.         CTabPtr colorTablePtr = *colorTableHandle;
  83.     
  84.         colorTablePtr->ctSeed = 0;
  85.         colorTablePtr->ctFlags = 0;
  86.         colorTablePtr->ctSize = nColorCount - 1;
  87.     
  88.         for (short i = 0; i < nColorCount; ++ i)
  89.         {
  90.             colorTablePtr->ctTable[i].value    = i;
  91.             FW_MacColorToRGB(colors[i], &colorTablePtr->ctTable[i].rgb);
  92.         }
  93.     
  94.         HUnlock((Handle)colorTableHandle);
  95.         ::CTabChanged(colorTableHandle);
  96.     }
  97.     return colorTableHandle;
  98. #endif
  99. }
  100.  
  101. //----------------------------------------------------------------------------------------
  102. //    FW_GetPaletteSize
  103. //----------------------------------------------------------------------------------------
  104.  
  105. short SL_API FW_GetPaletteSize(FW_Palette palette)
  106. {
  107.     // No try block necessary - do not throw
  108.     FW_ASSERT(palette != NULL);
  109.  
  110. #ifdef FW_BUILD_WIN
  111.     WORD wCount;
  112.     ::GetObject(palette, sizeof(WORD), &wCount);
  113.     return wCount;
  114. #endif
  115. #ifdef FW_BUILD_MAC
  116.     return (*palette)->ctSize + 1;
  117. #endif
  118. }
  119.  
  120. //----------------------------------------------------------------------------------------
  121. //    FW_SetPaletteEntries
  122. //----------------------------------------------------------------------------------------
  123.  
  124. void SL_API FW_SetPaletteEntries(FW_Palette            palette,
  125.                                 const FW_SColor*    colors,
  126.                                 short                nStartIndex,
  127.                                 short                nColorCount)
  128. {
  129.     // No try block necessary - do not throw
  130.     FW_ASSERT(palette != NULL);
  131.     FW_ASSERT(nStartIndex >= 0);
  132.     FW_ASSERT(nStartIndex + nColorCount <= FW_GetPaletteSize(palette));
  133.  
  134. #ifdef FW_BUILD_WIN
  135.     PALETTEENTRY palEntries[256];
  136.     
  137.     for (short i = 0; i < nColorCount; ++i, ++colors)
  138.     {
  139.         palEntries[i].peRed        = FW_RGB_R(*colors);
  140.         palEntries[i].peGreen    = FW_RGB_G(*colors);
  141.         palEntries[i].peBlue    = FW_RGB_B(*colors);
  142.         palEntries[i].peFlags    = 0;
  143.     }
  144.  
  145.     ::SetPaletteEntries(palette, nStartIndex, nColorCount, palEntries);
  146. #endif
  147. #ifdef FW_BUILD_MAC
  148.     HLock((Handle)palette);
  149.     CTabPtr colorTablePtr = *palette;
  150.     for (short i = nStartIndex; i < nStartIndex + nColorCount; ++ i, ++ colors)
  151.     {
  152.         colorTablePtr->ctTable[i].value = i;
  153.         FW_MacColorToRGB(*colors, &colorTablePtr->ctTable[i].rgb);
  154.     }
  155.     HUnlock((Handle)palette);
  156.  
  157.     ::CTabChanged(palette);
  158. #endif
  159. }
  160.  
  161. //----------------------------------------------------------------------------------------
  162. //    FW_GetPaletteEntries
  163. //----------------------------------------------------------------------------------------
  164.  
  165. void SL_API FW_GetPaletteEntries(FW_Palette        palette,
  166.                                 FW_SColor*        colors,
  167.                                 short            nStartIndex,
  168.                                 short            nColorCount)
  169. {
  170.     // No try block necessary - do not throw
  171.     FW_ASSERT(palette != NULL);
  172.     FW_ASSERT(nStartIndex >= 0);
  173.     FW_ASSERT(nStartIndex + nColorCount <= FW_GetPaletteSize(palette));
  174.  
  175. #ifdef FW_BUILD_WIN
  176.     PALETTEENTRY palEntries[256];
  177.     
  178.     ::GetPaletteEntries(palette, nStartIndex, nColorCount, palEntries);
  179.  
  180.     for (short i = 0; i < nColorCount; ++i, ++colors)
  181.     {
  182.         FW_SetRGB(*colors, palEntries[i].peRed, palEntries[i].peGreen, palEntries[i].peBlue);
  183.     }
  184. #endif
  185. #ifdef FW_BUILD_MAC
  186.     HLock((Handle)palette);
  187.     CTabPtr colorTablePtr = *palette;
  188.     for (short i = nStartIndex; i < nStartIndex + nColorCount; ++ i, ++ colors)
  189.     {
  190.         *colors = FW_MacRGBToColor(&colorTablePtr->ctTable[i].rgb);
  191.     }
  192.     HUnlock((Handle)palette);
  193. #endif
  194. }
  195.  
  196. //----------------------------------------------------------------------------------------
  197. //    FW_PrivCopyPalette
  198. //----------------------------------------------------------------------------------------
  199.  
  200. FW_Palette SL_API FW_PrivCopyPalette(FW_Palette palette)
  201. {
  202.     // No try block necessary - returns NULL if fails
  203.     FW_ASSERT(palette != NULL);
  204.  
  205. #ifdef FW_BUILD_WIN
  206.     SWinPal logpal;
  207.     logpal.palVersion = 0x300;
  208.     ::GetObject(palette, sizeof(WORD), &logpal.palNumEntries);
  209.  
  210.     ::GetPaletteEntries(palette, 0, logpal.palNumEntries, logpal.palPalEntry);
  211.  
  212.     return ::CreatePalette((LOGPALETTE*)&logpal);
  213. #endif
  214. #ifdef FW_BUILD_MAC
  215.     FW_Palette palCopy = palette;
  216.     HandToHand((Handle*)&palCopy);
  217.     if (palCopy != NULL)
  218.     {
  219.         (*palCopy)->ctSeed = 0;
  220.         ::CTabChanged(palCopy);
  221.     }
  222.     return palCopy;
  223. #endif
  224. }
  225.  
  226. //----------------------------------------------------------------------------------------
  227. //    FW_DestroyPalette
  228. //----------------------------------------------------------------------------------------
  229.  
  230. void SL_API FW_DestroyPalette(FW_Palette palette)
  231. {
  232.     // No try block necessary - do not throw
  233. #ifdef FW_BUILD_WIN
  234.     ::DeleteObject(palette);
  235. #endif
  236. #ifdef FW_BUILD_MAC
  237.     ::DisposeCTable(palette);
  238. #endif
  239. }
  240.