Files
Work/proto/source/PUREPAL.CPP
2024-08-07 09:16:27 -04:00

256 lines
8.0 KiB
C++

#include <proto/purepal.hpp>
#include <common/bminfo.hpp>
#include <bsptree/rgbtree.hpp>
PurePalette::PurePalette(const PurePalette &somePurePalette)
: mhPalette(0), mhOldPalette(), mIsInUse(FALSE)
{
::GetPaletteEntries(somePurePalette,0,MaxColors,(PALETTEENTRY FAR *)&mPaletteData);
createPalette();
}
PurePalette::PurePalette(HPALETTE hPalette)
: mhPalette(0), mhOldPalette(0), mIsInUse(FALSE)
{
::GetPaletteEntries(hPalette,0,MaxColors,(PALETTEENTRY FAR *)&mPaletteData);
createPalette();
}
PurePalette::PurePalette(const BitmapInfo &someBitmapInfo)
: mhPalette(0), mhOldPalette(0), mIsInUse(FALSE)
{
*this=someBitmapInfo;
}
PurePalette &PurePalette::operator=(const PurePalette &somePurePalette)
{
destroyPalette();
::GetPaletteEntries(somePurePalette,0,MaxColors,(PALETTEENTRY FAR*)&mPaletteData);
createPalette();
return *this;
}
PurePalette &PurePalette::operator=(const BitmapInfo &someBitmapInfo)
{
destroyPalette();
for(int entryIndex=0;entryIndex<someBitmapInfo.colorImportant()&&entryIndex<MaxColors;entryIndex++)
{
mPaletteData[entryIndex].red(((BitmapInfo&)someBitmapInfo)[entryIndex].red());
mPaletteData[entryIndex].green(((BitmapInfo&)someBitmapInfo)[entryIndex].green());
mPaletteData[entryIndex].blue(((BitmapInfo&)someBitmapInfo)[entryIndex].blue());
}
createPalette();
return *this;
}
PurePalette &PurePalette::operator=(HPALETTE hPalette)
{
destroyPalette();
::GetPaletteEntries(hPalette,0,MaxColors,(PALETTEENTRY FAR*)&mPaletteData);
createPalette();
return *this;
}
WORD PurePalette::operator==(const PurePalette &somePurePalette)const
{
for(short paletteIndex=0;paletteIndex<MaxColors;paletteIndex++)
if(!(mPaletteData[paletteIndex]==somePurePalette.mPaletteData[paletteIndex]))return FALSE;
return TRUE;
}
void PurePalette::destroyPalette(void)
{
if(mhPalette){::DeleteObject(mhPalette);mhPalette=0;}
mhOldPalette=0;
mIsInUse=0;
clearPaletteEntries();
return;
}
void PurePalette::cyclePalette(void)
{
PaletteEntry paletteEntryOne(mPaletteData[0]);
::memcpy(&mPaletteData[0],&mPaletteData[1],sizeof(mPaletteData)-sizeof(PaletteEntry));
mPaletteData[MaxColors-1]=paletteEntryOne;
setPaletteColors();
}
WORD PurePalette::paletteEntry(PaletteIndex paletteIndex,RGBColor &someRGBColor)const
{
if(paletteIndex>=paletteEntries())return FALSE;
someRGBColor=(RGBColor)mPaletteData[paletteIndex];
return TRUE;
}
RGBColor PurePalette::paletteEntry(PaletteIndex paletteIndex)const
{
RGBColor tempColor(0,0,0);
paletteEntry(paletteIndex,tempColor);
return tempColor;
}
WORD PurePalette::createPalette()
{
PaletteEntry FAR *lpPaletteEntry;
LOGPALETTE FAR *lpLogPalette;
HGLOBAL hGlobalPalette;
if(mhPalette){::DeleteObject(mhPalette);mhPalette=0;}
hGlobalPalette=::GlobalAlloc(GMEM_ZEROINIT|GMEM_MOVEABLE,sizeof(LOGPALETTE)+(MaxColors*sizeof(PALETTEENTRY)));
lpLogPalette=(LOGPALETTE FAR *)::GlobalLock(hGlobalPalette);
lpLogPalette->palNumEntries=MaxColors;
lpLogPalette->palVersion=0x300;
for(short paletteIndex=0;paletteIndex<MaxColors;paletteIndex++)
{
lpPaletteEntry=(PaletteEntry FAR *)&lpLogPalette->palPalEntry[paletteIndex];
*lpPaletteEntry=mPaletteData[paletteIndex];
}
mhPalette=::CreatePalette((LOGPALETTE FAR *)lpLogPalette);
::GlobalUnlock(hGlobalPalette);
::GlobalFree(hGlobalPalette);
return (mhPalette?TRUE:FALSE);
}
void PurePalette::usePalette(const PureDevice &somePureDevice,short usage)
{
if(usage)
{
mhOldPalette=::SelectPalette(somePureDevice,mhPalette,FALSE);
::RealizePalette(somePureDevice);
mIsInUse=TRUE;
}
else
{
::SelectPalette(somePureDevice,mhOldPalette,FALSE);
::RealizePalette(somePureDevice);
mIsInUse=FALSE;
}
}
void PurePalette::getPaletteColors(PureVector<RGBColor> &someRGBColors)const
{
size_t paletteColors((size_t)someRGBColors.size(paletteEntries()));
for(short paletteIndex=0;paletteIndex<paletteColors;paletteIndex++)
someRGBColors[paletteIndex]=(RGBColor)mPaletteData[paletteIndex];
}
WORD PurePalette::getPaletteColor(short paletteIndex,RGBColor &someRGBColor)const
{
if(paletteIndex>=paletteEntries())return FALSE;
someRGBColor=(RGBColor)mPaletteData[paletteIndex];
return TRUE;
}
void PurePalette::setPaletteColors(PureVector<RGBColor> &someRGBColors,PaletteEntry::PaletteFlags paletteFlag)
{
size_t paletteColors((size_t)someRGBColors.size());
for(short paletteIndex=0;paletteIndex<paletteColors;paletteIndex++)
setPaletteColor(paletteIndex,someRGBColors[paletteIndex],paletteFlag);
}
WORD PurePalette::setPaletteColor(short paletteIndex,RGBColor &someRGBColor,PaletteEntry::PaletteFlags paletteFlag)
{
if(paletteIndex>=paletteEntries())return FALSE;
mPaletteData[paletteIndex]=someRGBColor;
mPaletteData[paletteIndex].flags(paletteFlag);
::SetPaletteEntries(mhPalette,paletteIndex,1,(PALETTEENTRY FAR*)&mPaletteData[paletteIndex]);
return TRUE;
}
void PurePalette::clearPalette(void)
{
short paletteColors(paletteEntries());
RGBColor nullColor;
for(short paletteIndex=0;paletteIndex<paletteColors;paletteColors++)setPaletteColor(paletteIndex,nullColor);
}
WORD PurePalette::systemPalette(void)
{
PureDevice screenDevice;
WORD systemPaletteEntries;
destroyPalette();
screenDevice.screenDevice();
systemPaletteEntries=::GetSystemPaletteEntries(screenDevice,0,MaxColors,(PALETTEENTRY FAR*)&mPaletteData);
createPalette();
return systemPaletteEntries;
}
WORD PurePalette::identityPalette(void)
{
PaletteEntry paletteData[MaxColors];
RGBTree paletteTree;
RGBTree systemTree;
RGBColor systemColor;
PureDevice pureDevice;
WORD staticColors;
WORD useableIndex(0);
RGBColor rgbColor;
pureDevice.screenDevice();
if(SYSPAL_NOSTATIC==::GetSystemPaletteUse(pureDevice))
{
PureVector<RGBColor> paletteColors;
getPaletteColors(paletteColors);
setPaletteColors(paletteColors,PaletteEntry::NoCollapse);
setPaletteColor(PurePalette::MaxColors-1,RGBColor(255,255,255));
setPaletteColor(0,RGBColor(0,0,0));
createPalette();
return TRUE;
}
if(SystemStaticColors!=(staticColors=::GetDeviceCaps(pureDevice,NUMCOLORS)))return FALSE;
staticColors/=2;
if(MaxColors!=::GetSystemPaletteEntries(pureDevice,0,MaxColors,(PALETTEENTRY FAR *)&paletteData))return FALSE;
for(short paletteIndex=0;paletteIndex<staticColors;paletteIndex++)
systemTree.insertItem(paletteData[paletteIndex]);
for(paletteIndex=MaxColors-staticColors;paletteIndex<MaxColors;paletteIndex++)
systemTree.insertItem(paletteData[paletteIndex]);
for(short index=0;index<staticColors;index++)paletteData[index].flags(PaletteEntry::NullFlag);
while(index<MaxColors-staticColors&&useableIndex<MaxColors)
{
getPaletteColor(useableIndex,rgbColor);
if(!systemTree.searchItem(rgbColor))
{
if(!paletteTree.searchItem(rgbColor))
{
paletteTree.insertItem(rgbColor);
paletteData[index]=rgbColor;
paletteData[index].flags(PaletteEntry::NullFlag);
index++;
}
}
useableIndex++;
}
for(;index<MaxColors-staticColors;index++)paletteData[index].flags(PaletteEntry::NullFlag); // NoCollapse
for(index=MaxColors-staticColors;index<MaxColors;index++)paletteData[index].flags(PaletteEntry::NullFlag);
::memcpy(mPaletteData,paletteData,sizeof(paletteData));
createPalette();
return TRUE;
}
WORD PurePalette::isIdentityPalette(void)const
{
PaletteEntry paletteData[MaxColors];
PureDevice pureDevice;
WORD staticColors;
pureDevice.screenDevice();
if(SYSPAL_NOSTATIC==::GetSystemPaletteUse(pureDevice))
{
if(!(RGBColor(255,255,255)==mPaletteData[PurePalette::MaxColors-1]))return FALSE;
if(!(RGBColor(0,0,0)==mPaletteData[0]))return FALSE;
return TRUE;
}
if(SystemStaticColors!=(staticColors=::GetDeviceCaps(pureDevice,NUMCOLORS)))return FALSE;
staticColors/=2;
if(MaxColors!=::GetSystemPaletteEntries(pureDevice,0,MaxColors,(PALETTEENTRY FAR *)&paletteData))return FALSE;
for(short paletteIndex=0;paletteIndex<staticColors;paletteIndex++)
if(!(RGBColor(mPaletteData[paletteIndex])==RGBColor(paletteData[paletteIndex])))return FALSE;
for(paletteIndex=MaxColors-staticColors;paletteIndex<MaxColors;paletteIndex++)
if(!(RGBColor(mPaletteData[paletteIndex])==RGBColor(paletteData[paletteIndex])))return FALSE;
return TRUE;
}