home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-17 | 6.6 KB | 240 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: SLPalete.cpp
- // Release Version: $ ODF 2 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWOS.hpp"
-
- #ifndef SLPALETE_H
- #include "SLPalete.h"
- #endif
-
- #ifndef FWDEBUG_H
- #include "FWDebug.h"
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__QDOFFSCREEN__)
- #include <QDOffscreen.h>
- #endif
-
- //========================================================================================
- // Runtime informations
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- #pragma segment FWGraphix_Palette
- #endif
-
- //========================================================================================
- // struct SWinPal
- //========================================================================================
-
- #ifdef FW_BUILD_WIN
-
- struct SWinPal
- {
- WORD palVersion;
- WORD palNumEntries;
- PALETTEENTRY palPalEntry[256];
- };
-
- #endif
-
- //========================================================================================
- // Palette functions
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_PrivCreatePalette
- //----------------------------------------------------------------------------------------
-
- FW_Palette SL_API FW_PrivCreatePalette(const FW_SColor* colors,
- short nColorCount)
- {
- // returns NULL if fails - No Try block necessary
- FW_ASSERT(nColorCount <= 256);
-
- #ifdef FW_BUILD_WIN
- SWinPal logpal;
- logpal.palVersion = 0x300;
- logpal.palNumEntries = nColorCount;
-
- for (short i = 0; i < nColorCount; ++ i)
- {
- logpal.palPalEntry[i].peRed = FW_RGB_R(colors[i]);
- logpal.palPalEntry[i].peGreen = FW_RGB_G(colors[i]);
- logpal.palPalEntry[i].peBlue = FW_RGB_B(colors[i]);
- logpal.palPalEntry[i].peFlags = 0;
- }
-
- return ::CreatePalette((LOGPALETTE*)&logpal);
- #endif
- #ifdef FW_BUILD_MAC
- Size colorTableSize = sizeof(ColorTable) + sizeof(ColorSpec) * (nColorCount - 1);
- CTabHandle colorTableHandle = (CTabHandle) NewHandle(colorTableSize);
- if (colorTableHandle != 0)
- {
- HLock((Handle)colorTableHandle);
- CTabPtr colorTablePtr = *colorTableHandle;
-
- colorTablePtr->ctSeed = 0;
- colorTablePtr->ctFlags = 0;
- colorTablePtr->ctSize = nColorCount - 1;
-
- for (short i = 0; i < nColorCount; ++ i)
- {
- colorTablePtr->ctTable[i].value = i;
- FW_MacColorToRGB(colors[i], &colorTablePtr->ctTable[i].rgb);
- }
-
- HUnlock((Handle)colorTableHandle);
- ::CTabChanged(colorTableHandle);
- }
- return colorTableHandle;
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_GetPaletteSize
- //----------------------------------------------------------------------------------------
-
- short SL_API FW_GetPaletteSize(FW_Palette palette)
- {
- // No try block necessary - do not throw
- FW_ASSERT(palette != NULL);
-
- #ifdef FW_BUILD_WIN
- WORD wCount;
- ::GetObject(palette, sizeof(WORD), &wCount);
- return wCount;
- #endif
- #ifdef FW_BUILD_MAC
- return (*palette)->ctSize + 1;
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_SetPaletteEntries
- //----------------------------------------------------------------------------------------
-
- void SL_API FW_SetPaletteEntries(FW_Palette palette,
- const FW_SColor* colors,
- short nStartIndex,
- short nColorCount)
- {
- // No try block necessary - do not throw
- FW_ASSERT(palette != NULL);
- FW_ASSERT(nStartIndex >= 0);
- FW_ASSERT(nStartIndex + nColorCount <= FW_GetPaletteSize(palette));
-
- #ifdef FW_BUILD_WIN
- PALETTEENTRY palEntries[256];
-
- for (short i = 0; i < nColorCount; ++i, ++colors)
- {
- palEntries[i].peRed = FW_RGB_R(*colors);
- palEntries[i].peGreen = FW_RGB_G(*colors);
- palEntries[i].peBlue = FW_RGB_B(*colors);
- palEntries[i].peFlags = 0;
- }
-
- ::SetPaletteEntries(palette, nStartIndex, nColorCount, palEntries);
- #endif
- #ifdef FW_BUILD_MAC
- HLock((Handle)palette);
- CTabPtr colorTablePtr = *palette;
- for (short i = nStartIndex; i < nStartIndex + nColorCount; ++ i, ++ colors)
- {
- colorTablePtr->ctTable[i].value = i;
- FW_MacColorToRGB(*colors, &colorTablePtr->ctTable[i].rgb);
- }
- HUnlock((Handle)palette);
-
- ::CTabChanged(palette);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_GetPaletteEntries
- //----------------------------------------------------------------------------------------
-
- void SL_API FW_GetPaletteEntries(FW_Palette palette,
- FW_SColor* colors,
- short nStartIndex,
- short nColorCount)
- {
- // No try block necessary - do not throw
- FW_ASSERT(palette != NULL);
- FW_ASSERT(nStartIndex >= 0);
- FW_ASSERT(nStartIndex + nColorCount <= FW_GetPaletteSize(palette));
-
- #ifdef FW_BUILD_WIN
- PALETTEENTRY palEntries[256];
-
- ::GetPaletteEntries(palette, nStartIndex, nColorCount, palEntries);
-
- for (short i = 0; i < nColorCount; ++i, ++colors)
- {
- FW_SetRGB(*colors, palEntries[i].peRed, palEntries[i].peGreen, palEntries[i].peBlue);
- }
- #endif
- #ifdef FW_BUILD_MAC
- HLock((Handle)palette);
- CTabPtr colorTablePtr = *palette;
- for (short i = nStartIndex; i < nStartIndex + nColorCount; ++ i, ++ colors)
- {
- *colors = FW_MacRGBToColor(&colorTablePtr->ctTable[i].rgb);
- }
- HUnlock((Handle)palette);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivCopyPalette
- //----------------------------------------------------------------------------------------
-
- FW_Palette SL_API FW_PrivCopyPalette(FW_Palette palette)
- {
- // No try block necessary - returns NULL if fails
- FW_ASSERT(palette != NULL);
-
- #ifdef FW_BUILD_WIN
- SWinPal logpal;
- logpal.palVersion = 0x300;
- ::GetObject(palette, sizeof(WORD), &logpal.palNumEntries);
-
- ::GetPaletteEntries(palette, 0, logpal.palNumEntries, logpal.palPalEntry);
-
- return ::CreatePalette((LOGPALETTE*)&logpal);
- #endif
- #ifdef FW_BUILD_MAC
- FW_Palette palCopy = palette;
- HandToHand((Handle*)&palCopy);
- if (palCopy != NULL)
- {
- (*palCopy)->ctSeed = 0;
- ::CTabChanged(palCopy);
- }
- return palCopy;
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_DestroyPalette
- //----------------------------------------------------------------------------------------
-
- void SL_API FW_DestroyPalette(FW_Palette palette)
- {
- // No try block necessary - do not throw
- #ifdef FW_BUILD_WIN
- ::DeleteObject(palette);
- #endif
- #ifdef FW_BUILD_MAC
- ::DisposeCTable(palette);
- #endif
- }
-