First public contribution.
1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
23 _LIT(KGdiCPalettePanicCategory,"CPalette");
25 EXPORT_C CPalette::CPalette():
32 EXPORT_C CPalette::~CPalette()
39 EXPORT_C void CPalette::Clear()
40 /** Clears all the entries in the palette to TRgb(0). */
43 for(TInt count=0;count<iNumEntries;count++)
48 EXPORT_C CPalette* CPalette::NewL(TInt aNumberOfEntries)
49 /** Creates a new palette with the specified number of entries.
51 @param aNumberOfEntries The number of entries in the palette being created.
52 @return The newly created palette. */
54 CPalette* thisptr=new(ELeave) CPalette;
55 CleanupStack::PushL(thisptr);
56 thisptr->ConstructL(aNumberOfEntries);
62 EXPORT_C CPalette* CPalette::NewDefaultL(TDisplayMode aDispMode)
63 /** Creates a new default palette for the specified display mode. The default palette
64 for a particular display mode has one entry for each possible colour in that
65 display mode (2 entries for EGray2, 16 entries for EColor16 etc.); the colour
66 of each index p in the default palette is set to its default value according
67 to its display mode (e.g. if the mode is EColor16 then palette[p]==TRgb::Color16(p);
68 if the mode is EGray4 then palette[p]==TRgb::_Gray4(p)).
70 @param aDispMode The display mode for which the palette is to be created.
71 @return The newly created palette */
91 User::Leave(KErrNotSupported);
93 CPalette* thisptr=new(ELeave) CPalette;
94 CleanupStack::PushL(thisptr);
95 thisptr->ConstructL(numentries);
100 thisptr->iArray[0]=TRgb::_Gray2(0);
101 thisptr->iArray[1]=TRgb::_Gray2(1);
104 for(;count<numentries;count++)
105 thisptr->iArray[count]=TRgb::_Gray4(count);
108 for(;count<numentries;count++)
109 thisptr->iArray[count]=TRgb::_Gray16(count);
112 for(;count<numentries;count++)
113 thisptr->iArray[count]=TRgb::Color16(count);
116 for(;count<numentries;count++)
117 thisptr->iArray[count]=TRgb::_Gray256(count);
120 for(;count<numentries;count++)
121 thisptr->iArray[count]=TRgb::Color256(count);
124 User::Leave(KErrNotSupported);
130 void CPalette::ConstructL(TInt aNumberOfEntries)
132 if(aNumberOfEntries<=0) User::Leave(KErrArgument);
133 iArray=new(ELeave) TRgb[aNumberOfEntries];
134 iNumEntries=aNumberOfEntries;
138 EXPORT_C TRgb CPalette::GetEntry(TInt aIndex) const
139 /** Gets the RGB value of the palette entry at aPaletteIndex.
141 @param aPaletteIndex The index of the palette entry to get.
142 @return The RGB value of the palette entry */
144 GDI_ASSERT_ALWAYS_GENERAL(aIndex<iNumEntries,User::Panic(KGdiCPalettePanicCategory,KErrTooBig));
145 if(aIndex<iNumEntries)
146 return(iArray[aIndex]);
147 TRgb defaultcol(0,0,0);
152 EXPORT_C TRgb CPalette::NearestEntry(const TRgb& aColor) const
153 /** Gets the colour in the palette which is closest to the specified colour.
155 @param aColor The colour to find.
156 @return The colour in the palette which is closest to the specified colour. */
158 return(iArray[NearestIndex(aColor)]);
162 EXPORT_C TInt CPalette::NearestIndex(const TRgb& aColor) const
163 /** Gets the index of the colour in the palette which is closest to the specified
166 @param aColor The colour to find.
167 @return The index of the colour in the palette which is closest to the specified
170 TRgb* entry = iArray;
171 TRgb* entryLimit = entry+iNumEntries;
172 TRgb* lowest = entry;
173 TInt mindif = KMaxTInt;
175 while(entry<entryLimit)
177 TInt value = entry->Internal();
179 TInt difference = Abs((TInt)(aColor.Internal()&0xFF)-(TInt)(value&0xFF))+
180 (Abs((TInt)(aColor.Internal()&0xFF00)-(TInt)(value&0xFF00))>>8)+
181 (Abs((TInt)(aColor.Internal()&0xFF0000)-(TInt)(value&0xFF0000))>>16);
183 if(difference<mindif)
192 return(lowest-iArray);
196 EXPORT_C void CPalette::SetEntry(TInt aIndex,const TRgb& aColor)
197 /** Sets the palette entry at aPaletteIndex to the RGB value aPaletteEntry.
199 @param aPaletteIndex The index of the palette entry to be set.
200 @param aPaletteEntry The RGB value to set that entry to. */
202 GDI_ASSERT_ALWAYS_GENERAL(aIndex<iNumEntries,User::Panic(KGdiCPalettePanicCategory,KErrTooBig));
203 iArray[aIndex]=aColor;
207 EXPORT_C void CPalette::GetDataPtr(TInt aFirstColor,TInt aNumColors,TPtr8& aPtr)
208 /** Returns a descriptor over the palette entries for the specifed colors. It is
209 designed so that the descriptor can be passed to another thread and that thread
210 copy the relevant entries.
212 @param aFirstColor The first colour.
213 @param aNumColors Number of colours.
214 @param aPtr Descriptor. */
216 TInt size=sizeof(TRgb)*aNumColors;
217 aPtr.Set(REINTERPRET_CAST(TUint8*,&iArray[aFirstColor]),size,size);