sl@0: // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: // Class TRgb Definition sl@0: sl@0: /** Sets the red component. sl@0: sl@0: @param aRed Red component (0 - 255). */ sl@0: EXPORT_C void TRgb::SetRed(TInt aRed) sl@0: { sl@0: iValue&=0xff00ffff; sl@0: iValue|=(aRed&0xff)<<16; sl@0: } sl@0: sl@0: /** Sets the green component. sl@0: sl@0: @param aGreen Green component (0 - 255). */ sl@0: EXPORT_C void TRgb::SetGreen(TInt aGreen) sl@0: { sl@0: iValue&=0xffff00ff; sl@0: iValue|=(aGreen&0xff)<<8; sl@0: } sl@0: sl@0: /** Sets the blue component. sl@0: sl@0: @param aBlue Blue component (0 - 255). */ sl@0: EXPORT_C void TRgb::SetBlue(TInt aBlue) sl@0: { sl@0: iValue&=0xffffff00; sl@0: iValue|=(aBlue&0xff); sl@0: } sl@0: sl@0: /** Gets TRgb from 2 level grayscale. sl@0: sl@0: The function takes a grayscale argument and return a TRgb whose red, green sl@0: and blue values are set to an appropriate level. sl@0: sl@0: @param aGray2 Grayscale value to be converted. sl@0: @return Equivalent 24 bit colour. Gray2 has only 2 levels (black and white), sl@0: the function returns r=g=b=0 or r=g=b=255. */ sl@0: EXPORT_C TRgb TRgb::Gray2(TInt aGray2) sl@0: { sl@0: if(aGray2) return(TRgb(0xffffff, 0xff)); sl@0: return(TRgb(0, 0xff)); sl@0: } sl@0: sl@0: /** Gets TRgb from 4 level grayscale. sl@0: sl@0: The function takes a grayscale argument and return a TRgb whose red, green sl@0: and blue values are set to an appropriate level. sl@0: sl@0: @param aGray4 Grayscale value to be converted. sl@0: @return Equivalent 24 bit colour. Gray4 has 4 levels- the function returns sl@0: r=g=b=85*c, where c=0,1,2, or 3. */ sl@0: EXPORT_C TRgb TRgb::Gray4(TInt aGray4) sl@0: { sl@0: aGray4&=3; sl@0: aGray4|=aGray4<<2; sl@0: aGray4|=aGray4<<4; sl@0: return(TRgb(aGray4,aGray4,aGray4)); sl@0: } sl@0: sl@0: /** Gets TRgb from 16 level grayscale. sl@0: sl@0: The function takes a grayscale argument and return a TRgb whose red, green sl@0: and blue values are set to an appropriate level. sl@0: sl@0: @param aGray16 Grayscale value to be converted. sl@0: @return Equivalent 24 bit colour. Gray16 has 16 levels - the function returns sl@0: r=g=b=17*c, where c=0, 1, ... 15. */ sl@0: EXPORT_C TRgb TRgb::Gray16(TInt aGray16) sl@0: { sl@0: aGray16&=0xf; sl@0: aGray16|=aGray16<<4; sl@0: return(TRgb(aGray16,aGray16,aGray16)); sl@0: } sl@0: sl@0: /** Gets TRgb from 256 level grayscale. sl@0: sl@0: The function takes a grayscale argument and return a TRgb whose red, green sl@0: and blue values are set to an appropriate level. sl@0: sl@0: @param aGray256 Grayscale value to be converted. sl@0: @return Equivalent 24 bit colour. Gray256 has 256 levels- the function sl@0: returns r=g=b=c, where c=0, 1, ... 255. */ sl@0: EXPORT_C TRgb TRgb::Gray256(TInt aGray256) sl@0: { sl@0: aGray256&=0xff; sl@0: return(TRgb(aGray256,aGray256,aGray256)); sl@0: } sl@0: sl@0: /** Gets TRgb from 4 bit colour index. sl@0: sl@0: The function takes a 4 bit index into a colour palette and returns a TRgb sl@0: whose red, green and blue values are set to an appropriate level. sl@0: sl@0: @param aColor16 4 bit index into a colour palette sl@0: @return Equivalent 24 bit colour. */ sl@0: EXPORT_C TRgb TRgb::Color16(TInt aColor16) sl@0: { sl@0: return(TRgb(DynamicPalette::Color16array()[aColor16&0xf])); sl@0: } sl@0: sl@0: /** Gets TRgb from 8 bit colour index. sl@0: sl@0: The function takes an 8 bit index into a colour palette and returns a TRgb sl@0: whose red, green and blue values are set to an appropriate level. sl@0: sl@0: @param aColor256 8 bit index into a colour palette. sl@0: @return Equivalent 24 bit colour. */ sl@0: EXPORT_C TRgb TRgb::Color256(TInt aColor256) sl@0: { sl@0: return(TRgb(DynamicPalette::DefaultColor256Util()->iColorTable[aColor256&0xff])); sl@0: } sl@0: sl@0: /** Gets TRgb from 4K colour index. sl@0: sl@0: The function takes a 12 bit index into a colour palette and returns a TRgb sl@0: whose red, green and blue values are set to an appropriate level. sl@0: sl@0: @param aColor4K 12 bit index into a colour palette sl@0: @return Equivalent 24 bit colour. */ sl@0: EXPORT_C TRgb TRgb::Color4K(TInt aColor4K) sl@0: { sl@0: return _Color4K(aColor4K); sl@0: } sl@0: sl@0: /** Gets TRgb from 64K colour index. sl@0: sl@0: The function takes a 16 bit index into a colour palette and returns a TRgb sl@0: whose red, green and blue values are set to an appropriate level. sl@0: sl@0: @param aColor64K 16 bit index into a colour palette sl@0: @return Equivalent 24 bit colour. */ sl@0: EXPORT_C TRgb TRgb::Color64K(TInt aColor64K) sl@0: { sl@0: return _Color64K(aColor64K); sl@0: } sl@0: sl@0: /** Gets TRgb from 16M colour index. sl@0: sl@0: The function takes a 24 bit index into a colour palette and returns the TRgb sl@0: whose red, green and blue values represent it exactly. sl@0: sl@0: @param aColor16M 24 bit index into a colour palette sl@0: @return The TRgb which represents the index exactly. */ sl@0: EXPORT_C TRgb TRgb::Color16M(TInt aColor16M) sl@0: { sl@0: return _Color16M(aColor16M); sl@0: } sl@0: sl@0: /** Gets the index of the closest TRgb value to this, sl@0: based on the matching display mode. sl@0: sl@0: @return The index (0 - 1) representing the nearest TRgb. */ sl@0: EXPORT_C TInt TRgb::Gray2() const sl@0: { sl@0: return _Gray2(); sl@0: } sl@0: sl@0: /**Gets the index of the closest TRgb value to this, sl@0: based on the matching display mode. sl@0: sl@0: @return The index (0 - 3) representing the nearest TRgb. */ sl@0: EXPORT_C TInt TRgb::Gray4() const sl@0: { sl@0: return _Gray4(); sl@0: } sl@0: sl@0: /** Gets the index of the closest TRgb value to this, sl@0: based on the matching display mode. sl@0: sl@0: @return The index (0 - 15) representing the nearest TRgb.*/ sl@0: EXPORT_C TInt TRgb::Gray16() const sl@0: { sl@0: return _Gray16(); sl@0: } sl@0: sl@0: /** Gets the index of the closest TRgb value to this, sl@0: based on the matching display mode. sl@0: sl@0: @return The index (0 - 255) representing the nearest TRgb.*/ sl@0: EXPORT_C TInt TRgb::Gray256() const sl@0: { sl@0: return _Gray256(); sl@0: } sl@0: sl@0: /** Gets the index of the closest TRgb value to this, sl@0: based on the matching display mode. sl@0: sl@0: @return The index (0 - 15) representing the nearest TRgb. */ sl@0: EXPORT_C TInt TRgb::Color16() const sl@0: { sl@0: TInt index = (iValue & 0x000000e0) << 1; sl@0: index |= (iValue & 0x0000e000) >> 10; sl@0: index |= (iValue & 0x00e00000) >> 21; sl@0: return DynamicPalette::Color16inverse()[index]; sl@0: } sl@0: sl@0: /** Gets the index of the closest TRgb value to this, sl@0: based on the matching display mode. sl@0: sl@0: @return The index (0 - 255) representing the nearest TRgb. */ sl@0: EXPORT_C TInt TRgb::Color256() const sl@0: { sl@0: TInt index = (iValue & 0x000000f0) << 4; sl@0: index |= (iValue & 0x0000f000) >> 8; sl@0: index |= (iValue & 0x00f00000) >> 20; sl@0: return DynamicPalette::DefaultColor256Util()->iInverseColorTable[index]; sl@0: } sl@0: sl@0: /** Gets the index of the closest TRgb value to this, sl@0: based on the matching display mode. sl@0: sl@0: @return The index (0 - 4095) representing the nearest TRgb. */ sl@0: EXPORT_C TInt TRgb::Color4K() const sl@0: { sl@0: return _Color4K(); sl@0: } sl@0: sl@0: /** Gets the index of the closest TRgb value to this, sl@0: based on the matching display mode. sl@0: sl@0: @return The index (0 - 65535) representing the nearest TRgb.*/ sl@0: EXPORT_C TInt TRgb::Color64K() const sl@0: { sl@0: return _Color64K(); sl@0: } sl@0: sl@0: /** Gets the index of the closest TRgb value to this, sl@0: based on the matching display mode. sl@0: sl@0: @return The index (0 - 16777215) representing the nearest TRgb.*/ sl@0: EXPORT_C TInt TRgb::Color16M() const sl@0: { sl@0: return _Color16M(); sl@0: } sl@0: sl@0: /** Gets the difference between two TRgbs. sl@0: sl@0: This difference is defined as the sum of the absolute values of the difference sl@0: in the red, green and blue components. sl@0: sl@0: @param aColor The TRgb to be compared. sl@0: @return The sum of the absolute value of the differences between the red, green sl@0: and blue components. */ sl@0: EXPORT_C TInt TRgb::Difference(const TRgb& aColor) const sl@0: { sl@0: return(Abs((TInt)(aColor.Internal()&0xFF)-(TInt)(Internal()&0xFF))+ sl@0: (Abs((TInt)(aColor.Internal()&0xFF00)-(TInt)(Internal()&0xFF00))>>8)+ sl@0: (Abs((TInt)(aColor.Internal()&0xFF0000)-(TInt)(Internal()&0xFF0000))>>16)); sl@0: } sl@0: sl@0: /** Internalises a TRgb object from a stream. sl@0: sl@0: The presence of this function means that the standard templated stream operator>>() sl@0: is available to internalise objects of this class. sl@0: sl@0: @param aStream Stream from which the object is internalised. sl@0: @see operator>>() */ sl@0: EXPORT_C void TRgb::InternalizeL(RReadStream& aStream) sl@0: { sl@0: TInt red=aStream.ReadUint8L(); sl@0: TInt green=aStream.ReadUint8L(); sl@0: TInt blue=aStream.ReadUint8L(); sl@0: *this=TRgb(red,green,blue); sl@0: } sl@0: sl@0: /** Externalises a TRgb object to a stream. sl@0: sl@0: The presence of this function means that the standard templated stream operator<<() sl@0: is available to externalise objects of this class. sl@0: sl@0: @param aStream Stream to which the object is externalised. */ sl@0: EXPORT_C void TRgb::ExternalizeL(RWriteStream& aStream) const sl@0: { sl@0: aStream.WriteUint8L(Red()); sl@0: aStream.WriteUint8L(Green()); sl@0: aStream.WriteUint8L(Blue()); sl@0: } sl@0: sl@0: /** Gets TRgb from 16MU colour index. sl@0: The function takes a 24 bit colour value with eight bits for each sl@0: component, blue in the low byte, and returns the TRgb sl@0: whose red, green and blue values represent it exactly. sl@0: @param a0RGB The color - 0, R, G, B bytes. / BGR0 - little endian format / sl@0: @return The TRgb which represents the index exactly. */ sl@0: EXPORT_C TRgb TRgb::Color16MU(TInt a0RGB) sl@0: { sl@0: // R G B sl@0: return _Color16MU(a0RGB); sl@0: } sl@0: sl@0: /** Gets the index of the closest TRgb value to this, based on the matching display mode. sl@0: @return The index (0 - 16777215) representing the nearest TRgb. */ sl@0: EXPORT_C TInt TRgb::Color16MU() const sl@0: { sl@0: // R G B sl@0: return _Color16MU(); sl@0: } sl@0: sl@0: sl@0: /** Sets the alpha component. sl@0: sl@0: @param aAlpha Alpha component (0 - 255). */ sl@0: EXPORT_C void TRgb::SetAlpha(TInt aAlpha) sl@0: { sl@0: iValue&=0x00ffffff; sl@0: iValue|=(aAlpha << 24); sl@0: } sl@0: sl@0: sl@0: /** Gets TRgb from 16MA colour index. sl@0: The function takes a 32 bit colour value with eight bits for each sl@0: component, blue in the low byte, and returns the TRgb sl@0: whose red, green, blue and alpha values represent it exactly. sl@0: @param aARGB The color - A, R, G, B bytes. / BGRA - little endian format / sl@0: @return The TRgb which represents the index exactly. */ sl@0: EXPORT_C TRgb TRgb::Color16MA(TUint aARGB) sl@0: { sl@0: return _Color16MA(aARGB); sl@0: } sl@0: sl@0: /** Gets the index of the closest TRgb value to this, based on the matching display mode. sl@0: @return The index (0 - 16777215) representing the nearest TRgb. */ sl@0: EXPORT_C TUint TRgb::Color16MA() const sl@0: { sl@0: return _Color16MA(); sl@0: } sl@0: sl@0: /** Gets TRgb from 16MAP colour index. sl@0: The function takes a 32 bit colour value with eight bits for each sl@0: component, blue in the low byte, and returns the TRgb sl@0: whose red, green, and blue vales are divided by the alpha value. sl@0: @param aARGB The pre-multiplied (EColor16MAP) color value. sl@0: @return The TRgb which represents the color channel and alpha information. */ sl@0: /* static */ sl@0: EXPORT_C TRgb TRgb::Color16MAP(TUint aARGB) sl@0: { sl@0: const TUint16* ArTable = PtrTo16BitNormalisationTable(); sl@0: TRgb retColor; sl@0: retColor.SetInternal(PMA2NonPMAPixel(aARGB, ArTable)); sl@0: return retColor; sl@0: } sl@0: sl@0: /** Gets the index of the closest TRgb value to this, based on the matching display mode. sl@0: pre-multiplies the alpha channels with the color channel. sl@0: @return The pre-multiplied color value */ sl@0: EXPORT_C TUint TRgb::Color16MAP() const sl@0: { sl@0: return NonPMA2PMAPixel(iValue); sl@0: } sl@0: sl@0: sl@0: /** Tests whether the display mode specified is colour or greyscale. sl@0: sl@0: @param aDispMode The display mode. sl@0: @return ETrue if colour; EFalse if greyscale or monochrome. */ sl@0: EXPORT_C TBool TDisplayModeUtils::IsDisplayModeColor(TDisplayMode aDispMode) sl@0: { sl@0: return (aDispMode >= EColor16); sl@0: } sl@0: sl@0: /** Tests whether the display mode specified is one of the valid values. sl@0: sl@0: @param aDispMode The display mode to be tested. sl@0: @return ETrue if aDispMode is valid; EFalse if not valid. */ sl@0: EXPORT_C TBool TDisplayModeUtils::IsDisplayModeValid(TDisplayMode aDispMode) sl@0: { sl@0: return aDispMode >= ENone && aDispMode < EColorLast; sl@0: } sl@0: sl@0: /** Gets the number of colours or shades of grey supported by the specified sl@0: display mode. sl@0: sl@0: For instance, a display mode of EGray4 returns 4, EColor4K returns 4096. sl@0: sl@0: @param aDispMode The display mode. sl@0: @return The number of colours/grey shades supported by the display mode. */ sl@0: EXPORT_C TInt TDisplayModeUtils::NumDisplayModeColors(TDisplayMode aDispMode) sl@0: { sl@0: switch (aDispMode) sl@0: { sl@0: case EGray2: sl@0: return 2; sl@0: case EGray4: sl@0: return 4; sl@0: case EGray16: sl@0: case EColor16: sl@0: return 16; sl@0: case EGray256: sl@0: case EColor256: sl@0: return 256; sl@0: case EColor4K: sl@0: return 4096; sl@0: case EColor64K: sl@0: return 65536; sl@0: case EColor16M: sl@0: case EColor16MU: sl@0: case EColor16MA: sl@0: case EColor16MAP: sl@0: return 16777216; sl@0: default: sl@0: return 0; sl@0: }; sl@0: } sl@0: sl@0: /** Gets the number of bits required by each pixel when displayed in the sl@0: specified display mode. sl@0: sl@0: @param aDispMode The display mode. sl@0: @return The number of bits required by each pixel. */ sl@0: EXPORT_C TInt TDisplayModeUtils::NumDisplayModeBitsPerPixel(TDisplayMode aDispMode) sl@0: { sl@0: switch (aDispMode) sl@0: { sl@0: case EGray2: sl@0: return 1; sl@0: case EGray4: sl@0: return 2; sl@0: case EGray16: sl@0: case EColor16: sl@0: return 4; sl@0: case EGray256: sl@0: case EColor256: sl@0: return 8; sl@0: case EColor4K: sl@0: return 12; sl@0: case EColor64K: sl@0: return 16; sl@0: case EColor16M: sl@0: return 24; sl@0: case EColor16MU: sl@0: case EColor16MA: sl@0: case EColor16MAP: sl@0: return 32; sl@0: default: sl@0: return 0; sl@0: }; sl@0: } sl@0: sl@0: // sl@0: // TColor256Util sl@0: // sl@0: /** Initialises the two lookup tables using the specified palette. sl@0: sl@0: @param aPalette The palette of colours used to initialise the colour lookup sl@0: tables. */ sl@0: EXPORT_C void TColor256Util::Construct(const CPalette& aPalette) sl@0: { sl@0: TInt n = aPalette.Entries(); sl@0: if(n>256) sl@0: n = 256; sl@0: sl@0: TInt i; sl@0: for(i=0; i> 4; sl@0: index |= (aRgb.Value() & 0x0000f000) >> 8; sl@0: index |= (aRgb.Value() & 0x00f00000) >> 12; sl@0: return iInverseColorTable[index]; sl@0: } sl@0: sl@0: /** Gets the entries from the inverse colour lookup table for the colours that sl@0: most closely match the specified TRgb values. sl@0: sl@0: @param aDestination On return, a pointer to a buffer containing the entries sl@0: from the inverse colour lookup table. sl@0: @param aSource Pointer to the first TRgb value to match. sl@0: @param aNumPixels The number of TRgb values to match. */ sl@0: EXPORT_C void TColor256Util::Color256(TUint8* aDestination,const TRgb* aSource,TInt aNumPixels) const sl@0: { sl@0: TUint8* limit = aDestination+aNumPixels; sl@0: while(aDestination> 4; sl@0: index |= (value & 0x0000f000) >> 8; sl@0: index |= (value & 0x00f00000) >> 12; sl@0: *(aDestination++) = iInverseColorTable[index]; sl@0: } sl@0: } sl@0: sl@0: /** Returns a pointer to the system default 256 colour palette. sl@0: sl@0: @return Pointer to the system default 256 colour palette. */ sl@0: EXPORT_C const TColor256Util* TColor256Util::Default() sl@0: { sl@0: return DynamicPalette::DefaultColor256Util(); sl@0: } sl@0: