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.
16 #include <graphics/lookuptable.h>
17 #include <graphics/blendingalgorithms.h>
21 // Class TRgb Definition
23 /** Sets the red component.
25 @param aRed Red component (0 - 255). */
26 EXPORT_C void TRgb::SetRed(TInt aRed)
29 iValue|=(aRed&0xff)<<16;
32 /** Sets the green component.
34 @param aGreen Green component (0 - 255). */
35 EXPORT_C void TRgb::SetGreen(TInt aGreen)
38 iValue|=(aGreen&0xff)<<8;
41 /** Sets the blue component.
43 @param aBlue Blue component (0 - 255). */
44 EXPORT_C void TRgb::SetBlue(TInt aBlue)
50 /** Gets TRgb from 2 level grayscale.
52 The function takes a grayscale argument and return a TRgb whose red, green
53 and blue values are set to an appropriate level.
55 @param aGray2 Grayscale value to be converted.
56 @return Equivalent 24 bit colour. Gray2 has only 2 levels (black and white),
57 the function returns r=g=b=0 or r=g=b=255. */
58 EXPORT_C TRgb TRgb::Gray2(TInt aGray2)
60 if(aGray2) return(TRgb(0xffffff, 0xff));
61 return(TRgb(0, 0xff));
64 /** Gets TRgb from 4 level grayscale.
66 The function takes a grayscale argument and return a TRgb whose red, green
67 and blue values are set to an appropriate level.
69 @param aGray4 Grayscale value to be converted.
70 @return Equivalent 24 bit colour. Gray4 has 4 levels- the function returns
71 r=g=b=85*c, where c=0,1,2, or 3. */
72 EXPORT_C TRgb TRgb::Gray4(TInt aGray4)
77 return(TRgb(aGray4,aGray4,aGray4));
80 /** Gets TRgb from 16 level grayscale.
82 The function takes a grayscale argument and return a TRgb whose red, green
83 and blue values are set to an appropriate level.
85 @param aGray16 Grayscale value to be converted.
86 @return Equivalent 24 bit colour. Gray16 has 16 levels - the function returns
87 r=g=b=17*c, where c=0, 1, ... 15. */
88 EXPORT_C TRgb TRgb::Gray16(TInt aGray16)
92 return(TRgb(aGray16,aGray16,aGray16));
95 /** Gets TRgb from 256 level grayscale.
97 The function takes a grayscale argument and return a TRgb whose red, green
98 and blue values are set to an appropriate level.
100 @param aGray256 Grayscale value to be converted.
101 @return Equivalent 24 bit colour. Gray256 has 256 levels- the function
102 returns r=g=b=c, where c=0, 1, ... 255. */
103 EXPORT_C TRgb TRgb::Gray256(TInt aGray256)
106 return(TRgb(aGray256,aGray256,aGray256));
109 /** Gets TRgb from 4 bit colour index.
111 The function takes a 4 bit index into a colour palette and returns a TRgb
112 whose red, green and blue values are set to an appropriate level.
114 @param aColor16 4 bit index into a colour palette
115 @return Equivalent 24 bit colour. */
116 EXPORT_C TRgb TRgb::Color16(TInt aColor16)
118 return(TRgb(DynamicPalette::Color16array()[aColor16&0xf]));
121 /** Gets TRgb from 8 bit colour index.
123 The function takes an 8 bit index into a colour palette and returns a TRgb
124 whose red, green and blue values are set to an appropriate level.
126 @param aColor256 8 bit index into a colour palette.
127 @return Equivalent 24 bit colour. */
128 EXPORT_C TRgb TRgb::Color256(TInt aColor256)
130 return(TRgb(DynamicPalette::DefaultColor256Util()->iColorTable[aColor256&0xff]));
133 /** Gets TRgb from 4K colour index.
135 The function takes a 12 bit index into a colour palette and returns a TRgb
136 whose red, green and blue values are set to an appropriate level.
138 @param aColor4K 12 bit index into a colour palette
139 @return Equivalent 24 bit colour. */
140 EXPORT_C TRgb TRgb::Color4K(TInt aColor4K)
142 return _Color4K(aColor4K);
145 /** Gets TRgb from 64K colour index.
147 The function takes a 16 bit index into a colour palette and returns a TRgb
148 whose red, green and blue values are set to an appropriate level.
150 @param aColor64K 16 bit index into a colour palette
151 @return Equivalent 24 bit colour. */
152 EXPORT_C TRgb TRgb::Color64K(TInt aColor64K)
154 return _Color64K(aColor64K);
157 /** Gets TRgb from 16M colour index.
159 The function takes a 24 bit index into a colour palette and returns the TRgb
160 whose red, green and blue values represent it exactly.
162 @param aColor16M 24 bit index into a colour palette
163 @return The TRgb which represents the index exactly. */
164 EXPORT_C TRgb TRgb::Color16M(TInt aColor16M)
166 return _Color16M(aColor16M);
169 /** Gets the index of the closest TRgb value to this,
170 based on the matching display mode.
172 @return The index (0 - 1) representing the nearest TRgb. */
173 EXPORT_C TInt TRgb::Gray2() const
178 /**Gets the index of the closest TRgb value to this,
179 based on the matching display mode.
181 @return The index (0 - 3) representing the nearest TRgb. */
182 EXPORT_C TInt TRgb::Gray4() const
187 /** Gets the index of the closest TRgb value to this,
188 based on the matching display mode.
190 @return The index (0 - 15) representing the nearest TRgb.*/
191 EXPORT_C TInt TRgb::Gray16() const
196 /** Gets the index of the closest TRgb value to this,
197 based on the matching display mode.
199 @return The index (0 - 255) representing the nearest TRgb.*/
200 EXPORT_C TInt TRgb::Gray256() const
205 /** Gets the index of the closest TRgb value to this,
206 based on the matching display mode.
208 @return The index (0 - 15) representing the nearest TRgb. */
209 EXPORT_C TInt TRgb::Color16() const
211 TInt index = (iValue & 0x000000e0) << 1;
212 index |= (iValue & 0x0000e000) >> 10;
213 index |= (iValue & 0x00e00000) >> 21;
214 return DynamicPalette::Color16inverse()[index];
217 /** Gets the index of the closest TRgb value to this,
218 based on the matching display mode.
220 @return The index (0 - 255) representing the nearest TRgb. */
221 EXPORT_C TInt TRgb::Color256() const
223 TInt index = (iValue & 0x000000f0) << 4;
224 index |= (iValue & 0x0000f000) >> 8;
225 index |= (iValue & 0x00f00000) >> 20;
226 return DynamicPalette::DefaultColor256Util()->iInverseColorTable[index];
229 /** Gets the index of the closest TRgb value to this,
230 based on the matching display mode.
232 @return The index (0 - 4095) representing the nearest TRgb. */
233 EXPORT_C TInt TRgb::Color4K() const
238 /** Gets the index of the closest TRgb value to this,
239 based on the matching display mode.
241 @return The index (0 - 65535) representing the nearest TRgb.*/
242 EXPORT_C TInt TRgb::Color64K() const
247 /** Gets the index of the closest TRgb value to this,
248 based on the matching display mode.
250 @return The index (0 - 16777215) representing the nearest TRgb.*/
251 EXPORT_C TInt TRgb::Color16M() const
256 /** Gets the difference between two TRgbs.
258 This difference is defined as the sum of the absolute values of the difference
259 in the red, green and blue components.
261 @param aColor The TRgb to be compared.
262 @return The sum of the absolute value of the differences between the red, green
263 and blue components. */
264 EXPORT_C TInt TRgb::Difference(const TRgb& aColor) const
266 return(Abs((TInt)(aColor.Internal()&0xFF)-(TInt)(Internal()&0xFF))+
267 (Abs((TInt)(aColor.Internal()&0xFF00)-(TInt)(Internal()&0xFF00))>>8)+
268 (Abs((TInt)(aColor.Internal()&0xFF0000)-(TInt)(Internal()&0xFF0000))>>16));
271 /** Internalises a TRgb object from a stream.
273 The presence of this function means that the standard templated stream operator>>()
274 is available to internalise objects of this class.
276 @param aStream Stream from which the object is internalised.
278 EXPORT_C void TRgb::InternalizeL(RReadStream& aStream)
280 TInt red=aStream.ReadUint8L();
281 TInt green=aStream.ReadUint8L();
282 TInt blue=aStream.ReadUint8L();
283 *this=TRgb(red,green,blue);
286 /** Externalises a TRgb object to a stream.
288 The presence of this function means that the standard templated stream operator<<()
289 is available to externalise objects of this class.
291 @param aStream Stream to which the object is externalised. */
292 EXPORT_C void TRgb::ExternalizeL(RWriteStream& aStream) const
294 aStream.WriteUint8L(Red());
295 aStream.WriteUint8L(Green());
296 aStream.WriteUint8L(Blue());
299 /** Gets TRgb from 16MU colour index.
300 The function takes a 24 bit colour value with eight bits for each
301 component, blue in the low byte, and returns the TRgb
302 whose red, green and blue values represent it exactly.
303 @param a0RGB The color - 0, R, G, B bytes. / BGR0 - little endian format /
304 @return The TRgb which represents the index exactly. */
305 EXPORT_C TRgb TRgb::Color16MU(TInt a0RGB)
308 return _Color16MU(a0RGB);
311 /** Gets the index of the closest TRgb value to this, based on the matching display mode.
312 @return The index (0 - 16777215) representing the nearest TRgb. */
313 EXPORT_C TInt TRgb::Color16MU() const
320 /** Sets the alpha component.
322 @param aAlpha Alpha component (0 - 255). */
323 EXPORT_C void TRgb::SetAlpha(TInt aAlpha)
326 iValue|=(aAlpha << 24);
330 /** Gets TRgb from 16MA colour index.
331 The function takes a 32 bit colour value with eight bits for each
332 component, blue in the low byte, and returns the TRgb
333 whose red, green, blue and alpha values represent it exactly.
334 @param aARGB The color - A, R, G, B bytes. / BGRA - little endian format /
335 @return The TRgb which represents the index exactly. */
336 EXPORT_C TRgb TRgb::Color16MA(TUint aARGB)
338 return _Color16MA(aARGB);
341 /** Gets the index of the closest TRgb value to this, based on the matching display mode.
342 @return The index (0 - 16777215) representing the nearest TRgb. */
343 EXPORT_C TUint TRgb::Color16MA() const
348 /** Gets TRgb from 16MAP colour index.
349 The function takes a 32 bit colour value with eight bits for each
350 component, blue in the low byte, and returns the TRgb
351 whose red, green, and blue vales are divided by the alpha value.
352 @param aARGB The pre-multiplied (EColor16MAP) color value.
353 @return The TRgb which represents the color channel and alpha information. */
355 EXPORT_C TRgb TRgb::Color16MAP(TUint aARGB)
357 const TUint16* ArTable = PtrTo16BitNormalisationTable();
359 retColor.SetInternal(PMA2NonPMAPixel(aARGB, ArTable));
363 /** Gets the index of the closest TRgb value to this, based on the matching display mode.
364 pre-multiplies the alpha channels with the color channel.
365 @return The pre-multiplied color value */
366 EXPORT_C TUint TRgb::Color16MAP() const
368 return NonPMA2PMAPixel(iValue);
372 /** Tests whether the display mode specified is colour or greyscale.
374 @param aDispMode The display mode.
375 @return ETrue if colour; EFalse if greyscale or monochrome. */
376 EXPORT_C TBool TDisplayModeUtils::IsDisplayModeColor(TDisplayMode aDispMode)
378 return (aDispMode >= EColor16);
381 /** Tests whether the display mode specified is one of the valid values.
383 @param aDispMode The display mode to be tested.
384 @return ETrue if aDispMode is valid; EFalse if not valid. */
385 EXPORT_C TBool TDisplayModeUtils::IsDisplayModeValid(TDisplayMode aDispMode)
387 return aDispMode >= ENone && aDispMode < EColorLast;
390 /** Gets the number of colours or shades of grey supported by the specified
393 For instance, a display mode of EGray4 returns 4, EColor4K returns 4096.
395 @param aDispMode The display mode.
396 @return The number of colours/grey shades supported by the display mode. */
397 EXPORT_C TInt TDisplayModeUtils::NumDisplayModeColors(TDisplayMode aDispMode)
425 /** Gets the number of bits required by each pixel when displayed in the
426 specified display mode.
428 @param aDispMode The display mode.
429 @return The number of bits required by each pixel. */
430 EXPORT_C TInt TDisplayModeUtils::NumDisplayModeBitsPerPixel(TDisplayMode aDispMode)
462 /** Initialises the two lookup tables using the specified palette.
464 @param aPalette The palette of colours used to initialise the colour lookup
466 EXPORT_C void TColor256Util::Construct(const CPalette& aPalette)
468 TInt n = aPalette.Entries();
474 iColorTable[i] = aPalette.GetEntry(i).Value();
479 for(TInt b=0; b<0x100; b+=0x11)
480 for(TInt g=0; g<0x100; g+=0x11)
481 for(TInt r=0; r<0x100; r+=0x11)
482 iInverseColorTable[i++] = (TUint8)aPalette.NearestIndex(TRgb(r,g,b));
485 /** Gets the entry from the inverse colour lookup table for the colour that most
486 closely matches the specified TRgb value.
488 Entries in the inverse colour lookup table are indices into the palette that
489 the object was created with. Essentially, this function matches aRgb to the
490 index of the nearest colour in the palette.
492 @param aRgb The conversion colour.
493 @return The index of the nearest colour to aRgb in the palette. */
494 EXPORT_C TInt TColor256Util::Color256(TRgb aRgb) const
496 TInt index = (aRgb.Value() & 0x000000f0) >> 4;
497 index |= (aRgb.Value() & 0x0000f000) >> 8;
498 index |= (aRgb.Value() & 0x00f00000) >> 12;
499 return iInverseColorTable[index];
502 /** Gets the entries from the inverse colour lookup table for the colours that
503 most closely match the specified TRgb values.
505 @param aDestination On return, a pointer to a buffer containing the entries
506 from the inverse colour lookup table.
507 @param aSource Pointer to the first TRgb value to match.
508 @param aNumPixels The number of TRgb values to match. */
509 EXPORT_C void TColor256Util::Color256(TUint8* aDestination,const TRgb* aSource,TInt aNumPixels) const
511 TUint8* limit = aDestination+aNumPixels;
512 while(aDestination<limit)
514 TInt value = (*aSource++).Value();
515 TInt index = (value & 0x000000f0) >> 4;
516 index |= (value & 0x0000f000) >> 8;
517 index |= (value & 0x00f00000) >> 12;
518 *(aDestination++) = iInverseColorTable[index];
522 /** Returns a pointer to the system default 256 colour palette.
524 @return Pointer to the system default 256 colour palette. */
525 EXPORT_C const TColor256Util* TColor256Util::Default()
527 return DynamicPalette::DefaultColor256Util();