sl@0: // Copyright (c) 1997-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 "BMCONV.H"
sl@0: #include "RGB.H"
sl@0: 
sl@0: extern TRgb* color256Palette;
sl@0: extern char* color256InversePalette;
sl@0: 
sl@0: TRgb* color256Palette = NULL;
sl@0: char* color256InversePalette = NULL;
sl@0: 
sl@0: TRgb::TRgb()
sl@0: 	: iRed(255),iGreen(255),iBlue(255),iSpare(0)
sl@0: /** Constructs default TRgb with all 3 colour components set to 255. */
sl@0: 	{}
sl@0: 
sl@0: TRgb::TRgb(long unsigned int val)
sl@0: 	: iRed((unsigned char)(val&0xff)),iGreen((unsigned char)((val>>8)&0xff)),iBlue((unsigned char)((val>>16)&0xff)),iSpare(0)
sl@0: 	{}
sl@0: 
sl@0: TRgb::TRgb(int r,int g,int b)
sl@0: 	: iRed((unsigned char)r),iGreen((unsigned char)g),iBlue((unsigned char)b),iSpare(0)
sl@0: /** Constructs a TRgb from its three component colours.
sl@0: 	
sl@0: Each colour has a value between 0 and 255.
sl@0: 	
sl@0: @param aRed Red component of the colour (0 - 255). 
sl@0: @param aGreen Green component of the colour (0 - 255). 
sl@0: @param aBlue Blue component of the colour (0 - 255). */
sl@0: 	{}
sl@0: 
sl@0: TRgb &TRgb::operator=(const TRgb &col)
sl@0: 	{
sl@0: 	iRed=col.iRed;
sl@0: 	iGreen=col.iGreen;
sl@0: 	iBlue=col.iBlue;
sl@0: 	return(*this);
sl@0: 	}
sl@0: 
sl@0: int TRgb::operator==(const TRgb &col)
sl@0: 	{
sl@0: 	return(iRed==col.iRed && iGreen==col.iGreen && iBlue==col.iBlue);
sl@0: 	}
sl@0: 
sl@0: int TRgb::Difference(const TRgb& col) const
sl@0: 	{
sl@0: 	return abs(iRed-col.iRed) + abs(iGreen-col.iGreen) + abs(iBlue-col.iBlue);
sl@0: 	}
sl@0: 
sl@0: int TRgb::Gray2() const
sl@0: /** Gets a 2 level grayscale value from this TRgb. 
sl@0: 
sl@0: @return Equivalent 2 level grayscale value. The greyscale value is 0 or 1, 
sl@0: and is calculated using c=(2*r+5*g+b)/1024. Note that the return value is 
sl@0: rounded down to the nearest integer. */
sl@0: 	{
sl@0: 	return Gray256() / 128;
sl@0: 	}
sl@0: 
sl@0: int TRgb::Gray4() const
sl@0: /** Gets a 4 level grayscale value from this TRgb.
sl@0: 
sl@0: @return Equivalent 4 level grayscale value. The greyscale value is calculated 
sl@0: using c=(2*r+5*g+b)/512. Note that the return value is rounded down to the 
sl@0: nearest integer. */
sl@0: 	{
sl@0: 	return Gray256() / 64;
sl@0: 	}
sl@0: 
sl@0: int TRgb::Gray16() const
sl@0: /** Gets a 16 level grayscale value from this TRgb.
sl@0: 
sl@0: @return The greyscale value is calculated using c=(2*r+5*g+b)/128. Note that 
sl@0: the return value is rounded down to the nearest integer. */
sl@0: 	{
sl@0: 	return Gray256() / 16;
sl@0: 	}
sl@0: 
sl@0: int TRgb::Gray256() const
sl@0: /** Gets a 256 level grayscale value from this TRgb.
sl@0: 
sl@0: @return The greyscale value is calculated using c=(2*r+5*g+b)/8. Note that 
sl@0: the return value is rounded down to the nearest integer. */
sl@0: 	{
sl@0: 	return((2*iRed+5*iGreen+iBlue)/8);
sl@0: 	}
sl@0: 
sl@0: int TRgb::Color16() const
sl@0: /** Gets a 4 bit index into a colour palette from this TRgb.
sl@0: 
sl@0: @return The EGA low colour constant closest to the TRgb. */
sl@0: 	{
sl@0: 	int index = (iRed >> 5) & 0x007;
sl@0: 	index |= (iGreen  >> 2) & 0x038;
sl@0: 	index |= (iBlue << 1) & 0x1c0;
sl@0: 	return color16inverse[index];
sl@0: 	}
sl@0: 
sl@0: int TRgb::Color256() const
sl@0: /** Gets an 8 bit index into a colour palette from this TRgb.
sl@0: 
sl@0: @return An 8 bit index. */
sl@0: 	{
sl@0: 	int index = (iRed >> 4) & 0x00f;
sl@0: 	index |= iGreen & 0x0f0;
sl@0: 	index |= (iBlue << 4) & 0xf00;
sl@0: 
sl@0: 	if (color256InversePalette)
sl@0: 		return color256InversePalette[index];
sl@0: 	else
sl@0: 		return color256inverse[index];
sl@0: 	}
sl@0: 
sl@0: int TRgb::Color4K() const
sl@0: /** Gets a 12 bit index into a colour palette from this TRgb.
sl@0: 
sl@0: @return A 12 bit index. */
sl@0: 	{
sl@0: 	return(((iRed&0xf0)<<4)|(iGreen&0xf0)|((iBlue&0xf0)>>4));
sl@0: 	}
sl@0: 
sl@0: int TRgb::Color64K() const
sl@0: /** Gets a 24 bit index into a colour palette from this TRgb.
sl@0: 
sl@0: @return A 24 bit index. */
sl@0: 	{
sl@0: 	return(((iRed&0xf8)<<8)|((iGreen&0xfc)<<3)|((iBlue&0xf8)>>3));
sl@0: 	}
sl@0: 
sl@0: long int TRgb::Color16M() const
sl@0: /** Gets a 16 bit index into a colour palette from this TRgb.
sl@0: 
sl@0: @return A 16 bit index. */
sl@0: 	{
sl@0: 	return((iRed<<16)|(iGreen<<8)|iBlue);
sl@0: 	}
sl@0: 
sl@0: TRgb TRgb::Gray2(int aGray2)
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: 	{
sl@0: 	aGray2 *= 255;
sl@0: 	return TRgb(aGray2,aGray2,aGray2);
sl@0: 	}
sl@0: 
sl@0: TRgb TRgb::Gray4(int aGray4)
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: 	{
sl@0: 	aGray4 *= 85;
sl@0: 	return TRgb(aGray4,aGray4,aGray4);
sl@0: 	}
sl@0: 
sl@0: TRgb TRgb::Gray16(int aGray16)
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: 	{
sl@0: 	aGray16 *= 17;
sl@0: 	return TRgb(aGray16,aGray16,aGray16);
sl@0: 	}
sl@0: 
sl@0: TRgb TRgb::Gray256(int aGray256)
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: 	{
sl@0: 	return TRgb(aGray256,aGray256,aGray256);
sl@0: 	}
sl@0: 
sl@0: TRgb TRgb::Color16(int aColor16)
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: 	{
sl@0: 	return TRgb(color16array[aColor16&0xf]);
sl@0: 	}
sl@0: 
sl@0: TRgb TRgb::Color256(int aColor256)
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: 	{
sl@0: 	if (color256Palette)
sl@0: 		return color256Palette[aColor256&0xff];
sl@0: 	else
sl@0: 		return TRgb(color256array[aColor256&0xff]);
sl@0: 	}
sl@0: 
sl@0: TRgb TRgb::Color4K(int aColor4K)
sl@0: 	{
sl@0: 	return TRgb(((aColor4K>>8)&0xf)*17,((aColor4K>>4)&0xf)*17,(aColor4K&0xf)*17);
sl@0: 	}
sl@0: 
sl@0: TRgb TRgb::Color64K(int aColor64K)
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: 	{
sl@0: 	return TRgb(((aColor64K>>11)&0x1f)*255/31,((aColor64K>>5)&0x3f)*255/63,(aColor64K&0x1f)*255/31);
sl@0: 	}
sl@0: 
sl@0: TRgb TRgb::Color16M(long int aColor16M)
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: 	{
sl@0: 	return TRgb(((aColor16M>>16)&0xff),(aColor16M>>8)&0xff,aColor16M&0xff);
sl@0: 	}
sl@0: